【问题标题】:How to set ActiveMQ message header in Mule?如何在 Mule 中设置 ActiveMQ 消息头?
【发布时间】:2014-06-27 10:15:10
【问题描述】:

我正在使用 MuleStudio 3.5.0 并尝试创建一个向 ActiveMQ 队列发送消息的流。该队列由一个旧的现有服务(用 .Net 编写)使用,我想将其集成到我的工作流程中。该服务期望消息的标头 Type 设置为“DoStuff”。我怎样才能做到这一点?

我尝试了以下流程。请注意,出于测试目的,我通过 DoStuff 类手动设置了 ActiveMQ 消息的负载,HTTP 连接器仅用于触发工作流:

<jms:activemq-connector name="Active_MQ" brokerURL="tcp://localhost:61616" validateConnections="true" doc:name="Active MQ"/>
<spring:beans>
    <spring:bean id="DoStuffBean" name="DoStuffBean" class="foo.DoStuff"/>
</spring:beans>
<flow name="Flow1" doc:name="Flow1">
    <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8199" path="activemq" doc:name="HTTP"/>
    <component doc:name="Java" class="com.kws.mule.DoStuff"/>
    <json:object-to-json-transformer doc:name="Object to JSON"/>
    <jms:outbound-endpoint queue="MyServiceQueue" connector-ref="Active_MQ" doc:name="JMS">
        <jms:object-to-jmsmessage-transformer name="ObjectToJmsMessage" />
        <message-properties-transformer>
            <add-message-property key="Type" value="DoStuff" />
        </message-properties-transformer>
    </jms:outbound-endpoint>
</flow>

但这不起作用,因为没有设置标题 Type,而是设置了一个属性。这显示在 ActiveMQ 代理管理器控制台的图像中

这里的参考是DoStuff 类:

public class DoStuff implements Callable {
    private String jobId;
    private String createdTime;

    public String getJobId()
    {
        return this.jobId;
    }

    public String getResult()
    {
        return this.createdTime;
    }

    @Override
    public Object onCall(MuleEventContext eventContext) throws Exception {
        DoStuff result = new DoStuff();
        result.jobId = "abd4df7b-0f31-41b4-826a-3dbbe77df7eb";
        result.createdTime = "0001-01-01T00:00:00";

        eventContext.getMessage().setPayload(result);
        return eventContext.getMessage();
    }
}

【问题讨论】:

    标签: jms activemq mule


    【解决方案1】:

    MEL,你可以这样做

    <set-property propertyName="Type" value="Set your value here"/>
    

    这将在Mule Message 上设置一个出站属性JMS Queue 将作为入站属性接收它

    【讨论】:

    • 感谢您的回答!不幸的是,这设置了消息的 property Type,而不是我想要的 header。我想我的答案不够清楚,所以我更新了一下。
    • 当您尝试检索此属性时会得到什么。尝试使用 inbound-endpoint 作为此活动 MQ 创建另一个流并在那里检索此属性。看看你得到哪一个
    • 你说得对,如果我用 Mule 构建一个新的消费 inbound-endpoint,我就会得到这个属性。但是已经存在的使用 ActiveMQ 队列的服务不是用 Mule 构建的,而是一个旧的 .Net 应用程序,它期望类型 header 被填充。不幸的是,我无法访问 ActiveMQ 消费源代码,因为这部分是专有的,因此我想设置 JMS 消息的标头。
    • &lt;set-property&gt; 是设置标题的最佳方式。您应该尝试查看 .Net 应用程序获取的 Type 是什么。可能是它得到了正确的类型。
    • 它无法解析消息,因为 .Net 应用程序不知道存在这样的自定义属性,它仍然需要标头 :( 如果我通过 ActiveMQ 代理管理器控制台发送自定义消息 并设置 Type 标头,.Net 应用程序可以代替。
    【解决方案2】:

    这个流程应该可以工作:

    <flow name="Flow1" doc:name="Flow1">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8199" path="activemq" doc:name="HTTP"/>
        <component doc:name="Java" class="com.kws.mule.DoStuff"/>
        <json:object-to-json-transformer doc:name="Object to JSON"/>
        <jms:outbound-endpoint queue="MyServiceQueue" connector-ref="Active_MQ" doc:name="JMS">
            <jms:object-to-jmsmessage-transformer name="ObjectToJmsMessage" />
            <expression-transformer returnSourceIfNull="true"
                doc:name="Expression"
                expression="#[payload.setJMSType(&quot;DoStuff&quot;); return payload]" />
        </jms:outbound-endpoint>
    </flow>
    

    【讨论】:

      猜你喜欢
      • 2015-07-23
      • 1970-01-01
      • 1970-01-01
      • 2016-04-13
      • 2013-05-09
      • 2013-01-07
      • 2015-08-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多