You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@activemq.apache.org by czy11421 <cz...@gmail.com> on 2009/09/13 00:53:16 UTC

error in subscribing topic

I am running ActiveMQ 5, in the admin web page, I could see 
"STOCKS.SUNW" in Topics, and this topic is sending out message, then how 
could I subscribe this topic and get the published message ?

I have tried this coding, but I get the error as bottom. Where is the bug ?

Thanks.

//////////////////////// code /////////////////
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");
             // create a new TopicConnection for pub/sub messaging
       javax.jms.TopicConnection conn = 
factory.createTopicConnection();  //getTopicConnection();
             System.out.println(conn); // output this : 
ActiveMQConnection 
{id=ID:xxxx-PC-51013-1252720683131-0:0,clientId=null,started=false}     
             // lookup an existing topic
       javax.jms.Topic mytopic = (javax.jms.Topic) 
ctx.lookup("STOCKS.SUNW"); //error is from here
             // create a new TopicSession for the client
       javax.jms.TopicSession session = conn.createTopicSession(false, 
TopicSession.AUTO_ACKNOWLEDGE);
             // create a new subscriber to receive messages
       javax.jms.TopicSubscriber subscriber = 
session.createSubscriber(mytopic);
       System.out.println(subscriber.receive());

////////////////////////// Exception ///////////////////
Exception in thread "main" javax.naming.NameNotFoundException: STOCKS.SUNW
   at 
org.apache.activemq.jndi.ReadOnlyContext.lookup(ReadOnlyContext.java:225)
   at javax.naming.InitialContext.lookup(Unknown Source)
   at com.Test.main(Test.java:31)


Re: error in subscribing topic

Posted by czy11421 <cz...@gmail.com>.
Rob Davies wrote:
>
> 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/
>
>
>
>
>
>
Hi, Rob,
Oh,yes, at least I need to keep main thread alive.I fix it, it works 
now. Learned a lot from you. Thanks.

Edward.

Re: error in subscribing topic

Posted by Rob Davies <ra...@gmail.com>.
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/






Re: error in subscribing topic

Posted by czy11421 <cz...@gmail.com>.
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();
        }
    }
}
/////////////////////////

Re: error in subscribing topic

Posted by Rob Davies <ra...@gmail.com>.
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/






Re: error in subscribing topic

Posted by czy11421 <cz...@gmail.com>.
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

Re: error in subscribing topic

Posted by Rob Davies <ra...@gmail.com>.
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/





Re: error in subscribing topic

Posted by czy11421 <cz...@gmail.com>.
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();
            }
        }
    }
//-----------------------------------

Re: error in subscribing topic

Posted by Rob Davies <ra...@gmail.com>.
On 13 Sep 2009, at 17:10, czy11421 wrote:

