4 Replies Latest reply on Oct 11, 2010 4:04 PM by Slawek Sobotka

    Asynchronous call from Observer

    Slawek Sobotka Newbie
      Hi, I'm trying to do the following scenario:
      [component -> observer -> asynch method]

      Logic works but asynch method is called in the synch way - i know it because it slows down response time
      When I get rid of observer:
      [component -> asynch method]
      than works perfectly in asynchronous way.


      1. Conversation component raises event:

      events.raiseTransactionSuccessEvent(EventsDictionary.DOCUMENT_RELEASED, document);

      2. Event Listener calls Async. method of other component:

      public void documentReleased(Document document){
        mailSender.sendMail("documentReleased", params);

      3. Where mail sender is:

      public class MailSender {
        public void sendMail(String mail, Map<String, Object> params) {
          ... tricks with mail sending
        • 1. Re: Asynchronous call from Observer
          Leo van den berg Master


          I solve this issue by adding an additional observer layer which is used to watch for the transaction success event, does some pre-processing (if necessary) and raise an Asynchronous event.

          One of the advantages is that within the original call (with the transaction) the delay is decereased to a minimum and you're also able to call something with a new Transaction. So basically

          Component --> raiseTransactionSuccess --> Component --> RaiseSyncronousEvent --> doSomething.

          Hopefully this helps.


          • 2. Re: Asynchronous call from Observer
            Slawek Sobotka Newbie

            hmm nice trick:)
            works like a charm.
            thanks a lot Leo!

            I'm wondering is it possible to add a facesMessage to some context in the last element of this chain?
            I know that last phase is asynchronous and works in different context (I guess) but it would be nice if user could see message in the n-th request in the future.

            • 3. Re: Asynchronous call from Observer
              Leo van den berg Master


              adding a message to the context is a bit more complex because you're running the task asynchronous and it no notion whatsoever of the context. However you can create messages without a problem. The thing I do is to send these messages to an Application scoped bean which hols the listener for an rich ajax-poll element. The meaaage is received and added to a Map which has the user as Key and a message List to which the message is added. At the moment the user-poll is triggered it reads the list with pending messages and  adds them to the context.



              • 4. Re: Asynchronous call from Observer
                Slawek Sobotka Newbie

                smart, i like that:)

                thanks again