4 Replies Latest reply on Aug 25, 2010 10:34 PM by Nikolay Elenkov

    Is it possible parameters to Asynchronous method ?

    Rocky S Master

      I have tried unsuccessfully to pass parameters to
      @Asynchronous declared annotation method. As soon as it called this method, the parameters are gone.


      How should i pass parameters?

        • 1. Re: Is it possible parameters to Asynchronous method ?
          Leo van den berg Master

          Hi,


          try using an event with parameters and put an observer annotation on the method.


          Something like the following, You want to use two String variables:





          @In Events events;
          
          events.raiseAsynchronousEvent("eventName", var1, var2);
           (or whatever other type of Event )
          
          ...
          
          Another class...
          
          @Transactional // If you need to handle entities
          @Asynchronous
          @Observer("eventname")
          public void doSomething(String var1, String var2) {
          // Now do something
          }
          



          • 2. Re: Is it possible parameters to Asynchronous method ?
            Shervin Asgari Master

            In addtion to Leo's answer, remember that if you are calling some context later (after your async call) which depend on injection from your variables, then you must set it in the async context manually



              


             @Transactional // If you need to handle entities
                @Asynchronous
                @Observer("eventname")
                public void doSomething(String var1, String var2) {
                    Context.getEventContext.set("var1,var1);
                    Context.getEventContext.set("var2,var2);
                    //Call some other component that injects var1,var2
                }



            • 3. Re: Is it possible parameters to Asynchronous method ?
              Rocky S Master

              Thanks for your overwhelming help.


              In my problem, I was passing the List component(entity query component which includes search parameters)-


              1) If I pass this as variable to Asynchrnous annotation declared method, it would loose its page parameters.


              2) If I pass entity query component to observer and then I call getResultList, it would throw exception, result set is closed.


              So I ended up in passing parameters as you said and it works.


              My real concern is to pass entity query component and it should retain its page parameters. Curious to know it it possible ? because this will save me from passing bunch of different parameters instead of single entity query component.

              • 4. Re: Is it possible parameters to Asynchronous method ?
                Nikolay Elenkov Master

                Passing parameters to async methods works just fine. However, the async method runs in a new thread and does not have access to your page/session/conversation contexts. All persistence contexts you had are also not accessible. To pass entities they have to be detached and fully initialized (i.e., lazy collections that you may need to access have to be loaded beforehand). The alternative is to pass pointers to your data (a list of primary keys, etc.) and load it in the async method. Think of it as a form of IPC -- you have to pass things by value.


                HTH