2 Replies Latest reply on Apr 5, 2012 5:19 AM by Stephen Coy

    Using @Asynchronous in JBoss 7

    bryan.solan Newbie

      I created a simple JSF/EJB project to try out @Asynchronous in JBoss 7, but the results were not what I expected. getMyResult() gets called from the JSF layer which calls the async method getMyResultSubroutine().

       

      Here's what my bean looks like:

       

      @Stateless
      public class TestAsyncBean extends implements Test {
      
          @Inject
          private Logger log;
      
          @Asynchronous
          public void getMyResultSubroutine() {
              log.info("Starting method getMyResultSubroutine()");
      
              try {
                  Thread.sleep(3000l);
              } catch (InterruptedException e) {
              }
      
              log.info("Ending method getMyResultSubroutine()");
         }
      
          public String getMyResult() {
              log.info("getMyResult() : BEFORE @Async call");
      
              getMyResultSubroutine();
      
              log.info("getMyResult() : AFTER @Async call");
              return "Some result"; 
          }
      

       

      Any my output:

       

      08:58:07,152 INFO  [com.test.TesAsynctBean] (http--127.0.0.1-8080-2) getMyResult() : BEFORE @Async call

      08:58:07,152 INFO  [com.test.TesAsync} (http--127.0.0.1-8080-2) Starting method getMyResultSubroutine()

      08:58:10,153 INFO  [com.test.TesAsync] (http--127.0.0.1-8080-2) Ending method getMyResultSubroutine()

      08:58:10,153 INFO  [com.test.TesAsync] (http--127.0.0.1-8080-2) getMyResult() : AFTER @Async call

       

      I was expecting at least the message "Ending method getMyResultSubroutine()" to come after the message "getMyResult() : AFTER @Async call"

       

      What am I missing here?

        • 1. Re: Using @Asynchronous in JBoss 7
          jaikiran pai Master

          Bryan Solan wrote:

           

          
              public String getMyResult() {
                  ...
           
                  getMyResultSubroutine();
           ...
              }
          

           

          You are doing a plain Java call from getMyResult to getMyResultSubroutine() method. That won' introduce any EJB semantics to the call. See this on how to get it working https://community.jboss.org/message/525521#525521

          • 2. Re: Using @Asynchronous in JBoss 7
            Stephen Coy Master

            From the EJB 3.1 Spec §4.5.1

            Asynchronous method invocation semantics only apply to the no-interface, Local business, and Remote business client views. 

             

            In other words, you have to invoke the @Asynchronous method from an external bean reference:

             

            {code:java}

            class MyClient {

             

                 @EJB

                 private TestAsyncBean testBean;

             

             

                 ... someMethod(...) {

                      ...

                      testBean.getMyResultSubroutine();

                      ...

                }

            }

            {code}

             

            This is because the EJB machinery needs the opportunity to invoke the method through a proxy. I've used the no-interface view above, but you could also use your interface if the method was declared on it.

            1 of 1 people found this helpful