4 Replies Latest reply on Mar 4, 2014 10:31 PM by Justin Bertram

    WildFly: HQ129006: It is illegal to call this method from within a Message Listener

    Troy Bronzan Newbie

      Hello, I seem to be getting the above mentioned error when I try and close a connection.  The error occurs on the call to session.close() in JMSMessageUser.uninitialize();  Originally session.close() wasn't there, so it was happing on connection.close() instead.  As far as I can tell JMSAsyncSubscriber is not a message listener.  The Message Listener that does get used (ReportRequesListener) is create outside of this class and passed in when initializing the subscriber.  I've cut a lot of the unnecessary code from the examples below.

       

      ReportRequester - creates a subscriber and message listener

      public class ReportRequester
      {
           private JMSAsyncSubscriber subscriber;
           private ReportRequestListener listener;     //implements MessageListener
           ...
      
           public void initialize()
           {
                this.listener = new ReportRequestListener();
                this.subscriber = new JMSAsyncSubscriber();
      
                this.subscriber.initialize("topic", null, this.listener);
           }
      
           public void uninitialize()
           {
                this.subscriber.uninitialize()
           }
           ...
      }
      

       

      JMSAsyncSubscriber

       

      public class JMSAsyncSubscriber extends JMSMessageUser
      {
           private MessageConsumer subscriber;
           ...
      
           public void initialize(String topic, String filter, MessageListener listener)
           {
                super.initialize(topic);
                this.subscriber = getSession().createConsumer(getTopic(), filter);
                this.subscriber.setMessageListener(listener);
           }
      
           public void uninitialize()
           {
                this.subscriber.close();
                super.uninitialize()
           }
           ...
      }
      
      

       

      JMSMessageUser

       

      public abstract class JMSMessageUser
      {
           private Topic topic;
           private Connection connection;
           private Session session;
           
           protected void initialize(String sDestination)
           {
                InitialContext context = getInitialiContext();
                ConnectionFactory cf = (ConnectionFactory)context.lookup("java:/ConnectionFactory");
                this.topic = (Topic) context.lookup(sDestination);
                this.connection = cf.createConnection();
                this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                this.connection.start();
           }
      
           public void unitialize()
           {
                this.session.close();
                this.connection.close();
           }
           ...
      }
      

       

      Stack trace:

       

      09:09:02,014 WARN  [com.foo.jms.JMSMessageUser] (Thread-1 (HornetQ-client-global-threads-1161930933)) Unable to close JMS connection org.hornetq.jms.client.HornetQConnection@23c8266f: javax.jms.IllegalStateException: HQ129006: It is illegal to call this method from within a Message Listener
        at org.hornetq.jms.client.ThreadAwareContext.assertNotMessageListenerThread(ThreadAwareContext.java:148)
        at org.hornetq.jms.client.HornetQSession.close(HornetQSession.java:254)
        at com.foo.jms.JMSMessageUser.uninitialize(JMSMessageUser.java:133) [common.jar:]
        at com.foo.jms.JMSAsyncSubscriber.uninitialize(JMSAsyncSubscriber.java:68) [common.jar:]
        at com.foo.reports.ReportRequester.uninitialize(ReportRequester.java:87) [classes:]
        at com.foo.reports.SendToPdfWindow.close(SendToPdfWindow.java:91) [classes:]
        at com.foo.reports.SendToPdfWindow.onReportsCompleted(SendToPdfWindow.java:245) [classes:]
        at com.foo.reports.ReportRequester.onReportCompleted(ReportRequester.java:120) [classes:]
        at com.foo.reports.ReportRequester$ReportRequesterUserRequestListener.onReport(ReportRequester.java:182) [classes:]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_45]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_45]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_45]
        at java.lang.reflect.Method.invoke(Method.java:606) [rt.jar:1.7.0_45]
        at com.foo.jms.request.UserRequestListener.onMessage(UserRequestListener.java:50) [common.jar:]
        at com.foo.jms.request.WebUserRequestListener.consumeMessage(WebUserRequestListener.java:46) [foo-web.jar:]
        at com.foo.jms.WebJMSMessageHandler$ConsumeMessage.run(WebJMSMessageHandler.java:72) [foo-web.jar:]
        at com.vaadin.ui.UI.accessSynchronously(UI.java:1231) [vaadin-server-7.1.9.jar:7.1.9]
        at com.foo.application.Application.accessSynchronously(Application.java:138) [foo-web.jar:]
        at com.vaadin.ui.UI$3.run(UI.java:1297) [vaadin-server-7.1.9.jar:7.1.9]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) [rt.jar:1.7.0_45]
        at java.util.concurrent.FutureTask.run(FutureTask.java:262) [rt.jar:1.7.0_45]
        at com.vaadin.server.VaadinService.runPendingAccessTasks(VaadinService.java:1758) [vaadin-server-7.1.9.jar:7.1.9]
        at com.vaadin.server.VaadinSession.unlock(VaadinSession.java:949) [vaadin-server-7.1.9.jar:7.1.9]
        at com.vaadin.server.VaadinService.ensureAccessQueuePurged(VaadinService.java:1722) [vaadin-server-7.1.9.jar:7.1.9]
        at com.vaadin.server.VaadinService.accessSession(VaadinService.java:1688) [vaadin-server-7.1.9.jar:7.1.9]
        at com.vaadin.server.VaadinSession.access(VaadinSession.java:1306) [vaadin-server-7.1.9.jar:7.1.9]
        at com.vaadin.ui.UI.access(UI.java:1294) [vaadin-server-7.1.9.jar:7.1.9]
        at com.foo.application.Application.access(Application.java:157) [foo-web.jar:]
        at com.foo.jms.WebJMSMessageHandler.onMessage(WebJMSMessageHandler.java:47) [foo-web.jar:]
        at com.foo.jms.request.WebUserRequestListener.onMessage(WebUserRequestListener.java:37) [foo-web.jar:]
        at org.hornetq.jms.client.JMSMessageListenerWrapper.onMessage(JMSMessageListenerWrapper.java:103)
        at org.hornetq.core.client.impl.ClientConsumerImpl.callOnMessage(ClientConsumerImpl.java:1116)
        at org.hornetq.core.client.impl.ClientConsumerImpl.access$500(ClientConsumerImpl.java:56)
        at org.hornetq.core.client.impl.ClientConsumerImpl$Runner.run(ClientConsumerImpl.java:1251)
        at org.hornetq.utils.OrderedExecutorFactory$OrderedExecutor$1.run(OrderedExecutorFactory.java:104)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_45]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_45]
        at java.lang.Thread.run(Thread.java:744) [rt.jar:1.7.0_45]