6 Replies Latest reply on Apr 5, 2011 8:09 AM by Leo van den berg

    Raising Events from @Async... method

    get boa Newbie

      How can I observer(using @Observer(myObserver)) an event which is gonna be raised(Events.instance().raiseAsynchronousEvent(myObserver)) before an async method ends? The observer method is placed in an event-scoped component(callerAction), and this async method belongs to an event-scoped component(callerAsync) and also annotated as @AutoCreate.


      for example:


      @Name("callerAction")
      public class Caller{
              
              @In
              private CallerAsync callerAsync;
              
              public void call(){
                      callerAsync.runAsyncMethod();
              }
              
              @Observer(value="myObserver")   
              public void callerAsyncObserver(){
                      System.out.println("Observing...");
              }
      }





      @Name("callerAsync")
      @AutoCreate
      public class CallerAsync{
              
         @Asynchronous        
              public void call(){
                      // some expensive work being processed
                      Events.instance().raiseAsynchronousEvent("myObserver");
              }
      
      }



      hmmmm... If there's another/better approach to do this, let me know.
      Thanks in advance.

        • 1. Re: Raising Events from @Async... method
          Leo van den berg Master

          Hi,


          make it a chain where you place the observer in another bean (that's what I usually do) In any way the method is called async and the call will NOT use the same bean (-instance) it will make a new instance when the event is raised.


          Leo

          • 2. Re: Raising Events from @Async... method
            get boa Newbie

            Leo van den Berg wrote on Mar 30, 2011 15:20:


            Hi,

            make it a chain where you place the observer in another bean (that's what I usually do) In any way the method is called async and the call will NOT use the same bean (-instance) it will make a new instance when the event is raised.

            Leo


            Hi Leo,
            thanks for your reply. Could you please explain it a little bit more!?

            • 3. Re: Raising Events from @Async... method
              get boa Newbie

              Did you mean to create a sort of @Filter and place that @observer in there?
              Sorry I didn't get it properly.

              • 4. Re: Raising Events from @Async... method
                Leo van den berg Master

                Hi,


                I mean that you should create a third bean which handles the termination of the long-running task.


                So:


                (1) Start the process with callerAsync.runAsyncMethod();
                (2) The observing callerAsync handles this request through its observer, and
                (3) A third bean resultObserver handles the event after processing the whole task.


                Although it looks this way, if your first bean (which kicks-off the task) isn't an application scoped bean, the Asynchronity of the different calls will not reach the calling bean, You could consioder making the fist bean application scoped, but that will be tricky when you're with these kind of unpredictable threads and especially if several users can start such an event.


                If you just want to store the result in a database, the chain of observers with an event scope will work fine if you add a createtrue attribute to you observer.


                Leo


                P.S. Although the static call works fine, i prefer to inject the Events component with a standard in-aanotation.


                .

                • 5. Re: Raising Events from @Async... method
                  get boa Newbie

                  Hi Leo,
                  Thanks a lot.
                  It worked, but I'm getting a weird behaviour on the progressBar from richfaces framework, What I'm doing is, the user provides an amount of objects to be loaded from database, afterwards the application validates those objects and that's it. It supposed now to show the user, a facesmessage with the result of this process(success or fail), which is in
                  f:facet namecomplete. The problem is, if the user provides an amount like, over than 100, the f:facet namecomplete is not reached, but if it's less than 100 it works like a charm.
                  Do you have any idea of why it's behaving like that?


                  • 6. Re: Raising Events from @Async... method
                    Leo van den berg Master

                    Hi,


                    I included a code snippet of one of our applications. The checkProgress method provides a value between 0 and 100%


                    You must change the max and min-value for other ranges.





                    <rich:progressBar label="#{bean.instance.checkProgress()} %" 
                                 eventsQueue="inputQueue" 
                                 ajaxSingle="true" 
                                 interval="10000" 
                                 id="progressHandling"  
                                       value="#{bean.instance.checkProgress()}"  
                                 minValue="-1" 
                                 maxValue="100"/>
                    
                    




                    Leo