6 Replies Latest reply on Dec 19, 2011 9:14 AM by jaikiran

    Jboss 7 simple hello world application.

    ziggy25

      Hi all,

       

      I am trying to get a simple JMS "Hello world" application to run. I would like to try it out on Jboss 7 but i am not able to run it. 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?      

        • 1. Re: Jboss 7 simple hello world application.
          kavandesai

          Append "java:" in your look up string as it is default name space like

           

          // lookup the queue object

                 Queue queue = (Queue) ctx.lookup("java:/queue/test");

                                                                                   

                 // lookup the queue connection factory

                 QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.

                     lookup("java:/queue/connectionFactory");

           

          Hope this helps.

          • 2. Re: Jboss 7 simple hello world application.
            ziggy25

            Thanks Kavan. I made that change but i still get the same error. How does the "Sender" client know which host it needs to connect to? In my case it is localhost but dont i need to specify it somewhere with a port number?

            • 3. Re: Jboss 7 simple hello world application.
              kavandesai

              Sorry I didn't notice that you are trying to connect to access JMS Queue out side the container.In this case you need to set following parameters. They were valid for JBOSS AS 6.x I have tried for JBOSS AS 7.x may be you give a try .

               

              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");
                 
              InitialContext  ctx = new InitialContext(env);

               


              • 4. Re: Jboss 7 simple hello world application.
                ctomc

                Hi,

                 

                take a look on how to do remote JDNI lookup and invocation here:

                 

                https://docs.jboss.org/author/display/AS71/EJB+invocations+from+a+remote+client+using+JNDI

                 

                guide is for remote EJB-s but it should also work for jms, but be sure you are using JBoss 7.1.x

                 

                 

                cheers,

                tomaz

                 

                Message was edited by: Tomaz Cerar

                • 5. Re: Jboss 7 simple hello world application.
                  ziggy25

                  Ok i have this error now after making that change:

                   

                  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)

                   

                  Presumabely it is looking for a jar file. I went through the whole of jboss7 directly and cant find any jar file with "client" in its name.

                  • 6. Re: Jboss 7 simple hello world application.
                    jaikiran

                    Remote access to anything other than remote EJBs, via JNDI,  is not supported in any of the released AS7 versions. Keep an eye on https://issues.jboss.org/browse/AS7-1338 feature request for updates.