java多线程jms队列接收器

时间:2013-01-30 20:08:10

标签: java multithreading jms

我使用的是oracle Advanced Queuing(AQ JMS)。我正在尝试使用多个线程来使用QueueReceiver来使用jms队列。

问题:此模型分叉多个线程,但性能仍接近单线程模型。如何增加队列消耗?

以下是代码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));
        }

消息工作者代码如下所示:

    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 个答案:

答案 0 :(得分:0)

每个线程都应该有一个单独的JMS会话。在您的代码段中似​​乎没有为每个线程创建会话。