简体   繁体   中英

How can I implement JMS MessageListener?

I tried to implement MessageListener, but it dosen't work, but when I use MessageListner, it doesn't work. So what should I do that MessageListener starts working, I'm using jboss 4.02.

recv.receive(); // This works

recv.setMessageListener(new ExListener()); // This doesn't work

    public static class ExListener 
    implements MessageListener
    {
        public void onMessage(Message msg)
        {
            TextMessage tm = (TextMessage) msg;
            try {
                System.out.println("onMessage, recv text="+tm.getText());
            } catch(Throwable t) {
                t.printStackTrace();
            }
        }
    }

Did you remember to call connection.start()? I guess you must have had that for the receive() method to work, but perhaps you accidentally deleted it when you changed the code.

这可以通过使用消息驱动的bean在Jboss服务器上实现,这可能是最好的方法。当然,更轻量级的选项是在Spring中通过实现MessageListener创建一个jmsListener http://bit.ly/epVSmS

The problem could be that the class that registers the listener has ended and the listener object goes out of existence. You should keep the object live and running to receive the message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM