2016-06-29 12 views
0

私は、アプリケーションinitでデータベースからmule appプロパティを読み込み、それらをmuleフローのプロパティプレースホルダに設定することに成功しました。このコードはここで参照されていますRead mule props from the DB実行時にjava.util.PropertiesオブジェクトからMuleフロープロパティプレースホルダにプロパティを設定する

ただし、これはアプリの起動時にのみ機能します。私は、データベースのプロパティを変更することができます(私は可能です)、それは実行時にミュールサーバーを再起動することなく、ミュールフローに反映させたいと考えています。

これを達成するために、データベースからプロパティを読み取ってPropertySourcesPlaceHolderConfigurerクラスを使用してBeanファクトリに設定しようとするJavaクラスを呼び出すHttp Listenerを使用して新しいフローを作成しました。 javaクラスのサンプルコードは次のようになります。

@Autowired 
ConfigurableListableBeanFactory configurableListableBeanFactory; 

@Autowired 
MyService myService; 

public MuleEventContext onCall(MuleEventContext eventContext){ 
Properties properties = new Properties(); 

    // get properties from the database 
    Map<String,String> propertiesMap = getMuleAppPropertiesFromDB(); 
    if(null != propertiesMap && !CollectionUtilsIntg.isEmpty(propertiesMap)) 
     properties.putAll(propertiesMap); 

PropertySourcesPlaceholderConfigurer cfg = new PropertySourcesPlaceholderConfigurer(); 
cfg.setProperties(properties); 
cfg.postProcessBeanFactory(configurableListableBeanFactory); 
} 

このコードは正常に実行されましたが、実行時に異常なアプリケーションフローにプロパティを設定できませんでした。

他にどのようにこれを達成することができますか?

私はPropertyPlaceholders寿命は、すぐにアプリケーションは、それらがアプリケーションの初期化にとだけ豆の作成時に限定されている、完全に、すなわち起動すると終了していると信じ

答えて

1

を助けてください。あなたは、実行時にプロパティを変更することができるようにしたい場合は、プロパティのプレースホルダを使用する代わりに、ファイルからの読み込みのorg.springframework.beans.factory.config.PropertiesFactoryBean

<?xml version="1.0" encoding="UTF-8"?> 
<mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd 
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd 
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd 
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd"> 

    <http:listener-config doc:name="HTTP Listener Configuration" host="0.0.0.0" name="HTTP_Listener_Configuration" port="8081"/> 
    <spring:beans> 
     <spring:bean id="myProps" name="myProps" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> 
      <spring:property name="properties"> 
       <bean factory-bean="databasePropertiesProvider" factory-method="getProperties" />  
      </spring:property> 
     </spring:bean> 
    </spring:beans> 
    <flow name="test"> 
     <http:listener config-ref="HTTP_Listener_Configuration" doc:name="Recieve HTTP request" path="/test"/> 
     <logger message="#[app.registry.myProps['testPropertyName']]" /> 
    </flow> 
</mule> 

のBeanを作成するように他のプロパティのメカニズムを使用しないでください、あなたは、DBからロードするために使用することができます。次にあなたのミュール設定で#[app.registry.myPropes ['mykey']]としてこれらを使用します。 MELコンテキストオブジェクトhereについて読む。

上記のサンプルコードでは、myPoros beanとロードされたプロパティをデータベースから登録しました。 app.registryは、アプリケーションレジストリコンテキストオブジェクトで、Muleで使用できます。これにより、Spring Beanにアクセスできます。

+0

回答ありがとう@ manik-magar。 PropertiesFactoryBeanを使用してデータベースからロードされたプロパティを設定するにはどうすればよいですか?上記の質問で示したように、私はjava.util.Propertiesオブジェクトを持っています。 app.registryとは何ですか? Propertiesオブジェクトをレジストリに設定する必要はありますか?もしそうなら、どのように?助けてください!! –

+0

サンプルコードを表示するための回答が更新されました。これは、プロパティー設定者、特にBeanを定義するプロパティーの100%を置き換えることはできません。たとえば、httpリスナーのホスト、またはファイル受信エンドポイントのパスを指定します。これらのプロパティはBeanの作成時に読み込まれ、ソースの変更は反映されません。再起動アプリ(つまり、変更がアプリケーションの開始時にのみ機能していたからです)。 –

+0

返事と非常に良い説明をありがとう@ manik-magar !!!!私はこれを試して、それが私のために働くかどうかを見てみましょう。私はそれがどのように更新されます。 よろしく! –

関連する問題