activemq-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Rob Davies <rajdav...@gmail.com>
Subject Re: error in subscribing topic
Date Sun, 13 Sep 2009 18:29:54 GMT

On 13 Sep 2009, at 17:55, czy11421 wrote:

> Rob,
> Thanks for your reply.
>
> Here is another question. If I use MessageListener, how could I  
> start to receive message ? Coding as below, the Listener will NOT  
> output message. Did I miss something ?
>
> Thanks.
> Edward
>
> //---------------------------------
> session = conn.createTopicSession(false,  
> TopicSession.AUTO_ACKNOWLEDGE);
>           javax.jms.Topic mytopic =  
> session.createTopic("STOCKS.SUNW");
>
>           Test2.MyListener listener = new Test2.MyListener();
>
>           javax.jms.TopicSubscriber subscriber =  
> session.createSubscriber(mytopic);
>                     /**  == works
>           while(true){
>               Message message = subscriber.receive();
>               TextMessage text = (TextMessage) message;
>               System.out.println(text.getText());
>               Thread.sleep(1000);
>           }
>           **/
>
>           subscriber.setMessageListener(listener);
> //-------------------------------------------
>
> //-----------------------------------
> static class MyListener implements MessageListener {
>       public void onMessage(Message message) {
>           System.out.println("Message: ");
>           TextMessage text = (TextMessage) message;
>           try {
>               System.out.println("Message: " + text.getText());
>           } catch (Exception e) {
>               e.printStackTrace();
>           }
>       }
>   }
> //-----------------------------------
You shouldn't set the listener on the same subscriber after calling  
receive() - its best to create a new session for each new subscriber

cheers,

Rob

Rob Davies
twitter.com/rajdavies
I work here: http://fusesource.com
My Blog: http://rajdavies.blogspot.com/
I'm writing this: http://www.manning.com/snyder/





Mime
View raw message