Mule表达式语言,用于从属性文件中动态读取属性

时间:2013-06-02 18:24:24

标签: properties mule mule-studio mule-el

我有一个Mule流,其中有一个会话变量“servicerequested”,这个变量可以有不同的值,如customerservice,accountservice,transferservice等。

有一个属性文件mule.dev.properties,其中包含基于此服务选项的端口信息:

customerservice=9914 
accountservice=9918
transferservice=9919

我希望http出站端点根据变量requestedservice从此属性文件中选择端口。 我尝试使用MEL如下:

$ {#[报头:会话:servicerequested]}

<http:outbound-endpoint exchange-pattern="request-response" host="localhost" 
port="${#[header:session:servicerequested]}" path="services" method="GET"/>

但它会引发异常

Template Endpoint "http://localhost:session:servicerequested]/services" resolved 
into a Malformed endpoint "http://localhost:session:servicerequested]/services"

请告诉我们如何使用MEL动态读取属性文件。

2 个答案:

答案 0 :(得分:0)

从你的帖子中我可以看到会话变量是基于其选择属性文件中的值的条件。

但问题是部署应用程序后会加载属性文件。但是会话变量仅在流程执行期间可用。

因此表达式$ {#[header:session:servicerequested]}将无效,因为在引用属性文件的值时,#[MEL]尚不可用。

可能的解决办法可能是将您的属性加载到某些流量变量中,然后根据会话变量值选择其中一个流量变量。

所有这一切都不会发生在一个MEL声明中。

尝试将属性存储为流变量,如下所示

<set-variable variableName="prop1" value="${property key from properties file}" />
<set-variable variableName="prop2" value="${property key from properties file}" />

然后构建一个自定义组件或groovy来评估会话变量并选择其中一个流变量,然后在你的port属性中使用该值。

希望这会有所帮助:)

答案 1 :(得分:0)

您可以直接创建java组件和读取属性。这是最简单的解决方案。 替代方法是在Spring中配置ReloadableResourceBundleMessageSource以读取属性。