4 Replies Latest reply on Jan 18, 2008 4:42 AM by Florian Marwede

    Please help with the a4j:push component

    Julien Martin Expert

      Hello,
      I tried to reproduce the example given for the a4j:push without success. Here is my code:

      The managed bean (request-scoped):

      package pack;
      
      import java.util.Date;
      import java.util.EventListener;
      import java.util.EventObject;
      import org.ajax4jsf.event.PushEventListener;
      
      
      /**
       *
       * @author Administrator
       */
      public class TheBean {
      
       private Date date;
       private PushEventListener listener= new MyPushEventListener();
      
       public TheBean() {
       }
      
       public void addListener(EventListener listener) {
       synchronized (listener) {
       if (this.listener != listener) {
       this.listener = (PushEventListener) listener;
       }
       }
       }
      
       public Date getDate() {
       return new Date();
       }
      
       public void push() {
       synchronized (this.listener) {
       this.listener.onEvent(new EventObject(this));
       }
       }
      }
      
      
      


      The push event listener:

      package pack;
      
      import java.util.EventObject;
      import org.ajax4jsf.event.PushEventListener;
      
      public class MyPushEventListener implements PushEventListener {
      
       public void onEvent(EventObject evt) {
       System.out.println(evt.getSource());
       TheBean b = (TheBean) evt.getSource();
       b.getDate();
       System.out.println(b.getDate());
       }
      }
      
      


      The jsp that pushes:

       <f:view>
       <a4j:status startText="in progress" stopText="done"/>
      
       <h:form>
       <a4j:region>
       <a4j:commandButton value="Push!!" action="#{theBean.push}" ajaxSingle="true"/>
       <a4j:log level="ALL" popup="false" hotkey="L"/>
       </a4j:region>
       </h:form>
       </f:view>
      


      The receiver with the a4j:push:

       <f:view>
       <a4j:status startText="in progress" stopText="done"/>
      
       <a4j:form>
       <rich:message for="date" passedLabel="ok" ajaxRendered="true" showDetail="true"/>
       <a4j:region>
       <a4j:push reRender="date" eventProducer="#{theBean.addListener}" interval="100"/>
       </a4j:region>
       <a4j:outputPanel id="date" ajaxRendered="true">
       <h:outputText value="#{theBean.date}">
       <f:convertDateTime type="time"/>
       </h:outputText>
       </a4j:outputPanel>
       <a4j:log level="ALL" popup="false" hotkey="L"/>
       </a4j:form>
       </f:view>
      


      Here is the behavior that I have: When I press the pusher button the onEvent method does get called but the date is not reRendered. Can anyone please tell me why?

      What I am getting wrong?

      Thanks in advance,

      Julien.

        • 1. Re: Please help with the a4j:push component
          Florian Marwede Expert

          Got the same problem. Firefox, Tomcat 5.5, Richfaces 3.1.3 RC4, Facelets.

          • 2. Re: Please help with the a4j:push component
            Julien Martin Expert

            thanks for your input I have added a post on the dev forum.

            • 3. Re: Please help with the a4j:push component
              Florian Marwede Expert

              Thanks. I created a bug for that. See post in developer forum.

              • 4. Re: Please help with the a4j:push component
                Florian Marwede Expert

                a4j:push is working know in firefox, too.

                This is great but one thing I don't understand: How to write an own EventListener that is really called?

                With that code it can't work, because the reference to my own listener is overwritten the first time addListener is called:

                private PushEventListener listener = new MyPushEventListener();
                
                public void addListener(EventListener listener) {
                 synchronized (listener) {
                 if (this.listener != listener) {
                 this.listener = (PushEventListener) listener;
                 }
                 }
                }
                
                public void push() {
                 synchronized (this.listener) {
                 this.listener.onEvent(new EventObject(this));
                 }
                }
                


                So I tried to use this code but I'm wondering if it is intended from developer side:

                private PushEventListener myListener = new MyPushEventListener();
                
                public MyBean() {
                 addListener(myListener);
                }
                
                public void addListener(EventListener listener) {
                 synchronized (listener) {
                 if (this.listener != listener) {
                 this.listener = (PushEventListener) listener;
                 }
                 }
                }
                
                public void push() {
                 synchronized (this.listener) {
                 this.listener.onEvent(new EventObject(this));
                 }
                
                 synchronized (this.myListener) {
                 this.myListener.onEvent(new EventObject(this));
                 }
                }
                
                


                But now I have two EventListeners - how can I replace the standard event listener bound at the a4j:push?