I am using Websphere Application Server 7.x version.
I want to read the messages buffered in MQ with the help of JMS API as given below. I referred from link1, link2
@Resource(lookup = "jms/ConnectionFactory")
private static ConnectionFactory connectionFactory;
@Resource(lookup = "jms/Queue")
private static Queue queue;
public void readMessagesFromQueue() {
try {
Connection connection = connectionFactory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = session.createConsumer(queue);
MyListener myListener = new MyListener();
consumer.setMessageListener(myListener); // Error here
connection.start();
} catch (JMSException e) {
throw new RuntimeException(e);
}
}
MyListener
class:
public class MyListener implements MessageListener {
@Override
public void onMessage(Message message) {
try {
String text=((TextMessage)message).getText();
System.out.println(text);
}
catch (JMSException e) {
throw new RuntimeException(e);
}
}
}
During the run time I am getting Exception:
javax.jms.IllegalStateException: Method setMessageListener not permitted
I could see in the link that the method setMessageListener
for asynchronous messaging, it is forbidden to use this method in a WebSphere Application Server.
Question 1:
Can't this setMessageListener
method be used in WAS?
If not, what is the work around to achieve the above required functionality?
Question 2:
I can explain the need in other words:
The JMS listner should not recive the messages as soon as the message arrived in the the queue. But I want the listener to listen only during the time I require.
I tried the approach which I mentioned above. But I got blocked because of the exception.
Any other way to achieve this?
consumer.receive()
method to read messages. – Gas