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 to Bayeux protocol? Perhaps something simple which reads messages off a topic and simply sends them on to a browser?
Perhaps an even simpler example would be to show how one could enter random words on a command line (simple readLine off the console) and push that to the browser.
Posted by falcon on July 01, 2008 at 08:40 PM PDT #
Posted by Jean-Francois Arcand's Blog on July 17, 2008 at 12:22 PM PDT #
Posted by Jean-Francois Arcand's Blog on July 24, 2008 at 04:26 PM PDT #
Posted by Jean-Francois Arcand's Blog on November 11, 2008 at 09:27 AM PST #
I've been trying to use this example to publish a message, but the published message is not being pushed directly to subscribed clients. However, if I publish a message from one of those clients (triggering a new request) the message I published on the server is then displayed in all clients.
I'd appreciate any suggestions on how to fix this. I'd like to use Grizzly to implement a cometd app but I'm not having much luck so far. I'm using the Glassfish v3 prelude version.
This sounds similar to the problem I read about here:
http://www.nabble.com/Problem-with-Bayeux-Servlet-Client-in-GlassFish-V3-td20810461.html
where Jean-Francois suggesting use of the deliverResponse.setFinished() method but recent versions of DeliverResponse don't have that method.
Thanks,
Matt
Posted by MattR on December 04, 2008 at 10:21 PM PST #
Shing Wai,
I would like to use the DeliverResponse class in the latest version of
grizzly, but v2ur2 seems to come with the 1.0.11 version. Is there any way
I can upgrade my GlassFish v2ur2 with a later version of Grizzly. My app requires clustering / HA, and it has been indicated that GlassFish v3 will not support clustering until Spring 2009, so what should I do?
Thank you,
Anthony
Posted by Anthony on January 02, 2009 at 08:19 AM PST #
Shing Wai,
Can you let me know where I can get the source code for the DeliverResponse.setFinished() method? We are having an issue returning data from the server to the client using grizzly-1.0.22, and would like to know what this method does.
Thanks,
Anthony
Posted by Anthony on January 16, 2009 at 03:13 PM PST #
There is a change in Grizzly and we have just updated the sample. Basically, one need to lookup the context by channel name. Note that there is no need to invoke DeliverRes.setFinished(). This will work once we have Grizzly 1.9.5 integrated to GlassFish v3 (probably this week).
Posted by Shing Wai Chan on January 26, 2009 at 03:04 PM PST #
Posted by Jean-Francois Arcand's Blog on February 03, 2009 at 08:10 AM PST #