9 Replies Latest reply on Jun 19, 2013 1:11 AM by ajinkya bambal

    About Jboss AS 7 send/receive jms by queue

    Victor Chen Newbie

      Hello everybody~

       

      I have a problem that I hope someone can help me or give some suggest.

      I have a sample code which is about use jboss queue to implement jms, and I can use this sample code on Jboss 4.2.3 GA.

      but when I change to use Jboss AS 7.1.0.CR1b or Jboss 7.1.1 final then this sample code can't works.

      I found many solution and information from internet, but I still can't solve this problem.

       

      Here are the error messages.

       

      Exception in thread "main" javax.naming.NoInitialContextException: Cannot instantiate class: org.jboss.naming.remote.client.InitialContextFactory [Root exception is java.lang.ClassNotFoundException: org.jboss.naming.remote.client.InitialContextFactory]

          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 SendRecvClient.setupPTP(SendRecvClient.java:60)

          at SendRecvClient.sendRecvAsync(SendRecvClient.java:78)

          at SendRecvClient.main(SendRecvClient.java:182)

      Caused by: java.lang.ClassNotFoundException: org.jboss.naming.remote.client.InitialContextFactory

          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)

          ... 7 more

       

       

      and it is my sample code

       

      import java.util.Hashtable;

       

      import javax.jms.JMSException;

      import javax.jms.Message;

      import javax.jms.MessageListener;

      import javax.jms.Queue;

      import javax.jms.QueueConnection;

      import javax.jms.QueueConnectionFactory;

      import javax.jms.QueueReceiver;

      import javax.jms.QueueSender;

      import javax.jms.QueueSession;

      import javax.jms.TextMessage;

      import javax.jms.MapMessage;

      import javax.naming.InitialContext;

      import javax.naming.NamingException;

      import javax.naming.Context;

      import javax.jms.DeliveryMode;

      import java.util.Date;

       

      public class SendRecvClient

      {   

          private final static String JNDI_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory";

          public final static String JMS_FACTORY="jms/RemoteConnectionFactory";

         

          QueueConnection qconn = null;

          QueueSession qsession = null;

          QueueSender qsend = null;

          QueueReceiver qrecv = null;

          Queue queue = null;

       

          TextMessage Sendmessage = null;

          String text = new String(" A text msg a;lsdoinyweoiuvtweryuvewvcygwelivrewv");

         

          public static class ExListener implements MessageListener

          {

              public void onMessage(Message msg)

              {

                  TextMessage Recvmessage = (TextMessage) msg;

                  try

                  {

                      System.out.println("onMessage, Recv text = " + Recvmessage.getText());

                  }

                  catch(Throwable t)

                  {

                      t.printStackTrace();

                  }

              }

          }

       

          public void setupPTP() throws JMSException, NamingException

          {

             

              Hashtable<String, String> env = new Hashtable();

              env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);

             

              env.put(Context.PROVIDER_URL, "remote://localhost:4447");

              env.put(Context.SECURITY_PRINCIPAL, "testuser");

              env.put(Context.SECURITY_CREDENTIALS, "testpassword");

             

              InitialContext iniCtx = new InitialContext(env);

             

              Object tmp = iniCtx.lookup("jms/RemoteConnectionFactory");

              QueueConnectionFactory qcf = (QueueConnectionFactory) tmp;

       

              qconn = qcf.createQueueConnection();

              queue = (Queue) iniCtx.lookup("jms/queue/test");

         

              qsession = qconn.createQueueSession(false,QueueSession.AUTO_ACKNOWLEDGE);

             

          }

       

          public void sendRecvAsync() throws JMSException, NamingException

          {

              System.out.println("Begin sendRecvAsync...");

             

              // Set up the PTP connection, session

              setupPTP();

             

              // Set the async listener

              qrecv = qsession.createReceiver(queue);

              recv.setMessageListener(new ExListener());

             

              // Send a text msg

              qsend = qsession.createSender(queue);

              Sendmessage = qsession.createTextMessage();

       

          }

         

          class Send extends Thread

          {

              public void run()

              {          

                  try

                  {

                      for(int i = 0; i < 10; i++)

                      {   

                          Sendmessage.setText( text + " # " + i);

                          qsend.send(queue, Sendmessage);   

                          System.out.println("Sent text = " + Sendmessage.getText());

                      }

         

                  }

                  catch(JMSException e)

                  {

                      System.out.print("Exception occurred:" + e.toString());

                  }

              }

          }

         

          class Recv extends Thread

          {

              public void run()

              {

                  try

                  {

                      qrecv.setMessageListener(new ExListener());

                      qconn.start();

                     

                  }

                  catch(JMSException e)

                  {

                     

                  }

              }

          }

          

          public void run_thread1()

          {

              Send[] tt = new Send[1];

             

              for(int i = 0; i < tt.length; i++)

              {

                  tt[i] = new Send();

                  tt[i].start();

                 

              }

             

              for(int i = 0; i < tt.length; i++)

              {

                  try

                  {

                      tt[i].join();

                  }

                  catch(InterruptedException je)

                  {

                      je.printStackTrace();

                  }

              }

          }

         

          public void run_thread2()

          {

              Recv[] kk = new Recv[10000];

              for(int i = 0; i < kk.length; i++)

              {

                  kk[i] = new Recv();

                  kk[i].start();

              }

          }

       

          public void close() throws JMSException

          {

              qconn.close();

              qsession.close();

              qconn.close();

          }

         

          public static void main(String args[]) throws Exception, JMSException,NamingException

          {   

              System.out.printf("Begin SendRecvClient,now =" + " %tF %<tT\n", new Date());

              SendRecvClient client = new SendRecvClient();   

         

              client.sendRecvAsync();

              client.run_thread1();   

              client.run_thread2();

              client.close();

              System.out.println("End SendRecvClient...");   

              System.out.printf("End SendRecvClient,now =" + " %tF %<tT\n", new Date());

          }  

         

      }

       

       

      Sorry, this is my first time to use Jboss application server.

      I hope that anyone can help me or give me a suggest or direction to solve this problem.

       

      thanks a lot.

        • 1. Re: About Jboss AS 7 send/receive jms by queue
          Justin Bertram Master

          Your code looks correct.  What jars are you using on your classpath?  You should only need <JBOSS_HOME>/bin/client/jboss-client.jar.  It contains org.jboss.naming.remote.client.InitialContextFactory which cannot be found in your current environment and thus results in a java.lang.ClassNotFoundException.

          1 of 1 people found this helpful
          • 2. Re: About Jboss AS 7 send/receive jms by queue
            Victor Chen Newbie

            Thanks for your suggest, but I already include jboss-client.jar in my project, which setup it in Java Build Path > Libraries.

            This way can't works in Jboss AS 7?

            And I can't find the file /bin/client/jboss-client.jar in Jboss AS 7.1.0.CR1b which download from Jboss website.

             

            I have another question that maybe you can give me some suggests.

            I found many information about use jms with queue in Jboss AS 7, but a lot of solutions all talk within standalone. If I want to use it within domain, then how can I implement it?

             

            Thanks for your help again.

            • 3. Re: About Jboss AS 7 send/receive jms by queue
              Justin Bertram Master

              It seems clear to me that whatever you have done to put jboss-client.jar on your classpath isn't working.  If it was working properly then your code wouldn't be hitting a java.lang.ClassNotFoundException. 

               

              I'm not sure what you mean by "setup it in Java Build Path > Libraries."  Is that something you've done in an IDE?  If so, drop the IDE and try doing it from the command line or Ant.

               

              Don't use JBoss AS 7.1.0.CR1b.  Use 7.1.1.Final.

               

              Lastly, domain vs. standalone is just a matter of management.  The semantics of the server don't really change either way.

              1 of 1 people found this helpful
              • 4. Re: About Jboss AS 7 send/receive jms by queue
                Victor Chen Newbie

                Your suggest is very clearly, I will try to do.

                 

                I used Eclipse IDE to implement this code, is my fault that I didn't tell you first.

                 

                Thanks for your help.

                • 5. Re: About Jboss AS 7 send/receive jms by queue
                  Victor Chen Newbie

                  Sorry~

                  I follow your suggest to implement my setup, but I got a new problem.

                  I'd search it on google, but it doesn't have a lot of information which people talk about.

                  Have you ever had this problem? Error code as follows.

                   

                  Exception in thread "main" javax.naming.NamingException: Failed to create remoting connection [Root exception is java.util.ServiceConfigurationError: org.xnio.XnioProvider: Provider org.xnio.nio.NioXnioProvider could not be instantiated: java.lang.NoSuchMethodError: org.jboss.logging.Logger.tracef(Ljava/lang/String;Ljava/lang/Object;)V]

                      at org.jboss.naming.remote.client.ClientUtil.namingException(ClientUtil.java:36)

                      at org.jboss.naming.remote.client.InitialContextFactory.getInitialContext(InitialContextFactory.java:121)

                      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 SendRecvClient.setupPTP(SendRecvClient.java:63)

                      at SendRecvClient.sendRecvAsync(SendRecvClient.java:81)

                      at SendRecvClient.main(SendRecvClient.java:184)

                  Caused by: java.util.ServiceConfigurationError: org.xnio.XnioProvider: Provider org.xnio.nio.NioXnioProvider could not be instantiated: java.lang.NoSuchMethodError: org.jboss.logging.Logger.tracef(Ljava/lang/String;Ljava/lang/Object;)V

                      at java.util.ServiceLoader.fail(Unknown Source)

                      at java.util.ServiceLoader.access$100(Unknown Source)

                      at java.util.ServiceLoader$LazyIterator.next(Unknown Source)

                      at java.util.ServiceLoader$1.next(Unknown Source)

                      at org.xnio.Xnio.doGetInstance(Xnio.java:187)

                      at org.xnio.Xnio.getInstance(Xnio.java:146)

                      at org.jboss.remoting3.Remoting.createEndpoint(Remoting.java:73)

                      at org.jboss.naming.remote.client.EndpointCache.get(EndpointCache.java:44)

                      at org.jboss.naming.remote.client.InitialContextFactory.createEndpoint(InitialContextFactory.java:193)

                      at org.jboss.naming.remote.client.InitialContextFactory.getOrCreateEndpoint(InitialContextFactory.java:174)

                      at org.jboss.naming.remote.client.InitialContextFactory.getOrCreateNamingStore(InitialContextFactory.java:138)

                      at org.jboss.naming.remote.client.InitialContextFactory.getInitialContext(InitialContextFactory.java:104)

                      ... 7 more

                  Caused by: java.lang.NoSuchMethodError: org.jboss.logging.Logger.tracef(Ljava/lang/String;Ljava/lang/Object;)V

                      at org.xnio.nio.NioXnio.<init>(NioXnio.java:76)

                      at org.xnio.nio.NioXnioProvider.<clinit>(NioXnioProvider.java:34)

                      at java.lang.Class.forName0(Native Method)

                      at java.lang.Class.forName(Unknown Source)

                      ... 17 more

                  • 6. Re: About Jboss AS 7 send/receive jms by queue
                    Justin Bertram Master

                    This looks like another classpath problem.  Make sure the client is only using JARs from the version of the server to which it is connecting. 

                     

                    When everything is set up properly it should work without issue.  I've done this many times.

                    • 7. Re: About Jboss AS 7 send/receive jms by queue
                      Victor Chen Newbie

                      Thanks for your help on time

                      But I already did it and checked three times that the set is correct.

                      I have checked the version of jboss client.jar, and make sure it is the correct version.

                      But it still can't work.

                      I believe It's really like you say just a set problem, so I really have no idea why can't work.

                      Does it have other setting in jboss or in environment variables that I didn't modify?

                      • 8. Re: About Jboss AS 7 send/receive jms by queue
                        Victor Chen Newbie

                        Hi Justin Bertram

                        Thanks for your help again.

                        I made a big mistake.

                        I found the old version of jbossall-client.jar in my project, and I remove it, then my simple code finally can work.

                        • 9. Re: About Jboss AS 7 send/receive jms by queue
                          ajinkya bambal Newbie

                          hi Victor,

                           

                          I am also facing the same issue..I have added  Jboss 7.1 /bin/client/jboss-client.jar in my clients classpath which is I guess is latest one but still problem persist...

                          plz help me out...:0