我正在使用 oracle 高级队列 (AQ JMS)。我正在尝试使用多个线程来使用 QueueReceiver 来消耗 jms 队列。
问题:该模型分叉了多个线程,但性能仍接近单线程模型。如何增加队列消耗?
以下是代码片段:
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.");
}
}