如何订阅现有的咨询主题?

时间:2013-06-16 08:59:02

标签: jms activemq jndi

I have activemq5.3.2 running and I wanted to subscribe existing advisory topics using my java program. while, `jndi` lookup I am getting following error:
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: 



java.naming.factory.initial
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:657)
    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:259)
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:296)
    at javax.naming.InitialContext.lookup(InitialContext.java:363)
    at jmsclient.Consumer.<init>(Consumer.java:38)
    at jmsclient.Consumer.main(Consumer.java:74)
Exception occurred: javax.jms.InvalidDestinationException: Don't understand null destinations

请说明问题出在哪里,或者我如何使用我的主题名称来查找?

package jmsclient;

import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.activemq.ActiveMQConnectionFactory;

public class Consumer implements MessageListener {
    private static int ackMode;
    private static String clientTopicName;

    private boolean transacted = false;
    //private MessageConsumer messageConsumer;
    static {
        clientTopicName = "ActiveMQ.Advisory.Consumer.Queue.example.A";
        ackMode = Session.AUTO_ACKNOWLEDGE;
    }
    @SuppressWarnings("null")
    public Consumer()
    {// TODO Auto-generated method stub
        TextMessage message = null;
        Context jndiContext;
        //TopicConnectionFactory topicConnectionFactory = null;
        TopicConnection topicConnection = null;
        TopicSession topicSession = null;
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://usaxwas012ccxra.ccmp.ibm.lab:61616");

        try{
            Topic myTopic = null;
            try {   jndiContext = new InitialContext();
                    myTopic = (Topic) jndiContext.lookup(clientTopicName);
            } catch (NamingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            topicConnection = connectionFactory.createTopicConnection();
            topicConnection.start();
            topicSession = topicConnection.createTopicSession(transacted, ackMode);
            TopicSubscriber topicSubscriber = topicSession.createSubscriber(myTopic);

            Message m = topicSubscriber.receive(1000);

            if (m != null) {
            if (m instanceof TextMessage) {
            message = (TextMessage) m;
            System.out.println("Reading message: " + message.getText());
            }
            }
            } //try ends

            catch (JMSException e) {
            System.out.println("Exception occurred: " + e.toString());
            } finally {
                if (topicConnection != null) {
                    try {
                            topicConnection.close();
                        } catch (JMSException e) {}
                }}}    
public void onMessage(Message arg0) {
    // TODO Auto-generated method stub
}   

public static void main(String[] args) {
 new Consumer();
}
}

0 个答案:

没有答案