JMS从同步异步 [英] JMS from Synchronous to Asynchronous

查看:328
本文介绍了JMS从同步异步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够创建一个使用JMS同步一个服务总线,但我不能把它转化为异步。
我试图以异步方式发布到服务的请求,所以如果服务停止,我希望JMS队列保持请求消息并在服务启动时,它提供了消息服务,并取回响应。

I was able to create a Synchronous service bus using the JMS, but I was not able to turn it to Asynchronous. I'm trying to post a request to a service asynchronously, so if the service is down, I want the JMS queue keeps the request message and when the service starts up it delivers the message to the service and get back the response.

这是我的code

<camelContext xmlns="http://camel.apache.org/schema/spring">
        <route >
            <from id ="server" uri="jetty:http://0.0.0.0:9500/rsb/toService?matchOnUriPrefix=true&amp;enableMultipartFilter=false&amp;disableStreamCache=false"/>
            <wireTap uri="log:test?level=INFO"><body><simple>Enter JMS test route and sending message to queue</simple></body></wireTap>
            <!--<to uri="direct:queue"/>-->
            <to uri="jms://testqueue?requestTimeout=360000&amp;replyTo=bar&amp;replyToDeliveryPersistent=true&amp;exchangePattern=InOut&amp;acknowledgementModeName=AUTO_ACKNOWLEDGE"/>
        </route>
        <route id="testqueuelistener" streamCache="true">
            <from uri="jms://testqueue?replyToDeliveryPersistent=true&amp;replyTo=bar" />
            <wireTap uri="log:test?level=INFO"><body><simple>Message recieved from queue: ${body}</simple></body></wireTap>             
            <to uri="http://localhost:18402/Home/addUser?bridgeEndpoint=true&amp;throwExceptionOnFailure=false"/>
            <to uri="jms:service"/> 
        </route>
        <route >
            <from uri="jms:service"/>
            <transform>
                <simple>${body}</simple>
            </transform>
        </route>
    </camelContext>

请帮我
在此先感谢

please help me Thanks in advance

推荐答案

的问题是,你要访问的JMS队列不使用事务 - 所以只要你得到的消息,它从队列中消失了。你需要使用一个事务,只提交(或回滚)消息消耗你处理完之后。

The issue is that you are accessing the JMS queue without using a transaction - so as soon as you get the message, it's gone from the queue. You need to use a transaction and only commit (or rollback) the message consumption after you've finished processing it.

相关企业集成模式是href=\"http://camel.apache.org/transactional-client.html\" rel=\"nofollow\">交易客户端的 JMS组件文档还提供了有关交易的一些信息。最后,在行动(第二版第12章)骆驼的第9章是专门为交易(我不能推荐它不够!)。

The relevant Enterprise Integration Pattern is the transactional client. The JMS component documentation also provides some information about transaction. Finally, chapter 9 of Camel in Action (chapter 12 for the second edition) is dedicated to transactions (and I can't recommend it enough!).

您需要:


  • 获取JMS事务管理器(您使用的事务管理器可能取决于您的具体的用例)

  • 配置骆驼JMS组件使用的事务管理器

  • 使用一个交易策略配置路线的事务行为(或仅标记为交易的路线,并使用默认策略)

配置可以是这样:

<!-- Import JMS connection factory -->
<osgi:reference id="jmsConnectionPool" interface="javax.jms.ConnectionFactory" />
<!-- We create a Spring JmsTransactionManager (our transaction manager could also be an
imported OSGi service, like we do for the connection factory; for example an XA transaction
manager) -->
<bean id="jmsTxManager" class="org.springframework.jms.connection.JmsTransactionManager">
    <property name="connectionFactory" ref="jmsConnectionPool"/>
</bean>
<!-- We configure the JMS component to use the transaction manager-->
<bean id="jms" class="org.apache.camel.component.jms.JmsComponent">
    <property name="connectionFactory" ref="jmsConnectionPool" />
    <property name="transacted" value="true"/>
    <property name="transactionManager" ref="jmsTxManager"/>
</bean>

<!-- Here's an example of a transaction policy -->
<bean id="requiresNew" class="org.apache.camel.spring.spi.SpringTransactionPolicy">
    <property name="transactionManager" ref="jtaTransactionManager"/>
    <property name="propagationBehaviorName" value="PROPAGATION_REQUIRES_NEW"/>
</bean>

下面是一个交易的路线:

Here's a transacted route:

<route id="myRoute">
    <from uri="jms://..." />
    <transacted/>
    ...
</route>

和路由可以使用特定的交易政策,如果我们想要的:

And a route can use a specific transaction policy if we want:

<route id="myRoute">
    <from uri="jms://..." />
    <transacted ref="requiresNew" />
    ...
</route>

这篇关于JMS从同步异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