java - setMessageListener jms method in Websphere Application server generates javax.jms.IllegalStateException: Method setMessageListener not permitted -


i using websphere application server 7.x version.

i want read messages buffered in mq of jms api given below. referred 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 run time getting exception:

javax.jms.illegalstateexception: method setmessagelistener not permitted 

i see in link method setmessagelistener asynchronous messaging, forbidden use method in websphere application server.

question 1:

can't setmessagelistener method used in was?
if not, work around achieve above required functionality?

question 2:

i can explain need in other words:
jms listner should not recive messages message arrived in the queue. want listener listen during time require.
tried approach mentioned above. got blocked because of exception.

any other way achieve this?

this not websphere problem.
in short, jms spec not allow use feature in jee container: check api here :

void setmessagelistener(messagelistener listener)              throws jmsexception     sets messageconsumer's messagelistener.    {...}    method must not used in java ee web or ejb application. doing may cause jmsexception thrown though not guaranteed. 

Comments