4 Replies Latest reply on Jul 14, 2010 9:25 PM by kapitanpetko

    Asynchronous methods doesn't run asynchronously :(

    idyoshin

      Hello community!


      I'm working on development of several background tasks for my System. Thus I need to create the asynchronous calls:


      here's how I'm doing this using Stateless EJB, the method which triggers this calculation is annotated with @Transactional @TransactionAttribute(REQUIRES_NEW) and @Asynchronous. The method only excepts String parameter of the id (no @Expires, @IntervalCron parameters). When I start using this method in loop - it doesn't makes asynchronous calls :(  the method is invoked one-by-one not as multithreaded asynchronous calls :(   I'm using Quartz with inMemory store (also tried the jdbc store - doesn't helped)


      any idea?


      Ah yeap, the code snippets:


      The main logic


      @Local
      public interface IRezervCalculator {
           
           @Transactional
           public void polici(Polici p, Date calculationDate);
      
           
           @Transactional
           public void cache(Polici p);
           
          
          @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
           @Transactional
           public void cache(String idPolici); 
      }
      



      The scheduler


      @Name("rezervScheduler")
      @Scope(ScopeType.STATELESS)
      public class RezervScheduler {
      
           @Logger
           Log logger;
           
           @In(create=true, value="polici.rezerv.calculator")
           IRezervCalculator calculator;
      
           @Asynchronous
           public void cache(String idPolici) {
                logger.info("scheduling #0 ", idPolici);
                calculator.cache(idPolici);
           }
      } 
      



      And I'm starting this calculations from the REST-service with something like this:


                      Query q = em.createQuery(.........);
                List<Map<String, Object>> obList = q.getResultList() ;
                for (Map<String, Object> m : obList) {
                     String id = m.get("id").toString();
                     rezervScheduler.cache(id);
                }
      



      Any idea how to make things working in parallel?



      Kind regards,
      Ilya Dyoshin

        • 1. Re: Asynchronous methods doesn't run asynchronously :(
          kapitanpetko

          How are you getting an instance of RezervScheduler in your REST-service?

          • 2. Re: Asynchronous methods doesn't run asynchronously :(
            idyoshin

            Via seam injection @In(value="rezervScheduler", create=true)

            • 3. Re: Asynchronous methods doesn't run asynchronously :(
              jeanluc

              I found in practice that calls to @Asynchronous code in the same class are shortcut and executed synchronously, even if the reference to the target object is obtained through Seam (injection or Component.getInstance()). This would have been expected if the methods were just invoked from the same instance, but not when the reference is obtained so that Seam's proxies are used.


              If methodA has to call methodB asynchronously, I had to put them in separate classes.

              • 4. Re: Asynchronous methods doesn't run asynchronously :(
                kapitanpetko

                Ilya Dyoshin wrote on Jul 14, 2010 06:03:


                Via seam injection @In(value="rezervScheduler", create=true)


                Well, in that case, either you components.xml is not set up correctly or the AsynchronousInterceptor is not being invoked. If your RezervScheduler is in an EJB jar, check you have set up the Seam interceptor correctly. If all fails, set a break point in AcynchronousInterceptor.aroundInvoke or simply examine the stack when cache is being called to see what is happening.


                Btw, don't mix @Transactional and @TransactionalAttribute. Since your component is an EJB, just use @TransactionalAttribute.


                HTH