com.sun.grizzly.messagesbus
Class MessagesBus

java.lang.Object
  extended by javax.servlet.GenericServlet
      extended by javax.servlet.http.HttpServlet
          extended by com.sun.grizzly.messagesbus.MessagesBus
All Implemented Interfaces:
Serializable, Servlet, ServletConfig
Direct Known Subclasses:
JMakiMessagesBus

public class MessagesBus
extends HttpServlet

Servlet implementation of the Grizzly Comet Protocol (GCP). The GCP protocol is a very basic protocol that can be used by browser to share data, using the comet technique, between serveral clients without having to poll for it. The protocol is very simple. First, a client must subscribe to a topic:


 http://host:port/contextPath?
    subscribe=[topic name]&cometTechnique=[polling|log-polling|http-streaming]&message[text]
 
 Mandatory: subscribe and cometTechnique.
 

When issuing the URL above, the connection will be automatically suspended based on the cometTechnique specified. To share data between application, a browser just need to send the following request:


 http://host:port/contextPath?publish=[topic name]&message[text] 
 

The Servlet can be used as it is or extended to add extra features like filtering messages, security, login, etc. To use this Servlet, just add in your web.xml:


    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
      xmlns:j2ee="http://java.sun.com/xml/ns/j2ee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"
      xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
      <description>Grizzly Messages Bus Servlet</description>
      <display-name>Grizzly Messages Bus Servlet</display-name>
      <servlet>
        <description>MessagesBus</description>
        <servlet-name>MessagesBus</servlet-name>
        <servlet-class>com.sun.grizzly.messagesbus.MessagesBus</servlet-class>
        <load-on-startup>0</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>MessagesBus</servlet-name>
        <url-pattern>/mb</url-pattern>
      </servlet-mapping>
      <session-config>
        <session-timeout>25</session-timeout>
      </session-config>
    </web-app>
 

By default, a connection will be timed out after a expirationDelay. The default is 30 seconds, but this can be configured by adding, in web.xml:


   <init-param>
        <param-name>expirationDelay</param-name>
        <param-value>60000</param-value>
   </init-param>
 

The Grizzly Comet NotificationHandler can also be replaced by adding:


   <init-param>
        <param-name>notificationHandler</param-name>
        <param-value>com.foo.bar.ScriptFilterNotificationHandler</param-value>
   </init-param>
 

A request can also be automatically suspended when the request URI only contains the publish GCP action.


   <init-param>
        <param-name>suspendOnTheFly</param-name>
        <param-value>true</param-value>
   </init-param>
 

Author:
Jeanfrancois Arcand
See Also:
Serialized Form

Nested Class Summary
static class MessagesBus.CometType
          The Comet technique supported.
 
Constructor Summary
MessagesBus()
           
 
Method Summary
protected  CometContext createCometContext(String topic)
          Create a Grizzly Comet CometContext
protected  void doGet(HttpServletRequest req, HttpServletResponse res)
          
protected  void doPost(HttpServletRequest req, HttpServletResponse res)
          Basic implementation of the Grizzly Comet Protocol (GCP).
 long getExpirationDelay()
          Return the time a connection can stay idle.
 void init(ServletConfig config)
          
protected  void push(HttpServletRequest req, HttpServletResponse res)
          Inspect the request, looking for the GCP parameters publish and message.
 void setExpirationDelay(long expirationDelay)
          Set the maximum idle time a connection can stay suspended.
protected  MessagesBus.CometType suspend(HttpServletRequest req, HttpServletResponse res)
          Suspend the connection if the request contains the GCP suscribe and cometTechnique action.
 
Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doHead, doOptions, doPut, doTrace, getLastModified, service, service
 
Methods inherited from class javax.servlet.GenericServlet
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MessagesBus

public MessagesBus()
Method Detail

init

public void init(ServletConfig config)
          throws ServletException

Specified by:
init in interface Servlet
Overrides:
init in class GenericServlet
Throws:
ServletException

doGet

protected void doGet(HttpServletRequest req,
                     HttpServletResponse res)
              throws ServletException,
                     IOException

Overrides:
doGet in class HttpServlet
Throws:
ServletException
IOException

doPost

protected void doPost(HttpServletRequest req,
                      HttpServletResponse res)
               throws ServletException,
                      IOException
Basic implementation of the Grizzly Comet Protocol (GCP).

Overrides:
doPost in class HttpServlet
Parameters:
req - The request
res - The response
Throws:
ServletException
IOException

push

protected void push(HttpServletRequest req,
                    HttpServletResponse res)
             throws IOException
Inspect the request, looking for the GCP parameters publish and message. If those parameters are included, the message will be pushed to all subscriber of the publish's value.

Parameters:
req - The http request
res - The http response.
Throws:
IOException

suspend

protected MessagesBus.CometType suspend(HttpServletRequest req,
                                        HttpServletResponse res)
                                 throws UnsupportedEncodingException,
                                        IOException
Suspend the connection if the request contains the GCP suscribe and cometTechnique action.

Parameters:
req -
res -
Returns:
MessagesBus.CometType used to suspend the connection.
Throws:
UnsupportedEncodingException
IOException

createCometContext

protected final CometContext createCometContext(String topic)
Create a Grizzly Comet CometContext

Parameters:
topic - The name of the CometContext
Returns:
a cached or newly created CometContext

getExpirationDelay

public long getExpirationDelay()
Return the time a connection can stay idle.

Returns:

setExpirationDelay

public void setExpirationDelay(long expirationDelay)
Set the maximum idle time a connection can stay suspended.

Parameters:
expirationDelay -


Copyright © 2011 SUN Microsystems. All Rights Reserved.