0 Replies Latest reply on Aug 22, 2014 4:36 PM by Sandeep Nanjegowda

    Need help with Push :  Value not updating in client

    Sandeep Nanjegowda Newbie

      Hi,

       

      I am trying the ajax push in RichFaces. Below are the files, value in pushdemo.xhtml is not getting updated. Anything that I am doing wrong?

      Web.xml

      <servlet>

              <servlet-name>Faces Servlet</servlet-name>

              <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>

              <load-on-startup>1</load-on-startup>

          </servlet>

          <servlet-mapping>

              <servlet-name>Faces Servlet</servlet-name>

              <url-pattern>*.xhtml</url-pattern>

          </servlet-mapping>

          <filter>

              <filter-name>PushFilter</filter-name>

              <filter-class>org.richfaces.webapp.PushFilter</filter-class>

              <async-supported>true</async-supported>

          </filter>

          <filter-mapping>

              <filter-name>PushFilter</filter-name>

              <servlet-name>Faces Servlet</servlet-name>

          </filter-mapping>

       

      pushdemo.xhtml

      <ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"

          xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:a4j="http://richfaces.org/a4j"

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

          <h:form>

              <a4j:push id="pushJms" address="pushTopicsContext">

                  <a4j:ajax event="dataavailable" render="uuid" />

              </a4j:push>

       

              <h:panelGrid columns="2">

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

                          ajaxSingle="true"  reRender="out"/>

                  <h:outputLabel value="New UUID:" />

                  <a4j:outputPanel id="uuid" layout="block">

                      #{push.uuid}

                  </a4j:outputPanel>

              </h:panelGrid>

          </h:form>

      </ui:composition>

       

       

      PushBean.java

      import java.util.Date;

      import java.util.EventListener;

      import java.util.EventObject;

      import java.util.UUID;

      import javax.faces.bean.*;

       

      //import org.ajax4jsf.event.PushEventListener;

      import org.richfaces.application.push.TopicKey;

      import org.richfaces.application.push.TopicsContext;

       

       

       

      @ManagedBean(name = "push")

      public class PushBean implements Runnable {

       

          private String uuid = "";

         

          private boolean enabled = false;

         

           public static final String PUSH_TOPICS_CONTEXT_TOPIC = "pushTopicsContext";

         

          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();

                      }

                      System.out.println("After 60000");

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

                      //listener.onEvent(new EventObject(this));

                      TopicKey topicKey = new TopicKey(PUSH_TOPICS_CONTEXT_TOPIC);

                      TopicsContext topicsContext = TopicsContext.lookup();

                      topicsContext.publish(topicKey, "message");

                      System.out.println("Sent Message");

                      Thread.sleep(10000);

                      System.out.println("After 10000  Sleep");

                  } catch (Exception 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;

          }

      }

       

      Thanks
      Sandeep