4 Replies Latest reply on Jan 31, 2011 5:50 AM by topfan

    problem with a4j:push--refresh only if i reload browser

    topfan Newbie

      I've got exactly the same code of exadel live demo:

       

      http://livedemo.exadel.com/richfaces-demo/richfaces/push.jsf?tab=usage&cid=314524

       

       

      the code:

       

       

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

      <html xmlns="http://www.w3.org/1999/xhtml"

          xmlns:ui="http://java.sun.com/jsf/facelets"

          xmlns:h="http://java.sun.com/jsf/html"

          xmlns:f="http://java.sun.com/jsf/core"

          xmlns:rich="http://richfaces.org/rich"

          xmlns:a4j="http://richfaces.org/a4j">

      <ui:composition>

          <h:form>

              <a4j:push interval="1000" eventProducer="#{push.addListener}"

                  reRender="push,out" enabled="#{push.enabled}" id="push"/>

              <rich:panel>

              <f:facet name="header">

                  <h:outputText value="Push Example"/>

              </f:facet>

              <h:panelGrid columns="1" id="out">

                  <h:outputText rendered="#{!push.enabled}" value="Press Start to run push example"/>

                  <h:panelGroup rendered="#{push.enabled}">

                      <h:outputText value="Generated UUID:"/>

                      <h:outputText value="#{push.uuid}"/>

                  </h:panelGroup>

                  <h:panelGroup>

                      <a4j:commandButton value="Start" action="#{push.start}"

                          ajaxSingle="true" rendered="#{!push.enabled}" reRender="push, out"/>

                      <a4j:commandButton value="Stop" action="#{push.stop}"

                          ajaxSingle="true" rendered="#{push.enabled}" reRender="push, out"/>

                  </h:panelGroup>

              </h:panelGrid>

              </rich:panel>

          </h:form>

      </ui:composition>

      </html>

       

      The "Bean":

       

      package pagecode;

       

      import java.util.Date;

      import java.util.EventListener;

      import java.util.EventObject;

      import java.util.UUID;

       

      import org.ajax4jsf.event.PushEventListener;

       

      public class Push extends PageCodeBase implements Runnable {

       

          private String uuid = "";

       

          private boolean enabled = false;

       

          private Date startDate;

       

          PushEventListener listener;

       

          private Thread thread;

       

          // private int eventsFired counter;

       

          public void addListener(EventListener listener) {

              synchronized (listener) {

                  if (this.listener != listener) {

                      this.listener = (PushEventListener) listener;

                  }

              }

          }

       

          public void run() {

              while (thread != null) {

                  try {

                      if (((new Date()).getTime() - startDate.getTime()) >= 60000) {

                          stop();

                      }

                      uuid = UUID.randomUUID().toString();

                      if ( listener != null ) {

                          listener.onEvent(new EventObject(this));

                      }

                      Thread.sleep(10000);

                  } catch (InterruptedException e) {

                      // TODO Auto-generated catch block

                      e.printStackTrace();

                  }

              }

          }

       

          public String getUuid() {

              return uuid;

          }

       

          public void start() {

              if (thread == null) {

                  thread = new Thread(this);

                  thread.setDaemon(true);

                  thread.start();

                  setStartDate(new Date());

                  setEnabled(true);

              }

          }

       

          public void stop() {

              if (thread != null) {

                  // thread.stop();

                  setStartDate(null);

                  setEnabled(false);

                  thread = null;

              }

          }

       

          public Thread getThread() {

              return thread;

          }

       

          public boolean isEnabled() {

              return enabled;

          }

       

          public void setEnabled(boolean enabled) {

              this.enabled = enabled;

          }

       

          public Date getStartDate() {

              return startDate;

          }

       

          public void setStartDate(Date startDate) {

              this.startDate = startDate;

          }

      }

       

       

      It work but not refresh the browser, only manually.

       

      What can i do to make it work???

       

      Thanks

       

      topfan