Reputation: 3156
I am using ActiveMQ 5.6. I have two consumer. When one consumer receive message on onmessage method, other consumer is not receiving message. means I want 2nd consumer to be able to receive second message during computation of first one. I tried many thing but nothing is working..
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
connectionFactory.setAlwaysSessionAsync(true);
connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Session session1 = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destinationQueue1 = session.createQueue(dataQ);
Destination destinationQueue2 = session1.createQueue(dataQ);
NormalListener listener = new NormalListener();
NormalListener listener2 = new NormalListener();
MessageConsumer consumerData1 = session.createConsumer(destinationQueue1);
consumerData1.setMessageListener(listner1);
MessageConsumer consumerData2 = session1.createConsumer(destinationQueue2);
consumerData2.setMessageListener(listener2);
public class NormalListener implements MessageListener{
@Override
public void onMessage(Message message) {
try {
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
String receivedMessage=textMessage.getText();
readMessage(message);
}
}
}
} catch (JMSException e) {
e.printStackTrace();
}
}
}
Upvotes: 1
Views: 958
Reputation: 36634
The code creates two sessions - session and session1 - and only uses the first one (session) to create the consumers.
Try to create the first consumer using the first session, and the second consumer using the second session. I also would use consistent naming, using session1 and session2:
MessageConsumer consumerData1 = session1.createConsumer(destinationQueue1);
consumerData1.setMessageListener(listner1);
MessageConsumer consumerData2 = session2.createConsumer(destinationQueue2);
consumerData2.setMessageListener(listener2);
NormalListener is not a standard JMS class / interface. Can you try a normal MessageListener instead?
Also you are not showing the code within the onMessage() method of the message listeners. Did you check it for potential threading issues, and reduce their code to a minimum for testing?
See also: http://activemq.apache.org/multiple-consumers-on-a-queue.html
Upvotes: 1