Activemq concurrency fail in Apache camel route

时间:2015-11-24 07:26:18

标签: concurrency apache-camel jms activemq apache-servicemix

Trying to send multiple requests at same instant to camel activemq route, one request is serviced and the other request is not serviced and sent back as it is. The Jms messages are set with JMScorrelationId too before sending like below

textMessage.setJMSCorrelationID(UUID.randomUUID().toString());

below is my activemq route

from("activemq:queue:TEST_QUEUE?disableReplyTo=true")
                .setExchangePattern(ExchangePattern.InOut)
                .process(new Processor() {
                    public void process(Exchange e) throws Exception {
                        log.info("Request : "
                                + MessageHelper.extractBodyAsString(e.getIn()));
                        /*Processing Logic*/
                    }
                })
                .beanRef("testBean","postDetails")
                .inOnly("activemq:queue:TEST_QUEUE");

Multiple (Test for 2 requests) requests sent to the above route concurrently not serviced except one. The servicemix.log shows all recieved requests. But only one is serviced.

Below is the code what is sending request deployed in jboss 6.1 as part of web application.

public Message receive(String message, String queueName) {
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
                "tcp://localhost:61616");
        String userName = "smx";
        String password = "smx";
        Connection connection;
        Message response =null;
        try {
            connection = connectionFactory.createConnection(userName, password);
            connection.start();
            ((ActiveMQConnectionFactory) connectionFactory)
                    .setDispatchAsync(false);
            Session session = connection.createSession(false,
                    Session.AUTO_ACKNOWLEDGE);
            Queue destination = session.createQueue(queueName);
            MessageProducer producer = session.createProducer(destination);
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            TextMessage textMessage = session.createTextMessage(message);
            Queue tempQueue = session.createQueue(queueName);
            textMessage.setJMSReplyTo(tempQueue);
            producer.send(textMessage);
            MessageConsumer consumer = session.createConsumer(tempQueue);
            response = consumer.receive();
            response.acknowledge();

            session.close();
            connection.close();
        } catch (JMSException e) {
            e.printStackTrace();
        }
        return response;
    }

Is there some or the other parameter im missing?? please suggest.

1 个答案:

答案 0 :(得分:1)

如果JMS消息有一个JMSReplyTo标题,Camel会自动发回一个回复,所以你的路由应该是

from("activemq:queue:TEST_QUEUE")
                .process(new Processor() {
                    public void process(Exchange e) throws Exception {
                        log.info("Request : "
                                + MessageHelper.extractBodyAsString(e.getIn()));
                        /*Processing Logic*/
                    }
                })
                .beanRef("testBean","postDetails");

在路由的末尾(例如,在调用testBean之后),然后将消息体的内容用作回复消息,将其发送回JMSReplyTo头中定义的名为的队列。