5 Replies Latest reply on Dec 10, 2015 5:51 AM by Michal Petrov

    How to make a4j poll work in a popupPanel

    Saroj Padhy Novice

      Hi All,

       

      I have problems on updating a count dynamically in a popupPanel. I was making use of a4j:poll. But it doesn't seems to work.

      I am sharing the code snippets, let me know if something is wrong in the code.

       

      xhtml code:

      <a4j:status name="waitCount"

              onstart="#{rich:component('waitPanel')}.show();"

              onstop="#{rich:component('waitPanel')}.hide();" />

       

       

          <rich:popupPanel id="waitPanel" autosized="true" styleClass="body"

              modal="true" domElementAttachment="parent"

              style="text-align: center; background: white; border: none;"

              width="240" moveable="false" resizeable="false">

       

              <h:form id="waitform">

                  <a4j:poll id="rowcountpoll" interval="1000"

                      enabled="#{mgrBean.enablePoll}" limitRender="true"

                      render="waitform,rowcountpoll,rowscount,waitPanel">

                  </a4j:poll>

       

                  <h:outputLabel id="rowlabelmsg" style="font-size: 14px;"

                      value="Count : " for="rowcountpoll" />

       

                  <h:outputText id="rowscount" style="font-size: 14px;"

                      value="#{mgrBean.count}" />

                  <p />

       

              </h:form>

          </rich:popupPanel>

       

       

          <h:panelGrid columns="2" width="80%" id="grid">

              <h:form>

                  <h:outputText value="Run : " style="font-weight: bold;" />

       

                  <a4j:commandButton style="width:120px" id="control"

                      value="#{mgrBean.enablePoll?'Stop':'Start'} Polling"

                      status="waitCount" action="#{mgrBean.runPoll}"

                      render="grid,rowcountpoll,waitPanel,waitform">

                      <a4j:param name="polling" value="#{!mgrBean.enablePoll}"

                          assignTo="#{mgrBean.enablePoll}" />

                  </a4j:commandButton>

       

              </h:form>

          </h:panelGrid>

       

      Java code:

       

       

      private boolean enablePoll = false;

          private int count = 0;

       

       

          public String runPoll() {

       

                 if (enablePoll) {
         do {
         count += 10;
         try {
         System.out.println("count : " + count);
         Thread.sleep(3000);
         } catch (InterruptedException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
         }
         } while (count <= 100);
         enablePoll = false;
         }
         return null;

          }

       

          public boolean isEnablePoll() {

         return enablePoll;

          }

       

          public void setEnablePoll(boolean enablePoll) {

         this.enablePoll = enablePoll;

          }

       

          public String getCount() {

         return Integer.toString(count);

          }

       

          public void setCount(String count) {

         this.count = Integer.parseInt(count);

          }

       

      I am using following libraries:

      myfaces - 2.2.8

      richfaces - 4.5.1.Final

       

       

      Thanks,

      Saroj

        • 1. Re: How to make a4j poll work in a popupPanel
          Michal Petrov Master

          When you suspect the problem is in a certain component you should first check if removing the component helps, in this case it doesn't.

           

          The execution is single-threaded, so the server waits for the method to finish before sending a response (the response is what activates the poll). You would need to run the method asynchronously, but that's not possible if you're using CDI.

          • 2. Re: How to make a4j poll work in a popupPanel
            Saroj Padhy Novice

            Hi Michal,

             

            Thanks for the quick reply. How do I run the method asynchronously?

            I need to use the popupPanel and update only the count number. So, I thought a4j:poll will do that for me.

             

            If I start a Thread inside the action method, the popupPanel (status attribute) comes and goes soon, as it returns from the action method.

             

            I remember similar code was working in Richfaces 3.3.3 and after migration I tried it didn't work.

             

            I tried with following code with a jsFunction, but no success.

            <a4j:commandButton style="width:120px" id="control"

              value="#{mgrBean.enablePoll?'Stop':'Start'} Polling"

              action="#{mgrBean.doNothing}" oncomplete="actionSelection();">

              <a4j:param name="polling" value="#{!mgrBean.enablePoll}"

              assignTo="#{mgrBean.enablePoll}" />

              </a4j:commandButton>

             

              <a4j:jsFunction render="grid,rowcountpoll,waitPanel,waitform"

              name="actionSelection" limitRender="true" execute="@this"

              status="waitCount" action="#{mgrBean.runPoll}">

              </a4j:jsFunction>

             

             

            Thanks,

            Saroj

             

            Michal Petrov wrote:

             

            When you suspect the problem is in a certain component you should first check if removing the component helps, in this case it doesn't.

             

            The execution is single-threaded, so the server waits for the method to finish before sending a response (the response is what activates the poll). You would need to run the method asynchronously, but that's not possible if you're using CDI.

            • 3. Re: How to make a4j poll work in a popupPanel
              Michal Petrov Master

              If you're using CDI you can't (it's planned for CDI 2.0 but that's not out yet), if you're using EJB there's the @Asynchronous annotation.

              • 4. Re: How to make a4j poll work in a popupPanel
                Saroj Padhy Novice

                Hi again,

                 

                I am not using CDI nor EJB. I have a simple web application where I have to display count dynamically.

                 

                When user clicks a button, it has to start the count in a popupPanel and stop it when execution ends. So I think "status" attribute will be useful. And ajax rendering feature which makes only a part of page to be rendered.

                 

                But seems that a4j:poll feature is not really ajax, it should be independent of single-threaded actions. Otherwise, I might be wrong with the concept.

                 

                Thanks,

                Saroj

                • 5. Re: How to make a4j poll work in a popupPanel
                  Michal Petrov Master

                  Ajax is asynchronous with regards to the client-side JavaScript, but the JSF processing on server side is not asynchronous by design (and if you fire multiple requests they will be put in a queue).