【问题标题】:Mule 3 retrieving the placeholder value dynamicallyMule 3 动态检索占位符值
【发布时间】:2020-08-09 18:54:39
【问题描述】:

我有一个用例,我需要从我的属性文件中检索值,但该键应该从我的查询参数中动态派生。

如何在 MEL 或 Groovy 中处理这个问题?我知道在 DW 中是可能的。

Http request
https://localhost:9898/getStore?search=customer.weststore.name

我的占位符是 -

file.properties
customer.weststore.name=TESTING
customer.eaststore.name=IRERRER

所以我需要访问这样的东西的方式

<set-variable variableName="westDetail" value="#[message.inboundProperites['customer.weststore.name']" doc:name="Variable"/>
<logger message="${westDetail}" level="INFO" /> --> Failed as no placeholder available

当我尝试上述方法时,由于没有可用的“westDetail”占位符而失败,而我需要从属性文件中获取该特定键。

这与本文有关 - https://help.mulesoft.com/s/question/0D52T00004mXTQUSA4/dynamically-read-property-values-from-a-properties-file-in-mule 但 DW 提供的唯一解决方案不是 MEL 或 Groovy。

有人怎么建议,有可能吗?

【问题讨论】:

    标签: groovy mule mule-esb mule-el


    【解决方案1】:

    我知道问题是您想通过在执行时获得的键来查询属性。

    你做错了。 ${} 用于评估属性的值,这是在应用程序初始化时完成的。您错过了在 set-variable 中获取实际值的方法。

    #[] 用于执行 MEL 表达式,该表达式在执行时发生。 flowVars.westDetail 是一个 MEL 表达式,它返回流变量 westDetail 的值。您不能使用 MEL 表达式来评估属性占位符 ${},因为它们是在不同的时间进行评估的。

    一种解决方案是使用 Spring bean 来存储属性,而不是配置属性占位符。然后您可以将其分配给流变量并像地图一样访问它。

    例子:

    <spring:beans>
      <spring:bean id="myProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <spring:property name="location" value="classpath:items.properties"/>      
      </spring:bean>    
    </spring:beans>
    
    <flow name="myFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
        <set-variable value="#[app.registry.myProperties]" variableName="props"></set-variable>
        <logger message="a=#[flowVars.props['a']]" level="INFO"/>
    </flow>
    

    items.properties:

    a=1
    b=2
    c=3
    

    输出:

    a=1
    

    【讨论】:

    • 谢谢,但正如我在用例中提到的,它动态传递 flowVars 来读取属性值,因此最终变量的值和键名应该相同。这是我试图探索的实际问题 - help.mulesoft.com/s/question/0D52T00004mXTQUSA4/…
    • 应该更新问题以澄清这一点。问题应该是独立的,而不是依赖于外部链接。
    • 更新了我的编辑和它的自包含但有点类似的用例。
    猜你喜欢
    • 2015-09-17
    • 1970-01-01
    • 2010-09-11
    • 1970-01-01
    • 2019-07-25
    • 2016-10-03
    • 1970-01-01
    • 2013-12-06
    • 1970-01-01
    相关资源
    最近更新 更多