1
votes

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 !

3

3 Answers

1
votes

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 :)

0
votes

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.

0
votes

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

Context ctxt = InitalContext(hashtable_w_values);

It wortked fine for me.