4 Replies Latest reply on Oct 9, 2013 11:32 AM by jbertram

    Bind JMS on JTA

    suikast42

      Hi,

       

      I have little bit problems to understand the binding JMS on JTA. In our application we wirte some information about entity changes to JMS. And if the transaction was rolled back the JMS should send nothing. For that I convert our database connection to a xa-datasource and use the JMS Factory to java:/JmsXA. So far so good.

       

      So my questions:

       

      1. Is it right that only the sender have to use java:/JmsXA. for sending messages. Because if the I try to register a messageListener with JmsXA I get this exception :

      This method is not applicable inside the application server. See the J2EE spec, e.g. J2EE1.4 Section 6.6

      I assume the sender must use java:/JmsXA and the recevier java:/ConenctionFactory or java:/RemoteConnectionFactory. Right??

       

      2. When and where I have to create my JMS session?

      For sending messages I create an EJB (it doesn't matter if Singleton or Stateless). The JTA binding on JMS works only if create the JMS session within my sender method. In other words in my transaction.

       

       

      For example sender:

         public void sendText(String text){
              try {
                  createSender(); // Create JMS Session with java:/JmsXA.
                  logger.debug(utx.getStatus());
                  TextMessage textMessage = session.createTextMessage();
                  textMessage.setText(text);
                  sender.send(textMessage);
                  logger.debug("Message send "+textMessage.getJMSMessageID());
              } catch (JMSException |javax.transaction.SystemException e) {
                  e.printStackTrace(); 
              }  finally {
                  deinit();
              }
          }
      

       

      JMs Listener

         public synchronized void init() {
                  if (initilized) {
                      return;
                  }
                  try {
                      String destination = getReportingQueue();
      
                      conn = ServiceLocatorFactory.getServiceLocator().getQueueConnection(); // Lookup from java:/ConnectionFactory the lookup from java:/JmsXA will throw  This method is not applicable inside the application server. See the J2EE spec, e.g. J2EE1.4 Section 6.6
      
                      session = conn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
                      Queue queue = ServiceLocatorFactory.getServiceLocator().getQueue(destination);
      
                      receiver = session.createReceiver(queue);
                      conn.start();
                      receiver.setMessageListener(this);
                      logger.info("Register on " + queue.getQueueName());
                  } catch (Exception e) {
                      logger.error("", e);
                      fail(e.getMessage());
                  }
                  initilized = true;
              }
      

       

      Full stacktrace if I use XA on listener side:

      16:50:00,369 ERROR [com.siemag.base.test.utils.WMSTestWatcher] (http-127.0.0.1/127.0.0.1:8090-2) ##### com.siemag.base.test.integration.reporting.JMSTransactionTest.transactionRollbackThenOpenNewTransaction failed #####
      16:50:00,369 ERROR [com.siemag.base.test.utils.WMSTestWatcher] (http-127.0.0.1/127.0.0.1:8090-2) : java.lang.AssertionError: This method is not applicable inside the application server. See the J2EE spec, e.g. J2EE1.4 Section 6.6
          at org.junit.Assert.fail(Assert.java:88) [arquillian-junit.jar:]
          at com.siemag.base.test.integration.reporting.JMSTransactionTest$JMSQueueListener.init(JMSTransactionTest.java:86) [additionalClasses.jar:]
          at com.siemag.base.test.integration.reporting.JMSTransactionTest.createSender(JMSTransactionTest.java:285) [additionalClasses.jar:]
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_25]
          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_25]
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_25]
          at java.lang.reflect.Method.invoke(Method.java:606) [rt.jar:1.7.0_25]
          at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) [arquillian-junit.jar:]
          at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [arquillian-junit.jar:]
          at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) [arquillian-junit.jar:]
          at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24) [arquillian-junit.jar:]
          at org.jboss.arquillian.junit.Arquillian$StatementLifecycleExecutor.invoke(Arquillian.java:351) [arquillian-junit.jar:]
          at org.jboss.arquillian.container.test.impl.execution.BeforeLifecycleEventExecuter.on(BeforeLifecycleEventExecuter.java:35) [arquillian-core.jar:]
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_25]
          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_25]
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_25]
          at java.lang.reflect.Method.invoke(Method.java:606) [rt.jar:1.7.0_25]
          at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94) [arquillian-core.jar:]
          at org.jboss.arquillian.core.impl.EventContextImpl.invokeObservers(EventContextImpl.java:99) [arquillian-core.jar:]
          at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:81) [arquillian-core.jar:]
          at org.jboss.arquillian.test.impl.TestContextHandler.createTestContext(TestContextHandler.java:89) [arquillian-core.jar:]
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_25]
          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_25]
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_25]
          at java.lang.reflect.Method.invoke(Method.java:606) [rt.jar:1.7.0_25]
          at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94) [arquillian-core.jar:]
          at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88) [arquillian-core.jar:]
          at org.jboss.arquillian.test.impl.TestContextHandler.createClassContext(TestContextHandler.java:75) [arquillian-core.jar:]
          at sun.reflect.GeneratedMethodAccessor153.invoke(Unknown Source) [:1.7.0_25]
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_25]
          at java.lang.reflect.Method.invoke(Method.java:606) [rt.jar:1.7.0_25]
          at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94) [arquillian-core.jar:]
          at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88) [arquillian-core.jar:]
          at org.jboss.arquillian.test.impl.TestContextHandler.createSuiteContext(TestContextHandler.java:60) [arquillian-core.jar:]
          at sun.reflect.GeneratedMethodAccessor152.invoke(Unknown Source) [:1.7.0_25]
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_25]
          at java.lang.reflect.Method.invoke(Method.java:606) [rt.jar:1.7.0_25]
          at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94) [arquillian-core.jar:]
          at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88) [arquillian-core.jar:]
          at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:135) [arquillian-core.jar:]
          at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:115) [arquillian-core.jar:]
          at org.jboss.arquillian.test.impl.EventTestRunnerAdaptor.before(EventTestRunnerAdaptor.java:95) [arquillian-core.jar:]
          at org.jboss.arquillian.junit.Arquillian$4.evaluate(Arquillian.java:222) [arquillian-junit.jar:]
          at org.jboss.arquillian.junit.Arquillian.multiExecute(Arquillian.java:314) [arquillian-junit.jar:]
          at org.jboss.arquillian.junit.Arquillian.access$100(Arquillian.java:46) [arquillian-junit.jar:]
          at org.jboss.arquillian.junit.Arquillian$5.evaluate(Arquillian.java:240) [arquillian-junit.jar:]
          at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55) [arquillian-junit.jar:]
          at org.junit.rules.RunRules.evaluate(RunRules.java:20) [arquillian-junit.jar:]
          at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) [arquillian-junit.jar:]
          at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) [arquillian-junit.jar:]
          at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) [arquillian-junit.jar:]
          at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) [arquillian-junit.jar:]
          at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) [arquillian-junit.jar:]
          at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) [arquillian-junit.jar:]
          at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) [arquillian-junit.jar:]
          at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) [arquillian-junit.jar:]
          at org.jboss.arquillian.junit.Arquillian$2.evaluate(Arquillian.java:185) [arquillian-junit.jar:]
          at org.jboss.arquillian.junit.Arquillian.multiExecute(Arquillian.java:314) [arquillian-junit.jar:]
          at org.jboss.arquillian.junit.Arquillian.access$100(Arquillian.java:46) [arquillian-junit.jar:]
          at org.jboss.arquillian.junit.Arquillian$3.evaluate(Arquillian.java:199) [arquillian-junit.jar:]
          at org.junit.runners.ParentRunner.run(ParentRunner.java:309) [arquillian-junit.jar:]
          at org.jboss.arquillian.junit.Arquillian.run(Arquillian.java:147) [arquillian-junit.jar:]
          at org.junit.runner.JUnitCore.run(JUnitCore.java:160) [arquillian-junit.jar:]
          at org.junit.runner.JUnitCore.run(JUnitCore.java:138) [arquillian-junit.jar:]
          at org.jboss.arquillian.junit.container.JUnitTestRunner.execute(JUnitTestRunner.java:65) [arquillian-junit.jar:]
          at org.jboss.arquillian.protocol.servlet.runner.ServletTestRunner.executeTest(ServletTestRunner.java:160) [arquillian-protocol.jar:]
          at org.jboss.arquillian.protocol.servlet.runner.ServletTestRunner.execute(ServletTestRunner.java:126) [arquillian-protocol.jar:]
          at org.jboss.arquillian.protocol.servlet.runner.ServletTestRunner.doGet(ServletTestRunner.java:90) [arquillian-protocol.jar:]
          at javax.servlet.http.HttpServlet.service(HttpServlet.java:734) [jboss-servlet-api_3.0_spec-1.0.2.Final.jar:1.0.2.Final]
          at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.2.Final.jar:1.0.2.Final]
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:295) [jbossweb-7.2.0.Final.jar:7.2.0.Final]
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) [jbossweb-7.2.0.Final.jar:7.2.0.Final]
          at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230) [jbossweb-7.2.0.Final.jar:7.2.0.Final]
          at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:149) [jbossweb-7.2.0.Final.jar:7.2.0.Final]
          at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:389) [jbossweb-7.2.0.Final.jar:7.2.0.Final]
          at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.2.0.Alpha1-redhat-4.jar:7.2.0.Alpha1-redhat-4]
          at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.2.0.Alpha1-redhat-4.jar:7.2.0.Alpha1-redhat-4]
          at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:169) [jboss-as-web-7.2.0.Alpha1-redhat-4.jar:7.2.0.Alpha1-redhat-4]
          at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:145) [jbossweb-7.2.0.Final.jar:7.2.0.Final]
          at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:97) [jbossweb-7.2.0.Final.jar:7.2.0.Final]
          at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:102) [jbossweb-7.2.0.Final.jar:7.2.0.Final]
          at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:336) [jbossweb-7.2.0.Final.jar:7.2.0.Final]
          at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:880) [jbossweb-7.2.0.Final.jar:7.2.0.Final]
          at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:607) [jbossweb-7.2.0.Final.jar:7.2.0.Final]
          at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:2036) [jbossweb-7.2.0.Final.jar:7.2.0.Final]
          at java.lang.Thread.run(Thread.java:724) [rt.jar:1.7.0_25]
      

       

       

      My environment:

      Jboss 7.2.0.Final ( Hornetq as shipped )

      Windows 7 64-Bit

       

      Thanks in advance

        • 1. Re: Bind JMS on JTA
          jbertram

          1. Is it right that only the sender have to use java:/JmsXA. for sending messages.

          Yes, that is right.  See https://docs.jboss.org/author/display/WFLY8/Messaging+configuration for more information about what connection factories are suitable for what use-cases.

           

          I assume the sender must use java:/JmsXA and the recevier java:/ConenctionFactory or java:/RemoteConnectionFactory. Right??

          Right.

           

          2. When and where I have to create my JMS session?

          You need to do it in the context of the active JTA transaction in which you wish the resource to be enlisted.

          • 2. Re: Bind JMS on JTA
            suikast42

            I've forgotten my important question. Is that not an antipattern to create the JMS session everytime in the server?

            • 3. Re: Bind JMS on JTA
              suikast42

              Thanks Justin. If you say to me that is not an antipattern to create the jms stuff everytime in the sender method then I'm happy :-D.

              • 4. Re: Bind JMS on JTA
                jbertram

                As long as you are using a pooled-connection-factory (which JmsXA is) then it's not an anti-pattern.

                1 of 1 people found this helpful