Oracle GlassFish Server 3.0.1 Application Development Guide

Bayeux Protocol

The Bayeux protocol, often referred to as Cometd, greatly simplifies the use of Comet. No server-side coding is needed for servers such as GlassFish Server that support the Bayeux protocol. Just enable Comet and the Bayeux protocol, then write and deploy the client as described in the following tasks:

Enabling Comet

Before running a Comet-enabled application, you need to enable Comet in the HTTP listener for your application by setting a special attribute in the associated protocol configuration. The following example shows the asadmin set command that adds this attribute:

asadmin set server-config.network-config.protocols.protocol.http-1.http.comet-support-enabled="true"

Substitute the name of the protocol for http-1.

ProcedureTo Configure the web.xml File

To enable the Bayeux protocol on the GlassFish Server, you must reference the CometdServlet in your web application's web.xml file. In addition, if your web application includes a servlet, set the load-on-startup value for your servlet to 0 (zero) so that it will not load until the client makes a request to it.

  1. Open the web.xml file for your web application in a text editor.

  2. Add the following XML code to the web.xml file:

    <servlet>
       <servlet-name>Grizzly Cometd Servlet</servlet-name>
       <servlet-class>
          com.sun.grizzly.cometd.servlet.CometdServlet
       </servlet-class>
       <init-param>
          <description>
             expirationDelay is the long delay before a request is
             resumed. -1 means never.
          </description>
          <param-name>expirationDelay</param-name>
          <param-value>-1</param-value>
       </init-param>
       <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
       <servlet-name>Grizzly Cometd Servlet</servlet-name>
       <url-pattern>/cometd/*</url-pattern>
    </servlet-mapping>

    Note that the load-on-startup value for the CometdServlet is 1.

  3. If your web application includes a servlet, set the load-on-startup value to 0 for your servlet (not the CometdServlet) as follows:

    <servlet>
       ...
       <load-on-startup>0</load-on-startup>
    </servlet>
  4. Save the web.xml file.

ProcedureTo Write, Deploy, and Run the Client

The examples in this task are taken from the example chat application that is posted and discussed at http://weblogs.java.net/blog/jfarcand/archive/2007/02/gcometd_introdu_1.html.

  1. Add script tags to the HTML page. For example:

    <script type="text/javascript" src="chat.js"></script>
  2. In the script, call the needed libraries. For example:

    dojo.require("dojo.io.cometd");
  3. In the script, use publish and subscribe methods to send and receive messages. For example:

    cometd.subscribe("/chat/demo", false, room, "_chat");
    cometd.publish("/chat/demo", { user: room._username, chat: text});
  4. Deploy the web application as you would any other web application. For example:


    asadmin deploy cometd-example.war
  5. Run the application as you would any other web application.

    The context root for the example chat application is /cometd and the HTML page is index.html. So the URL might look like this:


    http://localhost:8080/cometd/index.html
See Also

For more information about deployment in the GlassFish Server, see the Oracle GlassFish Server 3.0.1 Application Deployment Guide.

For more information about the Bayeux protocol, see Bayeux Protocol.

For more information about the Dojo toolkit, see http://dojotoolkit.org/.

For information about pushing data from an external component such as an EJB module, see the example at http://blogs.sun.com/swchan/entry/java_api_for_cometd. Using this Grizzly Java API for Cometd makes your web application non-portable. Running your application on a server that doesn't support Grizzly Comet will not work.

For information about REpresentational State Transfer (RESTful) web services and Comet, see RESTful Web Services and Comet.