Java API for Bayeux Protocol
Friday May 02, 2008
In Cometd environment, one communicates through Bayeux Protocol. The protocol is currently in 1.0 draft 1. GlassFish v3 has incorporated implementation of Bayeux from Grizzly. Jean Francois already has several good blogs on Cometd on Grizzly. In this blog, we are going to illustrate how to send a Bayeux message to a Cometd client by using Java API without writing any JSON code.
Basic set up
Download GlassFish v3 from GlassFish website.
And add the following property to your http-listener
in domain.xml as follows:
<property name="cometSupport" value="true"/>
Start the server by "one" of the following:
asadmin start-domain domain1java -jar glassfish-10.0-SNAPSHOT.jar
One also need a Cometd web application. In this blog, we will use the grizzly-cometd-echo sample. Just download the war file and deploy as follows:
Generate a Bayeux Message
In our example, we will generate a cometd message in a servlet,
TestServlet.java. The cometdmsgtest.war file and source codes are
available here.
In our case, the contextPath is " Note that
Then one can send the Bayeux message as follows:
How to run the test
asadmin deploy grizzly-cometd-echo-1.7.3.2.war
CometEngine engine = CometEngine.getEngine();
CometContext context = engine.getCometContext(contextPath);/cometd/cometd" where
the first "/cometd" is context root of the
grizzly-cometd-echo.
com.sun.grizzly.cometd.bayeux. The classes that
we need to use are DeliverResponse and
Data. It is constructed as follows:
Map
map.put(messageDataName, messageDataValue);
Data data = new Data();
data.setMapData(map);
DeliverResponse deliverResponse = new DeliverResponse();
deliverResponse.setChannel("/service/echo");
//deliverResponse.setClientId("");
deliverResponse.setData(data);
deliverResponse.setLast(true);
deliverResponse.setFollow(true);
/service/echo" is the channel name of grizzly-cometd-echo sample.
deliverResponse.setClientId("") to workaround a bug.
deliverResponse.setLast(true) indicates
that this is the last Bayeux message in this Http response.
deliverResponse.setFollow(true) indicates
that this is not the first Bayeux message in this Http
response. In our case, the previous message is
/meta/connect.
context.notify(deliverResponse);
http://localhost:8080/cometd. One can type a message on
the text box and see that it is echoed through Bayeux
protocol.
http://localhost:8080/cometdmsgtest. One can
type a message in the text box and see that it appears in
browser A1 and browser A2.











Could you show an example of linking a JMS topic t...
I've been trying to use this example to publi...
Shing Wai,
I would like to use the DeliverResponse...
Shing Wai,
Can you let me know where I can get the...