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

    Add FacesMessage from java thread

    Miguel Angel Fernandez Diaz Newbie

      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