TOC PREV NEXT INDEX




Enhancing the TimeZoneBean


Now that we've integrated ICEfaces, the work to make the clocks tick is done in the bean. To make the clock tick every second, we are going to create our own thread and have it send updates at a set interval. We also need to add a bit of ICEfaces-specific code to force a JSF render pass after the clock data has been updated. For timezone3, the following changes are made to the TimeZoneBean.java file.

1. First we add some imports to support the new ICEfaces features:
import com.icesoft.faces.webapp.xmlhttp.PersistentFacesState;
 
2. Then we make the bean implement runnable so we can use it with a thread:
public class TimeZoneBean implements Runnable {
 
3. A polling interval is added as a bean property:
/**
 
* Time thread sleeps between polls.
 
*/
 
private int pollInterval = 500;
 
4. The constructor instantiates a thread and starts it:
/**
 
* Constructor, initializes time zones and starts thread which
 
* updates the time.
 
*/
 
public TimeZoneBean() {
 
		init();
 
		Thread ticker = new Thread(this);
 
		ticker.start();
 
	}
 
5. Then we add a run method to implement the Runnable interface. When the thread runs, it gets a reference to the PersistentFacesState from the HttpSession. This state can be used to initiate a render pass, which triggers the dynamic update to the client. The bean sleeps for the specified polling interval in between calls to render.
public void run() {
 
		try {
 
				 while (0 != pollInterval) {
 
						PersistentFacesState state = PersistentFacesState.getInstance();
 

 
						Thread.sleep(pollInterval);
 
						if (null != state)  {
 
								state.render();
 
						} else {
 
								System.out.println("null PersistentFacesState");
 
								break;
 
						}
 
				}
 
		} catch (Exception e)  {
 
				System.out.println(e);
 
		}
 
}
 


Copyright 2005. ICEsoft Technologies, Inc.
http://www.icesoft.com

TOC PREV NEXT INDEX