如何解决线程“main”中的异常javax.naming.NameNotFoundException:未绑定?

问题描述 投票:0回答:1

我正在尝试在 Eclipse 中创建一个 jms 发布者/订阅者聊天应用程序。

import java.util.Properties;
    import javax.jms.JMSException;
    import javax.jms.Message;
    import javax.jms.MessageListener;
    import javax.jms.TextMessage;
    import javax.jms.Topic;
    import javax.jms.TopicConnection;
    import javax.jms.TopicConnectionFactory;
    import javax.jms.TopicSession;
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    public class TopicConsumer implements MessageListener {
        public static void main(String[] args) throws JMSException, NamingException {
            System.out.println(".....Entering JMS example TopicConsumer....");  
            Context context = TopicConsumer.getInitialContext();
            TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) context.lookup("ConnectionFactory");
            Topic topic = (Topic) context.lookup("topic/zaneacademy_jms_tutorial_01");
            TopicConnection topicConnection = topicConnectionFactory.createTopicConnection();
            TopicSession topicSession = topicConnection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
            topicSession.createSubscriber(topic).setMessageListener(new TopicConsumer());
            topicConnection.start();
            System.out.println("......Exiting JMS Example TopicConsumer.....");
            }
            public void onMessage(Message message) {
                try {
                    System.out.println("Incoming Messages: " + ((TextMessage)message).getText());
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
            public static Context getInitialContext() throws JMSException, NamingException {
                Properties props = new Properties();
                props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
                props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
                props.setProperty("java.naming.provider.url", "localhost:1099");
                Context context = new InitialContext(props);
                return context;
            }
        }

尝试运行该程序,我在控制台中收到以下错误

.....进入 JMS 示例 TopicConsumer...
线程“main”中的异常 javax.naming.NameNotFoundException:zaneacademy_jms_tutorial_01 未绑定
    在 org.jnp.server.NamingServer.getBinding(NamingServer.java:564)
    在 org.jnp.server.NamingServer.getBinding(NamingServer.java:572)
    在 org.jnp.server.NamingServer.getObject(NamingServer.java:578)
    在 org.jnp.server.NamingServer.lookup(NamingServer.java:317)
    在 org.jnp.server.NamingServer.lookup(NamingServer.java:291)
    在 sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)
    在 sun.reflect.NativeMethodAccessorImpl.invoke(来源未知)
    在 sun.reflect.DelegatingMethodAccessorImpl.invoke(来源未知)
    在 java.lang.reflect.Method.invoke(来源未知)
    在 sun.rmi.server.UnicastServerRef.dispatch(来源未知)
    在 sun.rmi.transport.Transport$1.run(来源不明)
    在 sun.rmi.transport.Transport$1.run(来源不明)
    在 java.security.AccessController.doPrivileged(本机方法)
    在 sun.rmi.transport.Transport.serviceCall(来源未知)
    在 sun.rmi.transport.tcp.TCPTransport.handleMessages(来源未知)
    在 sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(来源未知)
    在 sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(来源未知)
    在 java.util.concurrent.ThreadPoolExecutor.runWorker(来源未知)
    在 java.util.concurrent.ThreadPoolExecutor$Worker.run(来源未知)
    在 java.lang.Thread.run(来源未知)
    在 sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(来源未知)
    在 sun.rmi.transport.StreamRemoteCall.executeCall(来源未知)
    在 sun.rmi.server.UnicastRef.invoke(来源未知)
    在 java.rmi.server.RemoteObjectIncationHandler.invokeRemoteMethod(来源未知)
    在 java.rmi.server.RemoteObjectIncationHandler.invoke(来源未知)
    在 com.sun.proxy.$Proxy0.lookup(来源未知)
    在 org.jnp.interfaces.NamingContext.lookup(NamingContext.java:669)
    在 org.jnp.interfaces.NamingContext.lookup(NamingContext.java:629)
    在 javax.naming.InitialContext.lookup(来源未知)
    在 TopicConsumer.main(TopicConsumer.java:19)
java jboss5.x jms-topic
1个回答
0
投票

正如例外中所说,名称

zaneacademy_jms_tutorial_01
不受约束。 您需要配置它并将其分配给
topic/zaneacademy_jms_tutorial_01

我想说问题是你手动配置的

getInitialContext()
,而不是通过
new InitialContext();

返回可能已经配置的上下文
© www.soinside.com 2019 - 2024. All rights reserved.