Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The following example is the simplest case. A sports score is updated in the web browser every two seconds using an ICEfaces push update.</p> <p>The method ScoreManagerBean.postConstruct() is called after the managed bean is constructed for a new session. This method starts a thread that calls PortableRenderer.render(sessionId) every two seconds, causing the new score to be pushed to the web browser. In a real application, the push update would be run in some kind of callback function after receiving a message/trigger that data in the application has changed.</p> <p>Exactly the same concept is used for more complex applications such as news-tickers and ebay-style auction sites.</p> <p>Here is the managed bean class ScoreManagerBean.java:</p> <pre><code>import javax.annotation.PostConstruct; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import javax.faces.context.FacesContext; import javax.servlet.http.HttpSession; import java.util.Random; import org.icefaces.application.PortableRenderer; import org.icefaces.application.PushRenderer; @ManagedBean(name = "scoreManager") @SessionScoped public class ScoreManagerBean { private int m_nPointsA = 0; private int m_nPointsB = 0; private PortableRenderer m_renderer; private String m_sessionId; @PostConstruct public void postConstruct() { FacesContext facesContext = FacesContext.getCurrentInstance(); HttpSession session; session = (HttpSession)facesContext.getExternalContext().getSession(false); m_sessionId = session.getId(); PushRenderer.addCurrentSession(m_sessionId); m_renderer = PushRenderer.getPortableRenderer(facesContext); new BidThread().start(); } public String getCurrentScore() { return m_nPointsA + "-" + m_nPointsB; } private class BidThread extends Thread { public void run() { Random r = new Random(); for (int i = 0; i &lt; 1000; i++) { try { sleep(2000); System.out.println("Score has changed"); if (r.nextInt() % 2 == 0) m_nPointsA++; else m_nPointsB++; m_renderer.render(m_sessionId); } catch (Exception e) { e.printStackTrace(); } } } } } </code></pre> <p>and the web page pushexample.xhtml that displays the current score:</p> <pre><code>&lt;html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:icecore="http://www.icefaces.org/icefaces/core" xmlns:ice="http://www.icesoft.com/icefaces/component" xmlns:ui="http://java.sun.com/jsf/facelets"&gt; &lt;h:head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/&gt; &lt;/h:head&gt; &lt;h:body&gt; &lt;ice:outputText value="Current score: #{scoreManager.currentScore}"/&gt; &lt;/h:body&gt; &lt;/html&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload