2016-11-08 84 views
0

我想用骆驼休息时间呼叫触发长时间操作。由于该操作需要花费大量时间,因此请求会超时并在客户端显示错误消息。我不希望发生这种情况。使用骆驼休息服务触发长时间操作

所以,我想用队列如下解决这个问题:

<bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent"> 
    <property name="brokerURL" value="tcp://0.0.0.0:61616" /> 
    <property name="userName" value="admin"/> 
    <property name="password" value="admin"/> 
</bean> 

<camelContext xmlns="http://camel.apache.org/schema/blueprint" trace="true"> 
    <route> 
     <from uri="cxfrs:bean:rsprovider" /> 
     <setBody> 
      <simple>${header.operationName}</simple> 
     </setBody> 
     <to uri="activemq:queue:myqueue"/> 
     <!-- Send immediate response to client as, the processing will take a while --> 
     <setBody> 
      <simple>Received feeder service request to ${header.operationName}. Request will be processed soon.</simple> 
     </setBody> 
    </route> 

    <route> 
     <from uri="activemq:queue:myqueue"/> 
     <convertBodyTo type="java.lang.String"/> 
     <recipientList> 
      <simple>direct-vm:operation-${body}</simple> 
     </recipientList> 
    </route> 
</camelContext> 

但是,似乎平添了队列同步和响应没有按时收到。我如何使这个队列异步?我尝试追加?jms.useAsyncSend=true到队列的URL。但那并不奏效。

回答

0

你应该尝试asyncConsumer

<from uri="activemq:queue:myqueue?asyncConsumer=true"/> 

骆驼2.9:是否JmsConsumer处理交易所 异步。如果启用,则JmsConsumer可以从JMS队列中获取下一个 消息,而之前的消息是异步处理的(由异步路由引擎) 。这个 意味着消息可能不是100%严格按照处理。如果 已禁用(默认情况下),则Exchange将完全处理,然后JmsConsumer将从JMS队列中提取下一条消息。注意:如果 交易已启用,则asyncConsumer=true不会异步运行 ,因为交易必须同步执行(骆驼 3.0可能支持异步交易)。

同时检查异步请求回复的API

http://camel.apache.org/async.html

Future<Object> future = template.asyncRequestBody("activemq:queue:myqueue", "<YOUR_MESSAGE>");