10 Replies Latest reply on Dec 4, 2015 4:03 PM by sasi Kiran

    ClassNotFoundException in MessageListener

    Zufar Muhamadeev Newbie

      Hello!

      My configuration is JBoss AS 7.1 Beta.

      I have a ClassNotFoundException in simple MessageListener. Message Listener can not find class which is in the same module with listener.

       

      I wrote a simple example. There is singleton. Singleton creates MessageConsumer and add Message Listener. In Message Listner i try to lookup slsb, which is in same jar with listener. When i send message to queue:

       

      {noformat}14:44:26,015 WARN  [org.hornetq.jms.client.JMSMessageListenerWrapper] (Thread-3 (group:HornetQ-client-global-threads-808200396)) Unhandled exception thrown from onMessage: java.lang.RuntimeException: javax.naming.NamingException: Could not load ejb proxy class test.TestLocal [Root exception is java.lang.ClassNotFoundException: test.TestLocal from [Module "org.hornetq:main" from local module loader @5090d8ea (roots: /home/zgood/Apps/servers/jboss-as-7.1.0.Beta1b/modules)]]

      at org.jboss.as.naming.InitialContext.lookup(InitialContext.java:75)

      at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:213)

      at javax.naming.InitialContext.lookup(InitialContext.java:409) [:1.6.0_22]

      at test.Listener.lookupTest(Listener.java:44)

      at test.Listener.onMessage(Listener.java:24)

      at org.hornetq.jms.client.JMSMessageListenerWrapper.onMessage(JMSMessageListenerWrapper.java:91) [hornetq-jms-2.2.7.Final.jar:]

      at org.hornetq.core.client.impl.ClientConsumerImpl.callOnMessage(ClientConsumerImpl.java:866) [hornetq-core-2.2.7.Final.jar:]

      at org.hornetq.core.client.impl.ClientConsumerImpl.access$100(ClientConsumerImpl.java:44) [hornetq-core-2.2.7.Final.jar:]

      at org.hornetq.core.client.impl.ClientConsumerImpl$Runner.run(ClientConsumerImpl.java:983) [hornetq-core-2.2.7.Final.jar:]

      at org.hornetq.utils.OrderedExecutorFactory$OrderedExecutor$1.run(OrderedExecutorFactory.java:100) [hornetq-core-2.2.7.Final.jar:]

      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) [:1.6.0_22]

      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) [:1.6.0_22]

      at java.lang.Thread.run(Thread.java:679) [:1.6.0_22]

      Caused by: javax.naming.NamingException: Could not load ejb proxy class test.TestLocal [Root exception is java.lang.ClassNotFoundException: test.TestLocal from [Module "org.hornetq:main" from local module loader @5090d8ea (roots: /home/zgood/Apps/servers/jboss-as-7.1.0.Beta1b/modules)]]

      at org.jboss.ejb.client.naming.ejb.EjbNamingContext.createEjbProxy(EjbNamingContext.java:108)

      at org.jboss.ejb.client.naming.ejb.EjbNamingContext.lookup(EjbNamingContext.java:96)

      at org.jboss.ejb.client.naming.ejb.EjbNamingContext.lookup(EjbNamingContext.java:76)

      at org.jboss.as.naming.InitialContext.lookup(InitialContext.java:73)

      ... 12 more

      Caused by: java.lang.ClassNotFoundException: test.TestLocal from [Module "org.hornetq:main" from local module loader @5090d8ea (roots: /home/zgood/Apps/servers/jboss-as-7.1.0.Beta1b/modules)]

      at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:190)

      at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:485)

      at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:444)

      at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:421)

      at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:143)

      at java.lang.Class.forName0(Native Method) [:1.6.0_22]

      at java.lang.Class.forName(Class.java:264) [:1.6.0_22]

      at org.jboss.ejb.client.naming.ejb.EjbNamingContext.createEjbProxy(EjbNamingContext.java:106)

      ... 15 more{noformat}

       

      In jboss 6 example works. How can i fix this?

        • 1. Re: ClassNotFoundException in MessageListener
          Kavan Desai Newbie

          Hi Zufar,

          Can you also provide code snippet also?

          • 2. Re: ClassNotFoundException in MessageListener
            Zufar Muhamadeev Newbie

            Here is Singleton:

             

            {code}

            package test;

             

            import javax.ejb.Local;

            import javax.ejb.Remote;

            import javax.ejb.Singleton;

            import javax.jms.Connection;

            import javax.jms.Message;

            import javax.jms.MessageConsumer;

            import javax.jms.MessageProducer;

            import javax.jms.Queue;

            import javax.jms.QueueConnectionFactory;

            import javax.jms.Session;

            import javax.naming.Context;

            import javax.naming.InitialContext;

            import javax.naming.NamingException;

             

            import org.jboss.logging.Logger;

             

            /**

            *

            */

            @Singleton

            @Local(TestSingletonLocal.class)

            @Remote(TestSingletonRemote.class)

            public class TestSingleton implements TestSingletonLocal {

             

                private static final Logger LOG = Logger.getLogger(TestSingleton.class);

             

                public static final String QUEUE_NAME = "/queue/test";

             

                private InitialContext _context;

             

                private Session _session;

             

                private Connection _connection;

             

                @Override

                public void createListener() {

                    LOG.info(this);

                    try {

                        QueueConnectionFactory connectionFactory =

                                (QueueConnectionFactory) getContext().lookup(

                                        "ConnectionFactory");

                        _connection = connectionFactory.createConnection();

                        _connection.start();

                        _session =

                                _connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

                        Queue queue = (Queue) getContext().lookup(QUEUE_NAME);

                        MessageConsumer consumer = _session.createConsumer(queue);

                        consumer.setMessageListener(new Listener());

                        LOG.info("created listener");

                    } catch (Exception e) {

                        LOG.error(e, e);

                    }

                }

             

                @Override

                public void closeListener() {

                    LOG.info(this);

                    try {

                        _session.close();

                        _connection.close();

                    } catch (Exception e) {

                        LOG.error(e);

                    }

                }

             

                private Context getContext() throws NamingException {

                    if (_context == null) {

                        // Создаем контекст

                        _context = new InitialContext();

                    }

                    return _context;

                }

             

                @Override

                public void doSome() {

                    LOG.info(this);

                }

             

                @Override

                public void sendMessage() {

                    try {

                        QueueConnectionFactory connectionFactory =

                                (QueueConnectionFactory) getContext().lookup(

                                        "ConnectionFactory");

                        Connection _connection = connectionFactory.createConnection();

                        _connection.start();

                        Session session =

                                _connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

                        Message message = session.createMessage();

                        Queue queue = (Queue) getContext().lookup(QUEUE_NAME);

                        MessageProducer producer = session.createProducer(queue);

                        producer.send(queue, message);

                        producer.close();

                        session.close();

                        _connection.close();

                        LOG.info("sended: " + message);

                    } catch (Exception e) {

                        LOG.error(e, e);

                    }

                }

             

            }

            {code}

             

            Here is Listener:

             

            {code}

            package test;

             

            import javax.jms.Message;

            import javax.jms.MessageListener;

            import javax.naming.InitialContext;

            import javax.naming.NamingException;

             

            import org.jboss.logging.Logger;

             

            /**

            *

            */

            public class Listener implements MessageListener {

             

                private static final Logger LOG = Logger.getLogger(Listener.class);

             

                /**

                 * {@inheritDoc}

                 */

                @Override

                public void onMessage(Message aMessage) {

                    try {

                        lookupTest().test();

                    } catch (NamingException e) {

                        LOG.error(e);

                    }

                }

             

                private TestLocal lookupTest() throws NamingException {

                    InitialContext ctx = new InitialContext();

                    TestLocal obj =

                            (TestLocal) ctx

                                    .lookup("ejb:/jboss7-test.hornetq-1.0.0-SNAPSHOT//TestBean!test.TestLocal");

                    return obj;

                }

             

            }

            {code}

            Here is slsb:

             

            {code}

            package test;

             

            import javax.ejb.Local;

            import javax.ejb.Stateless;

             

            import org.jboss.logging.Logger;

             

            /**

            *

            */

            @Stateless

            @Local(TestLocal.class)

            public class TestBean implements TestLocal {

             

                private static final Logger LOG = Logger.getLogger(TestBean.class);

             

                @Override

                public void test() {

                    LOG.info("test");

                }

             

            }

            {code}

            • 3. Re: ClassNotFoundException in MessageListener
              Kavan Desai Newbie

              Hi Zufar,

               

              Probably you are mssing the jndi property.Something like

               

              final Hashtable jndiProperties = new Hashtable();

              jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");

              InitialContext ctx = new InitialContext(jndiProperties);

              TestLocal obj =
                              (TestLocal) ctx
                                      .lookup("ejb:/jboss7-test.hornetq-1.0.0-SNAPSHOT//TestBean!test.TestLocal");

               

              Here is a nice example given by Jaikiran.

              • 4. Re: ClassNotFoundException in MessageListener
                Zufar Muhamadeev Newbie

                Hi Kavan!

                 

                Unfortunately your suggestion does not work in my case. I have there same exception.

                 

                There is no lookup exception(NameNotFoundException), but ClassNotFoundException. As i see in stack trace it is hornetq classloading problem(module org.hornetq cannot find Local interface of slsb).

                • 5. Re: ClassNotFoundException in MessageListener
                  Nicklas Karlsson Master

                  You could of course defined a dependency in the hornetq module to your jar (not sure about the format) but I hope there is a better way to do it than editing the xml for each new application dependency...

                  1 of 1 people found this helpful
                  • 6. Re: ClassNotFoundException in MessageListener
                    Zufar Muhamadeev Newbie

                    Nicklas Karlsson wrote:

                     

                    I hope there is a better way to do it than editing the xml for each new application dependency...

                    Totaly agree

                    • 8. Re: ClassNotFoundException in MessageListener
                      brian essen Newbie

                      I have a similar problem. However, trying your code with JBoss CRb1 (the current release version) still shows ClassNotFoundException. I am using JDK 1.6.0_21. Do you have any suggestion?

                      • 9. Re: ClassNotFoundException in MessageListener
                        Zufar Muhamadeev Newbie

                        Seems like it worked because i lookup singleton in java:global namespace instead of ejb namespace in Listener.

                        • 10. Re: ClassNotFoundException in MessageListener
                          sasi Kiran Newbie

                          Hi Zufar, I have just installed Jboss 9.0 and trying to run my project but I'm getting MessageListener Error even though I have replaced with Listener. Do you have any idea why is that?