> Rob Davies wrote:
>>
>> On 12 Sep 2009, at 23:53, czy11421 wrote:
>>
>>> I am running ActiveMQ 5, in the admin web page, I could see  
>>> "STOCKS.SUNW" in Topics, and this topic is sending out message,  
>>> then how could I subscribe this topic and get the published  
>>> message ?
>>>
>>> I have tried this coding, but I get the error as bottom. Where is  
>>> the bug ?
>>>
>>> Thanks.
>>>
>>> //////////////////////// code /////////////////
>>> 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");
>>>           // create a new TopicConnection for pub/sub messaging
>>>     javax.jms.TopicConnection conn =  
>>> factory.createTopicConnection();  //getTopicConnection();
>>>           System.out.println(conn); // output this :  
>>> ActiveMQConnection {id=ID:xxxx- 
>>> PC 
>>> -51013 
>>> -1252720683131-0:0,clientId=null,started=false}                 //  
>>> lookup an existing topic
>>>     javax.jms.Topic mytopic = (javax.jms.Topic)  
>>> ctx.lookup("STOCKS.SUNW"); //error is from here
>>>           // create a new TopicSession for the client
>>>     javax.jms.TopicSession session =  
>>> conn.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
>>>           // create a new subscriber to receive messages
>>>     javax.jms.TopicSubscriber subscriber =  
>>> session.createSubscriber(mytopic);
>>>     System.out.println(subscriber.receive());
>>>
>>> ////////////////////////// Exception ///////////////////
>>> Exception in thread "main" javax.naming.NameNotFoundException:  
>>> STOCKS.SUNW
>>> at  
>>> org 
>>> .apache.activemq.jndi.ReadOnlyContext.lookup(ReadOnlyContext.java: 
>>> 225)
>>> at javax.naming.InitialContext.lookup(Unknown Source)
>>> at com.Test.main(Test.java:31)
>>>
>>
>>
>> The ActiveMQ JNDI Context is local - in VM only. It doesn't  
>> communicate with the ActiveMQ Broker - but it does follow some  
>> conventions to allow easy "standard" ways of finding ActiveMQ  
>> administered objects (Connections, Topics, Queues etc). For example  
>> - it will create a ConnectionFactory because you've looked up in  
>> the Context for a "ConnectionFactory". It will also create a Queue  
>> or a Topic for you - if the Object you are looking up has a Queue  
>> or a Topic if the name you are looking up starts with "queue." or  
>> "topic.". Which isn't going to be of any use to you - as you want  
>> to subscribe to a Topic "STOCKS.SUNW".
>>
>> However, all destinations by default are dynamic - so you just have  
>> to change:
>> javax.jms.Topic mytopic = (javax.jms.Topic)  
>> ctx.lookup("STOCKS.SUNW"); //error is from here
>> for
>> javax.jms.Topic mytopic = session.createTopic("STOCKS.SUNW");
>>
>> Something else you need to do - is call connection.start() - to  
>> start receiving messages.
>>
>> cheers,
>>
>> Rob
>>
>> Rob Davies
>> I work here: http://fusesource.com
>> My Blog: http://rajdavies.blogspot.com/
>> I'm writing this: http://www.manning.com/snyder/
>>
>>
>>
>>
>>
> Thanks, your solution works. From you text, the context is local, so  
> it doesn't have topic objects located in MQ server, so we have to  
> create session and use session to get Topic object. My understanding  
> is correct ?
Yes - that's correct
>
> The session.createTopic("xxx") gave me some confusion, it should be  
> like session.locateTopic("xxx"); it is not a CREAT-new-topic, it is  
> a LOCATE-an-existing-topic.
Yes - though in ActiveMQ - a Destination (Topic or Queue) is a String  
- which is used on the broker as key to look up the physical  
destination held by the broker - and create it if it doesn't exist. So  
in reality - you are just creating a key.
>
> //======
> It will also create a Queue or a Topic for you - if the Object you  
> are looking up has a Queue or a Topic if the name you are looking up  
> starts with "queue." or "topic.". Which isn't going to be of any use  
> to you - as you want to subscribe to a Topic "STOCKS.SUNW".
> //===========
> I don't undestand this, are you saying, if I have a topic named as  
> "topic.MyTopic", then I can use ctx.lookup("topic.MyTopic") ?
Yes - correct
>
> Thanks.
> Edward


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/



Re: error in subscribing topic

Posted by czy11421 <cz...@gmail.com>.
Rob Davies wrote:
>
> On 12 Sep 2009, at 23:53, czy11421 wrote:
>
>> I am running ActiveMQ 5, in the admin web page, I could see 
>> "STOCKS.SUNW" in Topics, and this topic is sending out message, then 
>> how could I subscribe this topic and get the published message ?
>>
>> I have tried this coding, but I get the error as bottom. Where is the 
>> bug ?
>>
>> Thanks.
>>
>> //////////////////////// code /////////////////
>> 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");
>>            // create a new TopicConnection for pub/sub messaging
>>      javax.jms.TopicConnection conn = 
>> factory.createTopicConnection();  //getTopicConnection();
>>            System.out.println(conn); // output this : 
>> ActiveMQConnection 
>> {id=ID:xxxx-PC-51013-1252720683131-0:0,clientId=null,started=false}                 
>> // lookup an existing topic
>>      javax.jms.Topic mytopic = (javax.jms.Topic) 
>> ctx.lookup("STOCKS.SUNW"); //error is from here
>>            // create a new TopicSession for the client
>>      javax.jms.TopicSession session = conn.createTopicSession(false, 
>> TopicSession.AUTO_ACKNOWLEDGE);
>>            // create a new subscriber to receive messages
>>      javax.jms.TopicSubscriber subscriber = 
>> session.createSubscriber(mytopic);
>>      System.out.println(subscriber.receive());
>>
>> ////////////////////////// Exception ///////////////////
>> Exception in thread "main" javax.naming.NameNotFoundException: 
>> STOCKS.SUNW
>>  at 
>> org.apache.activemq.jndi.ReadOnlyContext.lookup(ReadOnlyContext.java:225) 
>>
>>  at javax.naming.InitialContext.lookup(Unknown Source)
>>  at com.Test.main(Test.java:31)
>>
>
>
> The ActiveMQ JNDI Context is local - in VM only. It doesn't 
> communicate with the ActiveMQ Broker - but it does follow some 
> conventions to allow easy "standard" ways of finding ActiveMQ 
> administered objects (Connections, Topics, Queues etc). For example - 
> it will create a ConnectionFactory because you've looked up in the 
> Context for a "ConnectionFactory". It will also create a Queue or a 
> Topic for you - if the Object you are looking up has a Queue or a 
> Topic if the name you are looking up starts with "queue." or "topic.". 
> Which isn't going to be of any use to you - as you want to subscribe 
> to a Topic "STOCKS.SUNW".
>
> However, all destinations by default are dynamic - so you just have to 
> change:
> javax.jms.Topic mytopic = (javax.jms.Topic) ctx.lookup("STOCKS.SUNW"); 
> //error is from here
> for
> javax.jms.Topic mytopic = session.createTopic("STOCKS.SUNW");
>
> Something else you need to do - is call connection.start() - to start 
> receiving messages.
>
> cheers,
>
> Rob
>
> Rob Davies
> I work here: http://fusesource.com
> My Blog: http://rajdavies.blogspot.com/
> I'm writing this: http://www.manning.com/snyder/
>
>
>
>
>
Thanks, your solution works. From you text, the context is local, so it 
doesn't have topic objects located in MQ server, so we have to create 
session and use session to get Topic object. My understanding is correct ?

