Java API for Bayeux Protocol

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:

  1. asadmin start-domain domain1
  2. java -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:

    asadmin deploy grizzly-cometd-echo-1.7.3.2.war

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.

  • As usual, one can get a CometContext as follows:

        CometEngine engine = CometEngine.getEngine();
        CometContext context = engine.getCometContext(contextPath);

    In our case, the contextPath is "/cometd/cometd" where the first "/cometd" is context root of the grizzly-cometd-echo.

  • One construct a Bayeux response message by using classes in package com.sun.grizzly.cometd.bayeux. The classes that we need to use are DeliverResponse and Data. It is constructed as follows:

        Map map = new HashMap();
        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);

    Note that

    • "/service/echo" is the channel name of grizzly-cometd-echo sample.
    • If one is not using an updated version of GlassFish, then one may need to call 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.

    Then one can send the Bayeux message as follows:

        context.notify(deliverResponse);

How to run the test

  • Use browser A1 to access the grizzly sample application by http://localhost:8080/cometd. One can type a message on the text box and see that it is echoed through Bayeux protocol.
  • Repeat the above in browser A2.
  • Use browser B to access the cometd message application by http://localhost:8080/cometdmsgtest. One can type a message in the text box and see that it appears in browser A1 and browser A2.
Comments:

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 01:40 PM PDT #

[Trackback] Before I deep dive into the AsyncFilter API like I've proposed in part 3, I've decided to show how easy is to add...PHP to your Grizzly based application with support for the Bayeux protocol.

Posted by Jean-Francois Arcand's Blog on July 17, 2008 at 05:22 AM PDT #

[Trackback] There is a lot happening this summer with Grizzly. We have two new commiters, several new tutorials, a new lead and our community is growing. Happy Summer from the monster...

Posted by Jean-Francois Arcand's Blog on July 24, 2008 at 09:26 AM PDT #

[Trackback] Today JavaLobby is publishing an article about who does what in Comet, and how. Time to try GlassFish v3 Prelude to taste Comet!

Posted by Jean-Francois Arcand's Blog on November 11, 2008 at 01: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 02: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 12: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 07:13 AM 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 07:04 AM PST #

[Trackback] We got attacked by a community boost in January, which resulted in tons of contributions and improvements to the monster. This is for sure our biggest community release...Here comes 1.9.5!

Posted by Jean-Francois Arcand's Blog on February 03, 2009 at 12:10 AM PST #

Post a Comment:
Comments are closed for this entry.
About

Shing Wai Chan

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today