The strategy explained in the response of @ PabloPéres-Aradros is a solution known as polling. This, the client consults the server from time to time. You must use setInterval
for a method to run in a time interval . It happens that JSF, by default, places certain parameters on the client to ensure the status of the view and thus avoid the CSRF attacks a>, so that using the method explained by Pablo is not "so simple" when using JSF when doing everything manually. In any case, JSF does support poll.
If you use pure JSF, it would be best to delegate the action to a component with <f:ajax>
:
<h:form id="idForm">
<!-- ... contenido ... -->
<!-- Nota: el componente DEBE ser visible, sino no se ejecutará. -->
<h:commandButton id="idBoton" action="#{bean.metodoParaRefrescar}">
<f:ajax render="idComponenteARefrescar" />
</h:commandButton>
<!-- ... contenido ... -->
</h:form>
And create a JavaScript function:
setInterval(function() {
document.getElementById("idForm:idBoton").click()
}, 3000); //cada 3 segundos, adáptalo a tus necesidades
If you use a library as PrimeFaces, consider that it already provides its own component to make poll (code adapted from the PrimeFaces examples page .
<h:form id="idForm">
<h:outputText id="txtContador" value="#{bean.numero}" />
<!--
Cada 3 segundos ejecutar una petición y ejecutar
de lado del servidor el método declarado en listener
-->
<p:poll interval="3" listener="#{bean.incrementar}"
update="txtContador" />
</h:form>
Consider this as a simple example. Your server-side method can execute the necessary database calls. Also, you should measure the response time of the server and update the page to set the value of your interval.
There is another strategy called push, which consists of the server executing the operations in time intervals and notifying the client that it must be updated. BalusC (guru in Java and JSF) explains how to use push in JSF in this answer , partially translated:
Up to the new JSF 2.3 <f:websocket>
( issue 1396 ), the standard JSF library does not offer facilities for this. Needed to go to third-party libraries for now:
The new JSF component 2.3 <f:websocket>
is widely based in <o:socket>
.
For the description of your problem, in my opinion, I would use polling. In any case, it is up to you to evaluate the alternatives and choose the most convenient one to use.