Search code examples
jbossjmsmessage-queuejndihornetq

JBoss AS 7 simple hello world application


I am trying to get a simple JMS "Hello world" application to run. I would like to try it out on JBoss Application Server 7 but i am not able to run it. Jboss as HornetQ embedden in it and i started it using the following command:

standalone.bat --server-config=standalone-preview.xml

I think the problem is most likely to the way i have configured the queue within JBoss. Here are the steps i did.

  • Configure queue

Quene name: testQueue JNDI name: queue/test

The queue configuration has an option for "Selector". Can this be left blank and if not, what goes in this field?

  • Code

Here is the code i am using as the Sender. I am not worried of the receiver for now as i just want to start sending a message first.

package jms.ex3;

import javax.naming.InitialContext;

import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.QueueSender;
import javax.jms.DeliveryMode;
import javax.jms.QueueSession;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;

public class Sender
{
    public static void main(String[] args) throws Exception
    {
       // get the initial context
       InitialContext ctx = new InitialContext();

       // lookup the queue object
       Queue queue = (Queue) ctx.lookup("queue/test");

       // lookup the queue connection factory
       QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.
           lookup("queue/connectionFactory");

       // create a queue connection
       QueueConnection queueConn = connFactory.createQueueConnection();

       // create a queue session
       QueueSession queueSession = queueConn.createQueueSession(false,
           Session.DUPS_OK_ACKNOWLEDGE);

       // create a queue sender
       QueueSender queueSender = queueSession.createSender(queue);
       queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

       // create a simple message to say "Hello"
       TextMessage message = queueSession.createTextMessage("Hello");

       // send the message
       queueSender.send(message);

       // print what we did
       System.out.println("sent: " + message.getText());

       // close the queue connection
       queueConn.close();
    }
}

When i run the above class i get the following error:

java -classpath C:\Users\702723344\Downloads\glassfish-3.1.1\glassfish3\glassfish\lib\javaee.jar;. jms.ex3.Sender
Exception in thread "main" 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(Unknown Source)
        at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
        at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
        at javax.naming.InitialContext.lookup(Unknown Source)
        at jms.ex3.Sender.main(Sender.java:22)

How exactly does the above class know that the Provider(JBoss) is running on the localhost machine? Dont i need to specify an IP address somewhere? Any ideas?

Edit

Most of the documentation seem to refer to JBoss AS 6. I have updated the code snippet to include the following:

Properties env = new Properties();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory" );
    env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
    env.put(Context.PROVIDER_URL, "jnp://localhost:1199");
    ctx = new InitialContext(env);

I am now getting classNotFound exception. I think it needs an additional jar file to be added to the classpath but which one???

java -classpath C:\Users\702723344\Downloads\glassfish-3.1.1\glassfish3\glassfish\lib\javaee.jar;. jms.ex3.Sender
    Exception in thread "main" javax.naming.NoInitialContextException: Cannot instantiate class: org.jnp.interfaces.NamingContextFactory [Root exception is java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory]
            at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
            at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
            at javax.naming.InitialContext.init(Unknown Source)
            at javax.naming.InitialContext.<init>(Unknown Source)
            at jms.ex3.Sender.main(Sender.java:27)
    Caused by: java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at java.lang.Class.forName0(Native Method)
            at java.lang.Class.forName(Unknown Source)
            at com.sun.naming.internal.VersionHelper12.loadClass(Unknown Source)
            ... 5 more

Solution

  • Answering on ClassNotFoundException.

    org.jnp.interfaces.NamingContextFactory can be located in jboss-as-7.0.0.Final\modules\org\jboss\as\naming\main\jboss-as-naming-7.0.0.Final.jar.

    Also noticed glassfish\lib\javaee.jar in your classpath and wanted to give an advice on how to easily include in classpath multiple jars from jboss/client folder. But oups - there's no such folder in JBoss AS 7.0.

    It appears that JBoss AS 7.0 doesn't support remote clients at all (at least for EJB). Take a look at this thread: http://community.jboss.org/message/613171. It's very interesting.

    There is a chance you'll further get some ClassCastExceptions with using glassfish\lib\javaee.jar.