ziggy
ziggy

Reputation: 15876

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.

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?

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

Upvotes: 1

Views: 13246

Answers (2)

Abhijith Rao
Abhijith Rao

Reputation: 1

I do not agree with the last answer from Vadzim

I know I am answering a very old question but this info miss-led me for a day, It was the first time I was trying to setup JMS remote queue on JBOSS 7 , hence had to answer, which got me working.

It appears that JBoss AS 7.0 doesn't support remote clients at all (at least for EJB)...??

It is possible to call remote queue. below are the following steps ..

  1. Make sure to add the queues in standalone.xml or (full.xml) , in below I have got myRemoteStatusQueue set up , if you observe, exported option is must, so that the jboss knows it will consumed by the external jms client

    <jms-destinations>
        <jms-queue name="testQueue">
            <entry name="queue/test"/>
            <entry name="java:jboss/exported/jms/queue/test"/>
        </jms-queue>
        <jms-queue name="ddsStatusQueue">
            <entry name="java:jboss/exported/jms/queue/myRemoteStatusQueue"/><!--Exported key necessary -->
        </jms-queue>
        <jms-topic name="testTopic">
            <entry name="topic/test"/>
            <entry name="java:jboss/exported/jms/topic/test"/>
        </jms-topic>
    </jms-destinations>
    
  2. Make sure to add a role to an application user : such as remote-role , after adding the role make sure to include them in the Jboss's standalone.xml.

    <jms-destinations>
        <jms-queue name="testQueue">
            <entry name="queue/test"/>
            <entry name="java:jboss/exported/jms/queue/test"/>
        </jms-queue>
        <jms-queue name="ddsStatusQueue">
            <entry name="java:jboss/exported/jms/queue/myRemoteStatusQueue"/>   <!--Exported key necessary -->
        </jms-queue>
        <jms-topic name="testTopic">
            <entry name="topic/test"/>
            <entry name="java:jboss/exported/jms/topic/test"/>
        </jms-topic>
    </jms-destinations>
    <security-settings>
        <security-setting match="#">
            <permission type="send" roles="remote-role guest"/>
            <permission type="consume" roles="remote-role guest"/>
            <permission type="createNonDurableQueue" roles="guest"/>
            <permission type="deleteNonDurableQueue" roles="guest"/>
        </security-setting>
    </security-settings>
    
  3. Make sure to use below code ,you can see I have used remote:/ as part of URL for remoting

    private static final String DEFAULT_MESSAGE = " Dummy message local Client Message At"+new Date();
    private static final String DEFAULT_CONNECTION_FACTORY = "/jms/ConnectionFactory";
    private static final String DEFAULT_DESTINATION = "jms/queue/myRemoteStatusQueue";
    private static final String DEFAULT_MESSAGE_COUNT = "1";
    private static final String DEFAULT_USERNAME = "abhijith";
    private static final String DEFAULT_PASSWORD = "password";
    private static final String INITIAL_CONTEXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory";
    private static final String PROVIDER_URL = "remote://localhost:4447";
    
  4. Make sure to use the right jars, for the Above example , I used hornetq-core-2.2.14.Final.jar and the usage of correct JBOSS client jar , go to the client folder of the bin directory of JBOSS, you will have readme.txt, there you will have reference for Maven version to use, if you are using as a standalone application then you can reference the given jar in the directory.

  5. Deploy your app on JBOSS , During the start up you should see the configured Queue without any error. You can see for example in my server startup log, java:jboss/exported/jms/queue/myRemoteStatusQueue. If the above entry has come up with out error, now you are good to go ...

    16:24:46,527 INFO  [org.jboss.as.messaging] (MSC service thread 1-2) JBAS011601: Bound messaging object to jndi name java:/queue/test
    16:24:46,533 INFO  [org.jboss.as.messaging] (MSC service thread 1-2) JBAS011601: Bound messaging object to jndi name java:jboss/exported/jms/queue/test
    16:24:46,545 INFO  [org.jboss.as.messaging] (MSC service thread 1-8) JBAS011601: Bound messaging object to jndi name java:jboss/exported/jms/RemoteConnectionFactory
    16:24:46,548 INFO  [org.jboss.as.messaging] (MSC service thread 1-8) JBAS011601: Bound messaging object to jndi name java:/RemoteConnectionFactory
    16:24:46,551 INFO  [org.jboss.as.messaging] (MSC service thread 1-3) JBAS011601: Bound messaging object to jndi name java:/ConnectionFactory
    16:24:46,552 INFO  [org.hornetq.core.server.impl.HornetQServerImpl] (MSC service thread 1-4) trying to deploy queue jms.topic.testTopic
    16:24:46,569 INFO  [org.jboss.as.deployment.connector] (MSC service thread 1-6) JBAS010406: Registered connection factory java:/JmsXA
    16:24:46,607 INFO  [org.hornetq.ra.HornetQResourceAdapter] (MSC service thread 1-6) HornetQ resource adaptor started
    16:24:46,609 INFO  [org.jboss.as.connector.services.ResourceAdapterActivatorService$ResourceAdapterActivator] (MSC service thread 1-6) IJ020002: Deployed: file://RaActivatorhornetq-ra
    16:24:46,612 INFO  [org.jboss.as.deployment.connector] (MSC service thread 1-6) JBAS010401: Bound JCA ConnectionFactory [java:/JmsXA]
    16:24:46,638 INFO  [org.jboss.as.messaging] (MSC service thread 1-4) JBAS011601: Bound messaging object to jndi name java:/topic/test
    16:24:46,641 INFO  [org.jboss.as.messaging] (MSC service thread 1-4) JBAS011601: Bound messaging object to jndi name java:jboss/exported/jms/topic/test
    16:24:46,642 INFO  [org.hornetq.core.server.impl.HornetQServerImpl] (MSC service thread 1-7) trying to deploy queue jms.queue.myRemoteStatusQueue
    16:24:46,645 INFO  [org.jboss.as.messaging] (MSC service thread 1-7) JBAS011601: Bound messaging object to jndi name java:jboss/exported/jms/queue/myRemoteStatusQueue
    16:24:47,211 INFO  [org.jboss.ws.common.management.AbstractServerConfig] (MSC service thread 1-1) JBoss Web Services - Stack CXF Server 4.0.2.GA
    (-1 / -1) (org/jboss/as/network/ManagedBinding$Factory/org/jboss/as/network/ManagedBinding$Factory)
    

Happy coding,

Regards, Abhijith

Upvotes: 0

Vadzim
Vadzim

Reputation: 26160

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.

Upvotes: 5

Related Questions