3 Replies Latest reply on Oct 18, 2013 1:22 PM by bleathem

    Add FacesMessage from java thread

    miguel0afd

      Hi,

       

      I'm trying to show a FacesMessage from a java thread, launched from a bean, using a rich:notify element but the problem is that when the process finishes, I try to get the FacesContext of the current instance from the thread, using the same bean object that launched the thread, but the FacesContext is null (I guess the request and response are already done). I'll post some parts of the code:

       

      main.xhtml

      ...
      <rich:notifyMessages stayTime="2000" nonblocking="true" escape="false" />
      ...
      <h:form>
           <a4j:region>
                <a4j:commandButton value="Submit" action="#{backingBean.launchTest()}" execute="@region" />
           </a4j:region>
      </h:form>
      ...
      


      BackingBean.java


      ...
      private String command;
      ...
      public void launchTest(){
           ExecThread execThread = new ExecThread(this, command);
           Thread thread = new Thread(execThread);
           thread.start();
      }
      
      public FacesContext getCurrentInstance(){
           return FacesContext.getCurrentInstance;
      }
      ...
      


      ExecThread.java

       

      ...
      public class ExecThread extends Thread {
      
           private BackingBean backingBean;
           private String command;
      
           public ExecThread(BackingBean backingBean, String command){
                this,backingBean = backingBean;
                this.command = command;
           }
      
          @Override
          public void run(){
                Process p = Runtime.getRuntime().exec(command);
                p.waitFor();
                backingBean.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Test done", "Test done"))
           }
      }
      ...
      

       

      By the way, I'm using RichFaces 4.3.4

       

      Any ideas o alternatives? Thanks in advance

        • 1. Re: Add FacesMessage from java thread
          bleathem

          When you launch your own thread, the JSF lifecyle will carry on processing independent of your thread.  Rather, if you want to create a Faces Message from a separate Java thread try using RichFaces push (the a4j:push component).

          • 2. Re: Add FacesMessage from java thread
            miguel0afd

            I forgot to mention that I already try to solve it with the a4j:push component but the problem is that others ajax components stop working:

             

            main.xhtml

             

            ...
            <h:form>
                 <a4j:push id="pushJms" address="pushTopicsContext">
                      <a4j:ajax event="dataavailable" render="testsSimplyTable" />
                 </a4j:push>
            </h:form>
            ...
            <h:form>
                 <h:panelGrid />
                      <a4j:commandLink value="Main" action="#{userBean.change('Main')}" execute=@form render="generalPanel" />
                      <a4j:commandLink value="Test" action="#{userBean.change('Test')}" execute=@form render="generalPanel" />
                 </h:panelGrid>
            </h:form>
            ...
            

             

            In this case, both a4j:commandLink components don't work while the a4j:push component is waiting for a dataavailable ajax event

            • 3. Re: Add FacesMessage from java thread
              bleathem

              In that last code sample it looks like you might be trying to do ajax updates across multiple forms.  This is a currently known limitation of JSF:

              https://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-790

               

              Try putting your push component in the same JSF form.