0
我在寫一個發送消息的Qpid客戶端。我使用的示例代碼:無法實例化類:org.apache.qpid.jndi.PropertiesFileInitialContextFactory
private void runTest() throws Exception {
Properties properties = new Properties();
properties.load(this.getClass().getResourceAsStream("helloworld.properties"));
Context context = new InitialContext(properties);
ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup("qpidConnectionFactory");
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
Queue queue = (Queue) context.lookup("myqueue");
MessageConsumer messageConsumer = session.createConsumer(queue);
MessageProducer messageProducer = session.createProducer(queue);
TextMessage message = session.createTextMessage("Hello world!");
messageProducer.send(message);
session.commit();
message = (TextMessage)messageConsumer.receive();
session.commit();
System.out.println(message.getText());
connection.close();
context.close();
}
附帶的配置是:
java.naming.factory.initial = org.apache.qpid.jndi.PropertiesFileInitialContextFactory
connectionfactory.qpidConnectionFactory = amqp://guest:[email protected]/?brokerlist='tcp://localhost:61111'
queue.myqueue = queue1
,我得到的錯誤是:
Exception in thread "main" javax.naming.NoInitialContextException: Cannot instantiate class: org.apache.qpid.jndi.PropertiesFileInitialContextFactory [Root exception is java.lang.ClassNotFoundException: org.apache.qpid.jndi.PropertiesFileInitialContextFactory]
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:674)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
at javax.naming.InitialContext.init(InitialContext.java:244)
at javax.naming.InitialContext.<init>(InitialContext.java:216)
at Hello.runTest(Hello.java:19)
at Hello.main(Hello.java:13)
Caused by: java.lang.ClassNotFoundException: org.apache.qpid.jndi.PropertiesFileInitialContextFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:72)
at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:61)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:672)
... 5 more
看起來它是無法初始化屬性文件的上下文。
我有一個ActiveMQ實例在端口61111
上本地運行。屬性文件確實在我的CLASSPATH中。我還能在這裏錯過什麼?
你有lib文件夾中正確的所有必要的庫嗎? – Stultuske
我的IDE抱怨的唯一庫是'java.jmx。*'。所以我加了他們。 –
你使用的是什麼Qpid JMS客戶端,這似乎是一個較老的客戶端,而不是最新的官方Qpid JMS客戶端:http://qpid.apache.org/components/jms/index.html –