6 Replies Latest reply on Nov 27, 2009 11:24 AM by jeclic

    Display an StatusMessage from an Asynchronous method ?

    jeclic

      Hello,


      I am having a problem trying to display a status message from an Asynchronous method.
      The method is called from a button :


      part of page.xhtml :


      <h:messages globalOnly="true" styleClass="message" infoClass="messageInfo" errorClass="messageError" id="globalMessages"/>
      <s:button  value="Message asynchronous" action="#{sourceFileManager.messageAsynchronous}" />



      part of ISource.java :


      @Local
      public interface ISourceFileManager {
          @Asynchronous
          void messageAsynchronous(); 
      }



      part of Source.java :


      @Name("source")
      @Stateless
      public class Source implements ISource {
          @In private StatusMessages statusMessages;
      
          public void messageAsynchronous() {
              this.statusMessages.add(Severity.INFO, "test message");
          }
      }



      It works great when the method is not Asynchronous...
      It works also if a call the Asynchronous method from a synchronous method...but then I have to wait till the end of the treatment so I lose the advantages of @Asyncronous.


      Any idea ?
      Thank you

        • 1. Re: Display an StatusMessage from an Asynchronous method ?
          kapitanpetko

          Asynchronous methods don't have access to the session/conversation, so this will not work. You need to do polling on the client side or use something like RichFaces' <ajax:push> to notify the client.


          HTH

          • 2. Re: Display an StatusMessage from an Asynchronous method ?
            jeclic

            Ok thank you for your answer I had understood that async methods don't have access to the session/conversation but I had no idea how to handle that, ajax:push seams to be the right way.


            Just another question : is it a normal behavior that when I call my asynchronous method from a synchronous method, I have to wait till the end of the async treatment to do something else on the web page ?


            Because I could display the message just before calling the async method...


            Thank you

            • 3. Re: Display an StatusMessage from an Asynchronous method ?
              kapitanpetko

              If you don't need the result form the asynchronous method, you don't have to wait for it to complete.


              • 4. Re: Display an StatusMessage from an Asynchronous method ?
                jeclic

                Well I thought the same but I don't get it working with that code :


                page.xhtml :



                <s:button  value="Start treatment" action="#{source.callAsynchronous(otherClass.instance)}" />






                ISource.java :



                @Local
                public interface ISourceFileManager {
                    void callAsynchronous();
                
                    @Asynchronous
                    void asynchronousMethod();
                }





                Source.java :



                @Name("source")
                @Stateless
                public class Source implements ISource {
                    @In private StatusMessages statusMessages;
                
                    public void callAsynchronous(Param param) {
                        this.statusMessages.add(Severity.INFO, "Treatment is being started");
                
                        asynchronousMethod(param);
                    }
                
                    public void asynchronousMethod(Param param) {
                        // treatment
                    }
                }



                Do you see something wrong ?
                Thank you

                • 5. Re: Display an StatusMessage from an Asynchronous method ?
                  kapitanpetko

                  For messages to be displayed, you need to submit the form. That doesn't happen with <s:button/>, use <h:commandButton/> instead.
                  Calling an asynchronous method from the same component generally doesn't work (it's not intercepted), so either have another component call it,
                  or use something like:


                  Source source = (Source)Component.getInstance("source");
                  source.asynchronousMethod();
                  

                  • 6. Re: Display an StatusMessage from an Asynchronous method ?
                    jeclic

                    I created a new component for my asynchronous method and it works well :-)


                    Thank you for you help.