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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用JSF和PrimeFaces进行自适应轮询相关的知识,希望对你有一定的参考价值。

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

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

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

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

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

答案

如果你给它一个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更新后将其更新为您想要的任何内容。

另一答案

解决了这里:

我面临着类似的行为,只需在bean中设置值并将轮询嵌入到面板中即可解决,例如在每次轮询之后都可以更新。 Otherwises,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("	 > Attennding "+ callNumber + "° call on  thread " + Thread.currentThread().getId() + ". Sleeping ..." );
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    LOG.info("		 E R R O R ! ! ! 	 > Attennding "+ callNumber + "° call on  thread " + Thread.currentThread().getId() );
                }
                //Later on we will try to adapt dynamically the interval.
            } else  {
                LOG.info("	 > 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

恕我直言;不要玩完成和工作的东西。

以上是关于使用JSF和PrimeFaces进行自适应轮询的主要内容,如果未能解决你的问题,请参考以下文章

使用 JSF2.0 和 Primefaces 3.4 进行导航

JSF / PrimeFaces - 如何显示自定义验证器的消息,但不显示同一组件上的必需验证

在重置JSF + PrimeFaces中的表单之前,检查一些必需的输入字段

JSF 2 PrimeFaces 安装

JSF、RichFaces、PrimeFaces 和 IceFaces 之间的区别

在 JSF 中使用 Primefaces 和 Bootstrap