如何使用ActiveMQ在Mule中将呼叫负载返回到呼叫者流量?

时间:2015-02-25 06:44:17

标签: java jms mule activemq

<jms:activemq-connector name="jms-connector"  brokerURL="${BrokerURL}" disableTemporaryReplyToDestinations="true"  maxRedelivery="10" durable="true" specification="1.1"/>
<jms:endpoint connector-ref="jms-connector"  name="SyncRequestsQueue" queue="SyncRequestsQueue">
    <jms:transaction action="ALWAYS_BEGIN"/>
</jms:endpoint>

<flow name = "RequestsEntryFlow" >
    <http:listener allowedMethods="POST"  parseRequest="false"  config-ref="HTTP_Request_Listener" path="/{name}" doc:name="HTTP Entry Flow" />
    <flow-ref name="requestBeanCreate" />
    <jms:outbound-endpoint connector-ref="jms-connector" exchange-pattern="request-response" ref="SyncRequestsQueue" /> 
</flow>

<flow name = "SyncRequestsQueueConsumer">
    <jms:inbound-endpoint  connector-ref="jms-connector" exchange-pattern="request-response" ref="SyncRequestsQueue" />
    <set-variable variableName="id" value="#[payload]" doc:name="Variable" />
    <set-variable variableName="destination" value="#[app.registry.routerBean.getDestination(app.registry.messageBean.getReceiverID(payload))]"  doc:name="Variable" />
    <set-payload value="#[app.registry.messageBean.sendRequestToDestination(payload)]" />
    <processor-chain>
        <http:outbound-endpoint address="http://#[flowVars.destination]" exchange-pattern="request-response">
             <transformer ref="contentTypeTextXML"/>
        </http:outbound-endpoint>                     
        <expression-component>
            app.registry.messageBean.sendResponseToSender(flowVars.id);
            app.registry.messageBean.messageProcessedSuccessfully(flowVars.id);
        </expression-component>
    </processor-chain>
</flow>

这是情景:

对远程服务的请求进入RequestsEntryFlow。 我用它做一些日志记录并将其发送到SyncRequestsQueue。 SyncRequestsQueueConsumer使用队列并将内容发送到远程服务器。 现在我想将远程服务器的响应返回给RequestsEntryFlow并从那里返回到服务调用者。 但是响应并没有回到服务呼叫者那里。

注意:当我从代码中删除JMS和Activemq并使用SyncRequestsQueueConsumer作为子流时,一切正常。所以我猜JMS连接器不能按我需要的方式工作。

代码有什么问题,如何解决?谢谢

- 编辑 -

我已将代码更改为采用范围,现在看起来像这样:

<flow name = "RequestsEntryFlow" >
    <http:listener allowedMethods="POST"  parseRequest="false"  config-ref="HTTP_Request_Listener" path="/{name}" doc:name="HTTP Entry Flow" />
    <flow-ref name="requestBeanCreate" />                          

    <choice doc:name="Choice">
        <when expression="#[app.registry.messageBean.isEMCRequired(payload)]">
            <jms:outbound-endpoint connector-ref="jms-connector"  ref="EMCRequiredRequestsQueue" />
        </when>
        <otherwise>                         
         <request-reply>
                <jms:outbound-endpoint connector-ref="jms-connector" exchange-pattern="one-way" ref="SyncRequestsQueue" />
                <jms:inbound-endpoint connector-ref="jms-connector" exchange-pattern="one-way"  queue="ResponseTempQueue" >
                    <set-payload value="#[payload.get(0)]"/>
                    <remove-property propertyName="Content_Type"/>
                    <transformer ref="contentTypeTextXML"/>
                </jms:inbound-endpoint>
             </request-reply>
        </otherwise>
    </choice>        
</flow>

<flow name = "SyncRequestsQueueConsumer">
    <jms:inbound-endpoint connector-ref="jms-connector" exchange-pattern="request-response" ref="SyncRequestsQueue" />
    <set-variable variableName="id" value="#[payload]" doc:name="Variable" />
    <set-variable variableName="destination" value="#[app.registry.routerBean.getDestination(app.registry.messageBean.getReceiverID(payload))]"  doc:name="Variable" />
    <set-payload value="#[app.registry.messageBean.sendRequestToDestination(payload)]" />

    <http:outbound-endpoint address="http://#[flowVars.destination]" exchange-pattern="request-response">
    <transformer ref="contentTypeTextXML"/>
    </http:outbound-endpoint>                          
    <expression-component>
        app.registry.messageBean.sendResponseToSender(flowVars.id);
        app.registry.messageBean.messageProcessedSuccessfully(flowVars.id);
    </expression-component>

</flow>

如果我将e logger添加到我的代码中,它将打印正确的有效负载(来自服务器的响应)

                <jms:inbound-endpoint connector-ref="jms-connector" exchange-pattern="one-way"  queue="ResponseTempQueue" >
                    <set-payload value="#[payload.get(0)]"/>
                    <remove-property propertyName="Content_Type"/>
                    <transformer ref="contentTypeTextXML"/>
                    <logger level="INFO" message="#[payload]" />
                </jms:inbound-endpoint>

但响应未通过HTTP路由回发件人

2 个答案:

答案 0 :(得分:1)

尝试使用RequestsEntryFlow中的请求回复范围

请求块中的请求回复范围中的

定义了SyncRequestsQueue和请求回复范围的回复块。

在流程结束时的SyncRequestsQueueConsumer流中的

定义具有与请求 - 回复范围的回复块中定义的相同队列名称的VM。

答案 1 :(得分:-1)

我猜您没有正确使用请求回复。您能否请完成请求 - 回复范围的示例实现,并了解请求 - 回复范围。

我希望这在某种程度上有所帮助:

<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
    <flow name="raml-http-request-connectorFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="HTTP"/>
        <set-payload value="heloo" doc:name="Set Payload"/>
        <request-reply doc:name="Request-Reply">
            <vm:outbound-endpoint exchange-pattern="one-way" path="input" doc:name="VM"/>
            <vm:inbound-endpoint exchange-pattern="one-way" path="output" doc:name="VM"/>
        </request-reply>
        <logger message="log payload is:::::::::::::::::::::#[payload]" level="INFO" doc:name="Logger"/>
    </flow>
    <flow name="raml-http-request-connectorFlow1">
        <vm:inbound-endpoint exchange-pattern="one-way" path="input" doc:name="VM"/>
        <set-payload value="#[payload+'  welome']" doc:name="Set Payload"/>
        <vm:outbound-endpoint exchange-pattern="one-way" path="output" doc:name="VM"/>
    </flow>