0
votes

Let say we need to wait for any new messages available in the queue and process them imediatly. There is no requirenments related to transactional delivery.

While JmsTemplate#receive is quite handy, it is not quite clear for me if it is good in performance point of view; it appeared to be quite slow to be honest.

My guess is that it is slow because same initialization stuff is executed for each call. So I've made it next way, reusing connectin, destination and consumer which appeared to be much faster:

private void startAsyncReceiver(final BlockingQueue<String> localQueue, final String remoteQueueName) {
    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
            jmsTemplate.execute(new SessionCallback<Object>() {
                @Override
                public Object doInJms(Session session) throws JMSException {
                    try {
                        Destination destination = destinationResolver.resolveDestinationName(session, remoteQueueName, false);
                        MessageConsumer consumer = session.createConsumer(destination);
                        sessionsStartedLatch.countDown();
                        while (true) {
                            Message message = consumer.receive(MAX_VALUE);
                            String body = (String) jmsTemplate.getMessageConverter().fromMessage(message);
                            localQueue.put(body);
                        }
                    } catch (InterruptedException e) {
                        return null;
                    }
                }
            }, true);
        }
    }, remoteQueueName + "-Receiver");
    t.setDaemon(true);
    t.start();
}

Q1: Any idea why JmsTemplate#receive showed worse performabce result for the same algorithm? Am I missing something?
Q2: Can you see any potential issues with current implementation?

1

1 Answers

2
votes

The template has to create a consumer for each receive.

If you use a caching connection factory with cache consumers set to true it should improve the JmsTemplate performance significantly, even approaching your code.

But your implementation is fine.