As of version 1.1.7 & 1.2.1 jZeno supports server push (also known as Reverse Ajax, or Comet). This allows you to trigger an update of specific user sessions from the server.
In order to use this functionality you must add a CometComponent to your component tree. On this component you can configure an event handler (as ever via the property actionCommand) to be called when an event is triggered from a different session, messaging threads or any other part of your application that is not directly a GUI interaction on the current user's session (see below on how to trigger comet events).
You are only allowed to add a single instance of CometComponent to your component tree. An exception will be thrown if you add multiple instances.
To trigger an event to all user sessions that have a CometComponent in their component tree you must use CometManager.getInstance().doEvent(myEvent). The event object must implement net.sf.jzeno.echo.components.comet.CometEvent.
Filtering CometEvents for Specific User Sessions
CometComponent also supports the registration of an event filter (setEventFilter). This filter should determine if a given comet event should trigger an event handler on the current user's session. You must implement the interface net.sf.jzeno.echo.components.comet.CometEventFilter, which has a single method filter() that must return true to allow a given CometEvent to trigger an update.
If you do not configure an event filter, all events will trigger your CometComponent's event handler.
You should be aware that in the implementation of the event filter, you are not allowed to access any of the support classes (EchoSupport, ServletSupport, SecuritySupport, etc...) as the filter is not executed on a request thread of the web container. It is advisable to create both the event filter and the event object with all of the necessary information (as instance variables) to make a decission in the filter() method.
The CometComponent uses DWR for implementing it's functionality. Specifically this means that the prefered application server (for > 100 client sessions) is jetty, due to it's support for continuations. On other application servers, any user session that has a CometComponent in it's component tree, will consume a single request thread for as long as that component is part of the screen (the size of this thread pool is configurable in the application server).
When an event is triggered (through doEvent()), and the event filter allows the event for a given user session, then the configured event handler is executed. In this method you should retrieve the list of pending CometEvents in order to provide an adequat response by calling getPendingEvents(). After a call to getPendingEvents(), the list of pending events is empty. The list of pending events will contains 0,1 or multiple comet events, and your event handler should react correspondingly.
A complete code example :
package net.sf.jzeno.echo.screen.demopanel;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import net.sf.jzeno.echo.EchoSupport;
import net.sf.jzeno.echo.components.Screen;
import net.sf.jzeno.echo.components.comet.CometComponent;
import net.sf.jzeno.echo.components.comet.CometEvent;
import net.sf.jzeno.echo.components.comet.CometManager;
import net.sf.jzeno.echo.databinding.DynaHtmlContainer;
import org.apache.log4j.Logger;
import echopoint.HtmlContainer;
public class CometComponentDemo extends Screen implements Runnable {
private CometComponent cometComponent;
public CometComponentDemoPanel() {
cometComponent = new CometComponent();
cometComponent.setActionCommand("doit");
add(cometComponent);
new Thread(this).start();
}
public void doit() {
List events = cometComponent.getPendingEvents();
for (Iterator iterator = events.iterator(); iterator.hasNext();) {
TestEvent event = (TestEvent) iterator.next();
EchoSupport.addMessage(event.getMessage());
}
}
public void run() {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(3000);
log.info("Triggering event...");
CometManager.getInstance().doEvent(new TestEvent(
"Event that was triggered from the server."));
} catch (InterruptedException e) {
}
}
}
public static class TestEvent implements CometEvent {
private String message;
public TestEvent(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
}
}
Some More Tips
- In order to use server push support, you must include both dwr.jar in WEB-INF/lib, and dwr.xml in WEB-INF, as is the case in the template application.
- Comet is only supported in IE 6 or later. (Needless to say no known issues exist on firefox :-))
| < Prev | Next > |
|---|





