将消息放入队列的第一条路由和第二条读取消息的路由

时间:2012-07-04 14:49:18

标签: jms apache-camel

我遇到Camel和JMS的问题。

我的路线非常简单:

  @Override
    protected RouteBuilder[] createRouteBuilders() throws Exception {
        createJmsComponent();

        RouteBuilder route1 = new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("direct:startpoint")
                .log("TO JMS: ${body}")
                .to("jms://queue:QUEUE.NAME.OUT?replyTo=QUEUE.NAME.IN&replyToType=Exclusive")
                ;
            }
        };

        RouteBuilder route2 = new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("jms://queue:QUEUE.NAME.IN?receiveTimeout=10000")
                .log("FROM JMS: ${body}")
                ;
            }
        };
        return new RouteBuilder[]{route1, route2};
    }

在日志中,我有我需要的东西:

2012-07-04 16:28:10,443 INFO  route1 - TO JMS: message
2012-07-04 16:28:13,477 INFO  route2 - FROM JMS: message

但问题是我有异常:

2012-07-04 16:28:33,960 ERROR o.a.c.p.DefaultErrorHandler                        - Failed delivery for (MessageId: ID-PLWROW912268-4634-1341412084839-0-1 on ExchangeId: ID-PLWROW912268-4634-1341412084839-0-2). Exhausted after delivery attempt: 1 caught: org.apache.camel.ExchangeTimedOutException: The OUT message was not received within: 20000 millis due reply message with correlationID: ID-PLWROW912268-4634-1341412084839-0-3 not received. Exchange[Message: message]
org.apache.camel.ExchangeTimedOutException: The OUT message was not received within: 20000 millis due reply message with correlationID: ID-PLWROW912268-4634-1341412084839-0-3 not received. Exchange[Message: message]
    at org.apache.camel.component.jms.reply.ReplyManagerSupport.processReply(ReplyManagerSupport.java:126) ~[camel-jms-2.9.2.jar:2.9.2]
    at org.apache.camel.component.jms.reply.TemporaryQueueReplyHandler.onTimeout(TemporaryQueueReplyHandler.java:61) [camel-jms-2.9.2.jar:2.9.2]
    at org.apache.camel.component.jms.reply.CorrelationTimeoutMap.onEviction(CorrelationTimeoutMap.java:53) [camel-jms-2.9.2.jar:2.9.2]
    at org.apache.camel.component.jms.reply.CorrelationTimeoutMap.onEviction(CorrelationTimeoutMap.java:30) [camel-jms-2.9.2.jar:2.9.2]
    at org.apache.camel.support.DefaultTimeoutMap.purge(DefaultTimeoutMap.java:203) [camel-core-2.9.2.jar:2.9.2]
    at org.apache.camel.support.DefaultTimeoutMap.run(DefaultTimeoutMap.java:159) [camel-core-2.9.2.jar:2.9.2]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441) [na:1.6.0_23]
    at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:317) [na:1.6.0_23]
    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:150) [na:1.6.0_23]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$101(ScheduledThreadPoolExecutor.java:98) [na:1.6.0_23]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.runPeriodic(ScheduledThreadPoolExecutor.java:180) [na:1.6.0_23]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:204) [na:1.6.0_23]
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [na:1.6.0_23]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [na:1.6.0_23]
    at java.lang.Thread.run(Thread.java:662) [na:1.6.0_23]

如果我理解正确,现在我有两条等待回应的路线。 Route2从队列中读取响应,route1没有得到响应(因为被路由2引用),这就是我得到异常的原因。

所以我需要做些什么来实现这个场景:

route1 - only send message to JMS and not waiting for response. 
route2 - only read message from JMS.

2 个答案:

答案 0 :(得分:5)

解决方案是使用inOnly模式和preserveMessageQos = true标志。

.inOnly("jms://queue:QUEUE.NAME.OUT" +
                            "?replyTo=QUEUE.NAME.IN" +
                            "&replyToType=Shared" +
                            "&preserveMessageQos=true")

答案 1 :(得分:0)

如果你只想让route1向route2正在侦听的队列发送消息,那么不要使用replyTo或单独的队列......只需这样做。

from("direct:startpoint").log("TO JMS: ${body}").to("jms://queue:QUEUE.NAME.IN");

from("jms://queue:QUEUE.NAME.IN").log("FROM JMS: ${body}");