java multi-threaded jms queue receiver

2019-04-02 15:26发布

问题:

I am using oracle Advanced Queuing (AQ JMS). I am trying to use multiple threads to consume jms queue using QueueReceiver.

Question: This model forks multiple threads, but performance is still close to single threaded model. How to increase queue consumption?

Following is code snipet:

final QueueReceiver queueReceiver = getQueueReceiver(queueSession);
        qConn.start();
        //THREAD POOL SIZE
        final int threadPoolSize = getThreadCount();


        final ExecutorService executor = Executors.newFixedThreadPool(threadPoolSize);

        for (int i = 0; i < threadPoolSize; i++) {
            executor.submit(new MessageWorker(queueReceiver));
        }

And message worker code looks like following:

    class MessageWorker extends Thread {
    MessageConsumer messageConsumer;

    public MessageWorker(MessageConsumer subs) {
        this.messageConsumer = subs;
    }

    /**
     * @see java.lang.Thread#run()
     */
    @Override
    public void run() {
        try {
            while (true) {
                try {
                    Message msg = messageConsumer.receive();
                    if (null != msg) {
                        //log message
                        logMessage(msg);
                        //handle message
                        handleMessage(msg);
                        //ack
                        msg.acknowledge();
                    }
                } catch (JMSException e) {
                    log.error(e);
                }
            }
        } catch (Exception e) {
            log.error(e);
        } finally {
            log.info("Thread:" + this.getName() + " terminated.");
        }
    }

回答1:

There should be a separate JMS Session for each Thread. It doesn't seem like there is a session created for each Thread in your code snippet.