将 HornetQ 作为独立服务器运行示例
目前我正在努力学习HornetQ。从理论上讲,事情似乎很简单,但即使是最基本的示例,我也很难运行。
到目前为止,我已经尝试将 HornetQ 作为独立服务器启动,并连接一个发送消息然后接收返回消息的简单客户端。
我遵循的步骤是: (根据 HornetQ 文档 http://hornetq .sourceforge.net/docs/hornetq-2.0.0.GA/user-manual/en/html/using-jms.html)
- 下载最新版本的HornetQ (2.2.5)并提取它。 -修改INSTALL_DIRECTORY\config\stand-alone\non-clustered\hornetq-jms.xml文件来创建我需要的对象,以下是内容:
<configuration xmlns="urn:hornetq"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:hornetq ../schemas/hornetq-jms.xsd ">
<connection-factory name="ConnectionFactory">
<connectors>
<connector-ref connector-name="netty"/>
</connectors>
<entries>
<entry name="ConnectionFactory"/>
</entries>
</connection-factory>
<queue name="OrderQueue">
<entry name="queues/OrderQueue"/>
</queue>
</configuration>
-文件INSTALL_DIRECTORY\config\stand-alone\non-clustered\hornetq- beans.xml 包含启动 JNDI 服务所需的 bean。
-INSTALL_DIRECTORY\config\stand-alone\non-clustered\ 文件夹中还有一个名为 jndi.properties 的文件
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=jnp://localhost:1099
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
客户端代码如下:
public void test()
{
try
{
ic = new InitialContext();
cf = (ConnectionFactory)ic.lookup("/ConnectionFactory");
orderQueue = (Queue)ic.lookup("/queues/OrderQueue");
connection = cf.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(orderQueue);
consumer = session.createConsumer(orderQueue);
connection.start();
TextMessage message = session.createTextMessage("This is an order");
producer.send(message);
TextMessage receivedMessage = (TextMessage)consumer.receive();
System.out.println("Got order: " + receivedMessage.getText());
}
catch (Exception e)
{
e.printStackTrace();
}
}
然而每当我运行它时,它都会崩溃并出现以下异常:
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:662)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:344)
at javax.naming.InitialContext.lookup(InitialContext.java:411)
我想我已经为了让它运行,我尝试了一切,但它仍然让我无法理解我做错了什么。非常感谢有关此事的任何建议!
At the moment I'm struggling to learn HornetQ. The things seem quite straightforward in theory, but I'm having a hard time running even the most basic example.
So far I've tried to start HornetQ as a standalone server, and connect a simple client that sends a message and then receives it back.
The steps I've followed are :
(accordingly to HornetQ docs
http://hornetq.sourceforge.net/docs/hornetq-2.0.0.GA/user-manual/en/html/using-jms.html)
-Downloaded the latest version of HornetQ (2.2.5) and extracted it.
-Modified the INSTALL_DIRECTORY\config\stand-alone\non-clustered\hornetq-jms.xml file to create the objects I need, below is the content :
<configuration xmlns="urn:hornetq"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:hornetq ../schemas/hornetq-jms.xsd ">
<connection-factory name="ConnectionFactory">
<connectors>
<connector-ref connector-name="netty"/>
</connectors>
<entries>
<entry name="ConnectionFactory"/>
</entries>
</connection-factory>
<queue name="OrderQueue">
<entry name="queues/OrderQueue"/>
</queue>
</configuration>
-The file INSTALL_DIRECTORY\config\stand-alone\non-clustered\hornetq-beans.xml contain the bean needed to start the JNDI service.
-There is also a file called jndi.properties in the INSTALL_DIRECTORY\config\stand-alone\non-clustered\ folder
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=jnp://localhost:1099
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
The Client code is the following :
public void test()
{
try
{
ic = new InitialContext();
cf = (ConnectionFactory)ic.lookup("/ConnectionFactory");
orderQueue = (Queue)ic.lookup("/queues/OrderQueue");
connection = cf.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(orderQueue);
consumer = session.createConsumer(orderQueue);
connection.start();
TextMessage message = session.createTextMessage("This is an order");
producer.send(message);
TextMessage receivedMessage = (TextMessage)consumer.receive();
System.out.println("Got order: " + receivedMessage.getText());
}
catch (Exception e)
{
e.printStackTrace();
}
}
Yet whenever I run it, it crashes with the following exception :
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:662)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:344)
at javax.naming.InitialContext.lookup(InitialContext.java:411)
I think I've tried everything in order to make it run, and yet it still eludes me what I'm doing wrong. Any suggestions on this matter are greatly appreciated !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,我将从 文档开始 与您正在使用的 HornetQ 版本直接相关。
我认为您的客户端代码中可能缺少库。请确保将
$HORNETQ_HOME/lib
内容添加到客户端依赖项中,并查看是否有效。否则更新您的问题并将再次尝试提供帮助:)First off, I would start with the documentation that directly relates to the HornetQ version you are using.
I think you may be missing libraries in your client code. Please make sure you add
$HORNETQ_HOME/lib
contents to your client dependencies and see if that works. Otherwise update your question and will try to help again :)你的 jndi.properties 文件在你的类路径中吗?听起来它在服务器的配置路径中,但您的客户端代码找不到它。
Is your jndi.properties file in your classpath? It sounds like its in the config path of the server, but your client code cannot find it.
您还可以通过编程方式将信息放入上下文中,尝试使用 PROVIDER_URL、INITIAL_CONTEXT_FACTORY 和 URL_PKG_PREFIXES 的值定义一个哈希表;并将其传递给 InitialContext 构造函数
它对我来说效果很好。
Also you can put the information in the context programatically, for a try, definning a hashtable with the values for the PROVIDER_URL, INITIAL_CONTEXT_FACTORY, and URL_PKG_PREFIXES; and passing it to the InitialContext constructor
It wortked fine for me.