0
votes

I have Camel route which listens for incoming JMS messages. For some reason, the route doesn't send response to the JMS replyTo address. In the following example, the program hangs waiting for the reply message to be arrived at temporary destination. How do I make the JMS-listening route to handle the messages InOut-fashion?

@Component("testRouteBuilder")
public class TestRouteBuilder extends RouteBuilder {

    @SuppressWarnings("unchecked")
    @Override
    public void configure() throws Exception {

        from("timer://foo?delay=2000")
            .setBody(simple("hello"))
            .log("request: ${body}")
            .to("bean://jmsbean")
            .log("reply: ${body}");

        from("jms://queue:dest")
//          .setExchangePattern(ExchangePattern.InOut) // Not working
            .log("got message")
            .log("${headers}")
            .setBody(constant("reply"));
    }

    @Component("jmsbean")
    public static class JmsBean {

        @Autowired
        ConnectionFactory jmsServer1;

        @Autowired
        ConnectionFactory jmsServer2;

        public String testJms(@Body String body) throws JMSException {

            Connection conn = jmsServer1.createConnection();
            conn.start();
            Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

            Connection conn2 = jmsServer2.createConnection();
            conn2.start();
            Session session2 = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);

            TemporaryQueue tempQueue = session2.createTemporaryQueue();

            TextMessage message = session.createTextMessage();
            message.setJMSCorrelationID("tuomas");
            message.setJMSReplyTo(tempQueue);
            message.setJMSMessageID("tuomas");

            Queue dest = session.createQueue("dest");
            MessageProducer producer = session.createProducer(dest);
            producer.send(message);

            session.close();
            conn.close();

            MessageConsumer consumer = session2.createConsumer(tempQueue, "tuomas");
            Message reply = consumer.receive();

            session2.close();
            conn2.close();

            return reply.getBody(String.class);
        }
    }
}

.

2018-04-06 18:24:31.497  INFO 15256 --- [           main] t.springcamel.SpringcamelApplication     : Started SpringcamelApplication in 6.143 seconds (JVM running for 11.263)
2018-04-06 18:24:33.456  INFO 15256 --- [2 - timer://foo] route1                                   : request: hello
2018-04-06 18:24:33.503  INFO 15256 --- [sConsumer[dest]] route2                                   : got message
2018-04-06 18:24:33.503  INFO 15256 --- [sConsumer[dest]] route2                                   : {breadcrumbId=ID-DESKTOP-LI5P50P-1523028268251-0-2, JMSCorrelationID=tuomas, JMSCorrelationIDAsBytes=tuomas, JMSDeliveryMode=2, JMSDestination=queue://dest, JMSExpiration=0, JMSMessageID=ID:DESKTOP-LI5P50P-51501-1523028271227-4:2:1:1:1, JMSPriority=4, JMSRedelivered=false, JMSReplyTo=temp-queue://ID:DESKTOP-LI5P50P-51501-1523028271227-4:3:1, JMSTimestamp=1523028273488, JMSType=null, JMSXGroupID=null, JMSXUserID=null}
1
I don't see anywhere in the code that you are calling the route from("jms://queue:dest"). How are you calling this route?Souciance Eqdam Rashti
You should not have to write JMS specific code when using Camel. The JMS/ActiveMQ/RabbitMQ components take care of that for you.Ralf
Also, you have to set the exchange pattern to InOut in the route that sends the message. Not the receiving route.Ralf

1 Answers

0
votes

AFAIK, Camel by default sends a message to replyTo queue. It is stated so in JMS component docs within an attribute disableReplyTo. I have experienced such behaviour by myself.

However, your message is not consumed by any consumer in your example. For example, you can try to send it to some bean.