【问题标题】:Using parameters in a Mule app在 Mule 应用程序中使用参数
【发布时间】:2015-01-29 21:34:20
【问题描述】:

我为 Mule 流编写了一个自定义 Java 组件。我关注these instructions 并实现了 Callable 接口。另外,我需要通过属性配置我的 mule App,所以我遵循了here 描述的方法。

我的问题是我无法从组件的 Java 代码中检索属性值,即使用 onCall() 方法。我需要的是一种通过配置将一些参数传递给我的自定义 mule 组件的简单方法。

【问题讨论】:

    标签: java spring properties mule


    【解决方案1】:

    在以下示例中,属性名称在 abc.properties 中配置。在流中,它被设置为流变量并在java组件中访问。

    这是流程

    <context:property-placeholder location="abc.properties"/>
    
    <flow name="EchoFlow" doc:name="EchoFlow">
        <http:inbound-endpoint exchange-pattern="request-response"
            host="localhost" port="8084" doc:name="HTTP"
            doc:description="Process HTTP requests or responses." />
        <set-variable value="${name.from.config}" variableName="name"
            doc:name="Variable" />
        <component class="TestComponent" doc:name="Java"/>
        <logger doc:name="Logger" level="INFO" message="#[name]" />
    </flow>
    

    这是组件

    import org.mule.api.MuleEventContext;
    import org.mule.api.lifecycle.Callable;
    import org.mule.api.transport.PropertyScope;
    
    public class TestComponent implements Callable {
    
        @Override
        public Object onCall(MuleEventContext eventContext) throws Exception {
            // This is how you access a property
            String httpMethod = eventContext.getMessage().getProperty("http.method", PropertyScope.INBOUND);
            System.out.println("The value of property name is >>>" + httpMethod);
    
            // This is how you access flow variable
            String name = eventContext.getMessage().getInvocationProperty("name");
            System.out.println("The value of property name is >>>" + name);
            return name + " > " + httpMethod;
        }
    
    }
    

    【讨论】:

    • 我明白了,谢谢。在这里查看mulesoft.org/docs/site/current3/schemadocs/schemas/mule_xsd/… 后,我意识到我为什么会感到困惑。这有点复杂,但很有意义:您需要一个转换器来向消息添加一个变量(可以是属性/配置值),以便在组件的 Java 代码中检索它:)
    猜你喜欢
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-23
    • 1970-01-01
    • 2022-01-24
    相关资源
    最近更新 更多