2011-12-09 47 views
1

在我努力学习HornetQ的时刻。理论上这些事情看起来很直接,但即使是最基本的例子,我也很难做到。HornetQ运行于作为独立服务器的例子

到目前为止,我试图启动的HornetQ作为独立的服务器,并连接一个简单的客户端发送消息,然后接收回来。

我遵循的步骤是: (据此向HornetQ文档 http://hornetq.sourceforge.net/docs/hornetq-2.0.0.GA/user-manual/en/html/using-jms.html

-Downloaded最新版本的HornetQ的(2.2.5),并提取它。 修饰的安装目录\ CONFIG \独立\非群集\文件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> 

-The文件INSTALL_DIRECTORY \设置\待机动独立\非群集\ hornetq-beans.xml包含启动JNDI服务所需的bean。

-there也是在INSTALL_DIRECTORY一个名为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) 

我觉得我已经试过EV为了使它运行,但它仍然没有逃避我做错了什么。有关此事的任何建议非常感谢!

回答

1

首先,我会the documentation,直接关系到你正在使用的版本的HornetQ开始。

我想你可能会丢失在您的客户端代码库。请确保您将$HORNETQ_HOME/lib内容添加到您的客户端依赖项中,然后查看是否有效。否则,更新你的问题,并会尝试再次帮助:)

+0

我会说同样的这里+1 –

+0

嗯,我已经试过了......而这是行不通的!从我可以看出HornetQ似乎开始好了,初始化了JNDI服务,创建了所有的管理对象,但客户端无法与JNDI服务建立通信! – steve

+0

也许在客户端有更多的工作要配置正确的JNDI用法(在该文件中缺少jndi.properties或不完整的设置)! – steve

0

是您的jndi.properties文件在类路径?听起来像它在服务器的配置路径中,但你的客户端代码找不到它。

0

此外,您可以以编程方式将信息放入上下文中,以便尝试使用PROVIDER_URL,INITIAL_CONTEXT_FACTORY和URL_PKG_PREFIXES的值定义散列表;并将它传递给InitialContext的构造函数

Context ctxt = InitalContext(hashtable_w_values); 

它适合我。