我正在使用以下代码创建多个JMS会话,以供多个使用者使用消息。我的问题是代码以单线程方式运行。即使消息存在于队列中,第二个线程也无法接收任何内容,而是继续轮询。同时,第一个线程完成对第一批的处理,然后返回并使用剩余的消息。这里的用法有什么问题吗?
static {
try {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://172.16.143.99:61616");
connection = connectionFactory.createConnection();
connection.start();
} catch (JMSException e) {
LOGGER.error("Unable to initialise JMS Queue.", e);
}
}
public JMSClientReader(boolean isQueue, String name) throws QueueException {
init(isQueue,name);
}
@Override
public void init(boolean isQueue, String name) throws QueueException
{
// Create a Connection
try {
// Create a Session
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
if (isQueue) {
destination = new ActiveMQQueue(name);// session.createQueue("queue");
} else {
destination = new ActiveMQTopic(name);// session.createTopic("topic");
}
consumer = session.createConsumer(destination);
} catch (JMSException e) {
LOGGER.error("Unable to initialise JMS Queue.", e);
throw new QueueException(e);
}
}
public String readQueue() throws QueueException {
// connection.setExceptionListener(this);
// Wait for a message
String text = null;
Message message;
try {
message = consumer.receive(1000);
if(message==null)
return "done";
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
text = textMessage.getText();
LOGGER.info("Received: " + text);
} else {
throw new JMSException("Invalid message found");
}
} catch (JMSException e) {
LOGGER.error("Unable to read message from Queue", e);
throw new QueueException(e);
}
LOGGER.info("Message read is " + text);
return text;
}