4 Replies Latest reply on Apr 17, 2015 10:04 AM by Justin Bertram

    Sample JMS Application with HornetQ 2.4 Fails with javaee-api-5.0.1.jar

    Khaleel Ibrahim Newbie

      Hi,

       

      I wrote a sample JMS application where I am using HornetQ 2.4.0.Final. Please find below the code.

       

      *******************************************************

       

      package jms2.framework;

       

      import java.util.Hashtable;

       

      import javax.jms.Connection;

      import javax.jms.ConnectionFactory;

      import javax.jms.JMSException;

      import javax.jms.MessageConsumer;

      import javax.jms.MessageProducer;

      import javax.jms.Queue;

      import javax.jms.Session;

      import javax.jms.TextMessage;

      import javax.naming.Context;

      import javax.naming.InitialContext;

      import javax.naming.NamingException;

       

       

      public class HornetqMessageSender {

         

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

          {

              HornetqMessageSender.TestJMS();

          }

         

          protected static void TestJMS() throws JMSException {

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

              env.put(Context.PROVIDER_URL, "jnp://localhost:1099");

              env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");

              env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces  ");

             

              try {

                  Context ctx = new InitialContext(env);

                  ConnectionFactory cf = (ConnectionFactory)ctx.lookup("/ConnectionFactory");

                 

                  Queue queue = (Queue)ctx.lookup("/queue/DLQ");

                 

                 

                  Connection connection= cf.createConnection();

                  Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

                 

                  MessageProducer producer = session.createProducer(queue);

                 

                  TextMessage message = session.createTextMessage("Hello, HornetQ!");

                  producer.send(message);

                  System.out.println("Sent message: " + message.getText());

                 

                  MessageConsumer messageConsumer = session.createConsumer(queue);

                  connection.start();

                  TextMessage messageRecv = (TextMessage) messageConsumer.receive(5000);

                  System.out.println("Received Msg is" + messageRecv.getText());

       

                  connection.close();

       

       

              } catch (NamingException e) {

                  // TODO Auto-generated catch block

                  e.printStackTrace();

              }

                     

          }

      }

       

      ********************************************************************************************************************************

       

      I am using JMS 1.1 methods by using javaee-api-7.0.jar where JMS 2.0 classes are also available.

       

      When I used javaee-api-7.0.jar, I got the output successfully.

       

      But when I replaced the jar with javaee-api-5.0.1.jar, I got an exception. Please find below the same.

       

      *************************************************************************************

       

      Exception in thread "main" java.lang.NoClassDefFoundError: javax/jms/JMSSecurityRuntimeException

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

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

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

          at java.io.ObjectStreamClass.getDeclaredSUID(Unknown Source)

          at java.io.ObjectStreamClass.access$700(Unknown Source)

          at java.io.ObjectStreamClass$2.run(Unknown Source)

          at java.io.ObjectStreamClass$2.run(Unknown Source)

          at java.security.AccessController.doPrivileged(Native Method)

          at java.io.ObjectStreamClass.<init>(Unknown Source)

          at java.io.ObjectStreamClass.lookup(Unknown Source)

          at java.io.ObjectStreamClass.initNonProxy(Unknown Source)

          at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)

          at java.io.ObjectInputStream.readClassDesc(Unknown Source)

          at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)

          at java.io.ObjectInputStream.readClassDesc(Unknown Source)

          at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)

          at java.io.ObjectInputStream.readObject0(Unknown Source)

          at java.io.ObjectInputStream.readObject(Unknown Source)

          at org.hornetq.jms.referenceable.SerializableObjectRefAddr.deserialize(SerializableObjectRefAddr.java:78)

          at org.hornetq.jms.referenceable.ConnectionFactoryObjectFactory.getObjectInstance(ConnectionFactoryObjectFactory.java:42)

          at javax.naming.spi.NamingManager.getObjectInstance(Unknown Source)

          at org.jnp.interfaces.NamingContext.getObjectInstance(NamingContext.java:1479)

          at org.jnp.interfaces.NamingContext.getObjectInstanceWrapFailure(NamingContext.java:1496)

          at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:822)

          at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:686)

          at javax.naming.InitialContext.lookup(Unknown Source)

          at jms2.framework.HornetqMessageSender.TestJMS(HornetqMessageSender.java:33)

          at jms2.framework.HornetqMessageSender.main(HornetqMessageSender.java:22)

      Caused by: java.lang.ClassNotFoundException: javax.jms.JMSSecurityRuntimeException

          at java.net.URLClassLoader$1.run(Unknown Source)

          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)

          ... 28 more

      *************************************************************************************************************************************************************************

      As per HornetQ documentation, HornetQ provides a fully compliant JMS 1.1 and JMS 2.0 API.

       

      What I could understand is that even though HornetQ provides a fully compliant JMS 1.1 and JMS 2.0 API, it requires javaee-api-7.0.jar which contains both JMS 1.1 and JMS 2.0.

      Please let me know whether we can get the output by using javaee-api-5.0.1.jar, which obviously doesn't have JMSSecurityRuntimeException class as got above.

       

      My challenge here is I cannot just replace javaee-api-5.0.1.jar in my application since there are some other dependencies too.

       

      Please clarify whether javaee-api-7.0.jar would be required for using HornetQ 2.4.

       

      Regards,

      Khaleel

        • 1. Re: Sample JMS Application with HornetQ 2.4 Fails with javaee-api-5.0.1.jar
          Khaleel Ibrahim Newbie

          This thread says I have to use JRE 7

           

          https://developer.jboss.org/message/863732#863732

           

          My Working environment is JDK7 , but uses javaee-api-5.0.1.jar in the project for some modules.

           

          Please suggest.

           

          Regards

          Khaleel

          • 2. Re: Sample JMS Application with HornetQ 2.4 Fails with javaee-api-5.0.1.jar
            Justin Bertram Master

            HornetQ 2.4.0.Final (and every subsequent version of HornetQ) supports clients using either JMS 1.1 or 2.0.  Older clients using JMS 1.1 will not have to change their code.  However, they will have to change their runtime JAR files.  Typically they would just use the JAR files shipped with HornetQ.

             

            I wouldn't say that javaee-api-7.0.jar is necessarily required because HornetQ is just a JMS implementation (which is part of the larger Java EE ecosystem).  At the very least you would need the jboss-jms-api.jar shipped in HornetQ (or some equivalent).  How that fits with your particular application is something you'll have to determine.

             

            In general, I wouldn't recommend mixing implementations from both Java EE 5 and Java EE 7.

            • 3. Re: Sample JMS Application with HornetQ 2.4 Fails with javaee-api-5.0.1.jar
              Khaleel Ibrahim Newbie

              As checked, jboss-jms-api.jar contains JMS 2.0 classes too. Since I didn't remove javaee-api.5.0.1.jar from my maven repository path, JMS package from both jar files will create conflict for sure.

               

              If I remove javaee-5.0.1.jar and include jboss-jms-api.jar for the sake of JMS functionality, a lot of other dependencies will be affected.

               

              What can be done to get rid of this?

               

              Anyway I am not mixing implementations from both versions as I am using JMS 1.1 only.

               

              Regards

              Khaleel

              • 4. Re: Sample JMS Application with HornetQ 2.4 Fails with javaee-api-5.0.1.jar
                Justin Bertram Master

                As checked, jboss-jms-api.jar contains JMS 2.0 classes too.

                That's correct.  To be clear, the JMS 2.0 API classes are required at runtime for HornetQ 2.4.0.Final clients even if the client code only uses JMS 1.1.  As I stated in my previous comment, "Older clients using JMS 1.1 will not have to change their code.  However, they will have to change their runtime JAR files."

                 

                Since I didn't remove javaee-api.5.0.1.jar from my maven repository path, JMS package from both jar files will create conflict for sure.

                 

                If I remove javaee-5.0.1.jar and include jboss-jms-api.jar for the sake of JMS functionality, a lot of other dependencies will be affected.

                 

                What can be done to get rid of this?

                I'm not sure that the jboss-jms-api.jar will conflict with your javaee-api-5.0.1.jar.  If there are multiple jars with the same class then the classloader will typically just pick one and since the JMS 1.1 API in both jboss-jms-api.jar and javaee-api-5.0.1.jar is the same that shouldn't be a problem.  When it comes time to load JMS 2.0 API classes then the classloader will grab those from jboss-jms-api.jar.

                 

                Anyway I am not mixing implementations from both versions as I am using JMS 1.1 only.

                You are, in fact, mixing components from different Java EE implementations.  You're application is using javaee-api-5.0.1.jar and the JMS 1.1 API, but the implementation you're using is HornetQ 2.4.0.Final which requires the JMS 2.0 API at runtime which is from Java EE 7.

                1 of 1 people found this helpful