The session.createTopic("xxx") gave me some confusion, it should be like 
session.locateTopic("xxx"); it is not a CREAT-new-topic, it is a 
LOCATE-an-existing-topic.

//======
It will also create a Queue or a Topic for you - if the Object you are 
looking up has a Queue or a Topic if the name you are looking up starts 
with "queue." or "topic.". Which isn't going to be of any use to you - 
as you want to subscribe to a Topic "STOCKS.SUNW".
//===========
I don't undestand this, are you saying, if I have a topic named as 
"topic.MyTopic", then I can use ctx.lookup("topic.MyTopic") ?

Thanks.
Edward

Re: error in subscribing topic

Posted by Rob Davies <ra...@gmail.com>.
On 12 Sep 2009, at 23:53, czy11421 wrote:

> I am running ActiveMQ 5, in the admin web page, I could see  
> "STOCKS.SUNW" in Topics, and this topic is sending out message, then  
> how could I subscribe this topic and get the published message ?
>
> I have tried this coding, but I get the error as bottom. Where is  
> the bug ?
>
> Thanks.
>
> //////////////////////// code /////////////////
> 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");
>            // create a new TopicConnection for pub/sub messaging
>      javax.jms.TopicConnection conn =  
> factory.createTopicConnection();  //getTopicConnection();
>            System.out.println(conn); // output this :  
> ActiveMQConnection {id=ID:xxxx- 
> PC 
> -51013 
> -1252720683131-0:0,clientId=null,started=false}                 //  
> lookup an existing topic
>      javax.jms.Topic mytopic = (javax.jms.Topic)  
> ctx.lookup("STOCKS.SUNW"); //error is from here
>            // create a new TopicSession for the client
>      javax.jms.TopicSession session = conn.createTopicSession(false,  
> TopicSession.AUTO_ACKNOWLEDGE);
>            // create a new subscriber to receive messages
>      javax.jms.TopicSubscriber subscriber =  
> session.createSubscriber(mytopic);
>      System.out.println(subscriber.receive());
>
> ////////////////////////// Exception ///////////////////
> Exception in thread "main" javax.naming.NameNotFoundException:  
> STOCKS.SUNW
>  at  
> org.apache.activemq.jndi.ReadOnlyContext.lookup(ReadOnlyContext.java: 
> 225)
>  at javax.naming.InitialContext.lookup(Unknown Source)
>  at com.Test.main(Test.java:31)
>


The ActiveMQ JNDI Context is local - in VM only. It doesn't  
communicate with the ActiveMQ Broker - but it does follow some  
conventions to allow easy "standard" ways of finding ActiveMQ  
administered objects (Connections, Topics, Queues etc). For example -  
it will create a ConnectionFactory because you've looked up in the  
Context for a "ConnectionFactory". It will also create a Queue or a  
Topic for you - if the Object you are looking up has a Queue or a  
Topic if the name you are looking up starts with "queue." or "topic.".  
Which isn't going to be of any use to you - as you want to subscribe  
to a Topic "STOCKS.SUNW".

However, all destinations by default are dynamic - so you just have to  
change:
javax.jms.Topic mytopic = (javax.jms.Topic)  
ctx.lookup("STOCKS.SUNW"); //error is from here
for
javax.jms.Topic mytopic = session.createTopic("STOCKS.SUNW");

Something else you need to do - is call connection.start() - to start  
receiving messages.

cheers,

Rob

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