Friday May 02, 2008

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.
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