使用JSF和PrimeFaces进行自适应轮询

时间:2012-06-08 14:50:08

标签: java ajax jsf primefaces polling

为了刷新数据,我使用了一个素数poll

<p:poll id="myPoll" interval="#{controller.interval}"/>

并希望使用微调器控制更新间隔。

<p:spinner  value="#{controller.interval}">
  <p:ajax process="@this" update="myPoll"/>
</p:spinner>

bean属性的值按预期更新,但轮询间隔不是。轮询由生成的脚本执行,该脚本包括间隔值。如何强制poll重新生成更新脚本以包含新值而不提交/重新整理整个表单?

2 个答案:

答案 0 :(得分:4)

如果您给它widgetVar,那么您可以使用javascript操作它。

widget_form_j_idt19.stop();
widget_form_j_idt19.timer = 1;
widget_form_j_idt19.cfg.frequency = 1;
widget_form_j_idt19.start();

只需将其放入脚本中,您就可以在ajax更新后将其更新为您想要的任何内容。

答案 1 :(得分:3)

在这里解决:

我面临着类似的行为,只需在Bean中设置值并将调查嵌入到面板中即可解决,该面板可以在每次调查后进行更新。否则,poll只是js代码,不会添加到DOM中。否则,您可能会在回调参数上执行js并在客户端刷新值。

    <p:outputPanel id="infoPanel" >
    <h:panelGrid columns="1">
        <h1>TESTING POLLING</h1>
        <h:outputText id="ts" value="TS Update:#{testPollBean.timeStamp}"/>
        <h:outputText id="callNum" value="Call Number:#{testPollBean.callNumber}°"/>
        <h:outputText id="interval" value="Interval:#{testPollBean.interval} second"/>
        <h:outputText id="Stop" value="Stop:"#{testPollBean.stopPolling}"/>
    </h:panelGrid>  
</p:outputPanel>

<h:panelGrid id="panelPolling">
    <p:poll id="durrPolling" async="true" stop="#{testPollBean.stopPolling}"
        interval="#{testPollBean.interval}" 
        process="@this" partialSubmit="true" listener="#{testPollBean.listener}"
        update="panelPolling infoPanel" widgetVar="polling"
        autoStart="true" rendered="true"/>
</h:panelGrid>

@ViewScoped
@Named(value = "testPollBean")
public class TestPollBean implements Serializable {

    private static final long serialVersionUID = 1L;
    private static final Logger LOG = Logger.getLogger(TestPollBean.class);
    private Calendar calendar;
    private Date timeStamp;
    private Date nextCall;
    private int callNumber;
    private int interval;
    private boolean stopPolling;
    private boolean listenerBusy;

    @PostConstruct
    public void init() {
        this.calendar = Calendar.getInstance();
        this.timeStamp = calendar.getTime();
        this.nextCall = null;
        this.callNumber = 0;
        this.interval = 1;
        stopPolling = false;
        listenerBusy = false;
    }

    public void listener() {
        callNumber ++;
        LOG.info("-> Poll "+ callNumber + "° on thread " + Thread.currentThread().getId());
        if (!listenerBusy) {
            listenerBusy = true; // skip any other concurrence call 

            if (callNumber >= 30) {
                //stopPolling = true;
                this.interval = 30;
            }

            if (callNumber < 20) {
                this.timeStamp = calendar.getTime();
                calendar.add(Calendar.SECOND, interval);
                nextCall = calendar.getTime();

                LOG.info("\t > Attennding "+ callNumber + "° call on  thread " + Thread.currentThread().getId() + ". Sleeping ..." );
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    LOG.info("\t\t E R R O R ! ! ! \t > Attennding "+ callNumber + "° call on  thread " + Thread.currentThread().getId() );
                }
                //Later on we will try to adapt dynamically the interval.
            } else  {
                LOG.info("\t > Attennding inmediately "+ callNumber + "° call on  thread " + Thread.currentThread().getId() + "." );
            }
            LOG.info("<-  Poll "+ callNumber + "° on thread " + Thread.currentThread().getId());

            listenerBusy = false;
        } else {
            LOG.info("<- Listener on thread " + Thread.currentThread().getId() + " dismiss call " + callNumber + "° cause is already busy.");
        }
    }
    + Getters / Setters

恕我直言;不要玩弄已经完成且无法正常工作的东西。