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 Mon, 14 Sep 2009 06:19:08 GMT

On 14 Sep 2009, at 00:00, czy11421 wrote:

> Rob Davies wrote:
>>
>> On 13 Sep 2009, at 20:03, czy11421 wrote:
>>
>>> Rob Davies wrote:
>>>>
>>>> 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/
>>>>
>>>>
>>>>
>>>>
>>>>
>>> Rob,
>>> Thanks. I did not "set the listener on the same subscriber after  
>>> calling receive()", as you see, the coding has been commented out.
>>>
>>> If I use while(true){...}, it will work, but I switch to  
>>> MessageListener, it can't output received message .
>>>
>>> Thanks.
>>> Edward
>>
>> Hi Edward,
>>
>> thats very strange - and certainly not normal behaviour - could you  
>> send a test case - to replicate what you are doing ?
>>
>> cheers,
>>
>> Rob
>>
>> Rob Davies
>> http://twitter.com/rajdavies
>> I work here: http://fusesource.com
>> My Blog: http://rajdavies.blogspot.com/
>> I'm writing this: http://www.manning.com/snyder/
>>
>>
>>
>>
>>
>>
> Hi, Rob,
>
> The coding is below. The topic is using Market Data demo shipped  
> with ActiveMQ.
>
> Thanks.
> Edward
>
> ///////////////////////
> package com;
>
> import java.util.Properties;
>
> import javax.jms.Message;
> import javax.jms.MessageListener;
> import javax.jms.TextMessage;
> import javax.jms.TopicSession;
> import javax.naming.Context;
> import javax.naming.InitialContext;
>
> public class Test2 {
>   static javax.jms.TopicConnection conn;
>   static javax.jms.TopicSession session;
>
>   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();
>           }
>       }
>   }
>
>   public static void main(String[] aaa) throws Exception {
>       try {
>           Properties props = new Properties();
>           props.setProperty(Context.INITIAL_CONTEXT_FACTORY,  
> "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
>           props.setProperty(Context.PROVIDER_URL, "tcp://localhost: 
> 61616");
>           javax.naming.Context ctx = new InitialContext(props);
>
>           // lookup the connection factory
>           javax.jms.TopicConnectionFactory factory =  
> (javax.jms.TopicConnectionFactory) ctx.lookup("ConnectionFactory");
>
>           conn = factory.createTopicConnection();
>           System.out.println(conn);
>                     conn.start();
>
>           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);
>       } catch (Exception e) {
>           e.printStackTrace();
>       } finally {
>           session.close();
>           conn.close();
>       }
>   }
> }
> /////////////////////////


Hi Edward,

you need to put a long sleep at the end of the main method - as all  
the threads are daemon threads - there has to be a least one  
application thread running

cheers,

Rob
Rob Davies
http://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