CometD 2 Java Server Services Integration
CometD 2 Java Server Services Integration
There are several ways to integrate your Bayeux services into your web application.
All of these ways are complicated by the fact that the BayeuxServer object is created by a servlet, and there is no easy way to detect, in general, when the BayeuxServer object has been created.
Integration via Configuration Servlet
The simplest way to initialize your web application with your services is to use a configuration servlet.
This configuration servlet will have no URL mapping, because its only scope is to initialize (or "wire" together) your services for your web application to work properly.
Following you can find a sample web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>cometd</servlet-name>
<servlet-class>org.cometd.server.CometdServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cometd</servlet-name>
<url-pattern>/cometd/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>configuration</servlet-name>
<servlet-class>com.acme.cometd.ConfigurationServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
</web-app>
Note how we specified <load-on-startup> to be 1 for the CometD servlet (so that the Bayeux object gets created and put in the ServletContext), and to be 2 for the configuration servlet, so that it will be initialized only after the CometD servlet has been initialized and hence the BayeuxServer object be available.
This is the code for the ConfigurationServlet:
public class ConfigurationServlet extends GenericServlet
{
public void init() throws ServletException
{
// Grab the Bayeux object
BayeuxServer bayeux = (BayeuxServer)getServletContext().getAttribute(BayeuxServer.ATTRIBUTE);
new EchoService(bayeux);
// Create other services here
// This is also the place where you can configure the Bayeux object
// by adding extensions or specifying a SecurityPolicy
}
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException
{
throw new ServletException();
}
}
See here about the EchoService
Integration via Configuration Listener
Instead of using a configuration servlet, it is possible to use a configuration listener, by writing a class that implements ServletContextAttributeListener.
Following you can find the web.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>cometd</servlet-name>
<servlet-class>org.cometd.server.CometdServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cometd</servlet-name>
<url-pattern>/cometd/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>com.acme.cometd.BayeuxInitializer</listener-class>
</listener>
</web-app>
This is the code for the BayeuxInitializer:
public class BayeuxInitializer implements ServletContextAttributeListener
{
public void attributeAdded(ServletContextAttributeEvent event)
{
if (Bayeux.ATTRIBUTE.equals(event.getName()))
{
// Grab the Bayeux object
BayeuxServer bayeux = (BayeuxServer)event.getValue();
new EchoService(bayeux);
// Create other services here
// This is also the place where you can configure the Bayeux object
// by adding extensions or specifying a SecurityPolicy
}
}
public void attributeRemoved(ServletContextAttributeEvent event)
{
}
public void attributeReplaced(ServletContextAttributeEvent event)
{
}
}
Integration of Annotated Services
If you prefer annotated services, you still have to integrate them into your web application.
The procedure is very similar to the procedures above, but it requires usage of the annotation processor in order to process the annotations in your services.
For example, the ConfigurationServlet will become:
public class ConfigurationServlet extends GenericServlet
{
private final List<Object> services = new ArrayList<Object>();
private ServerAnnotationProcessor processor;
public void init() throws ServletException
{
// Grab the BayeuxServer object
BayeuxServer bayeux = (BayeuxServer)getServletContext().getAttribute(BayeuxServer.ATTRIBUTE);
// Create the annotation processor
processor = new ServerAnnotationProcessor(bayeux);
// Create your annotated service instance and process it
Object service = new EchoService();
processor.process(service);
services.add(service);
// Create other services here
// This is also the place where you can configure the Bayeux object
// by adding extensions or specifying a SecurityPolicy
}
public void destroy() throws ServletException
{
// Deprocess the services that have been created
for (Object service : services)
processor.deprocess(service);
}
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException
{
throw new ServletException();
}
}
Integration of Annotated Services via AnnotationCometdServlet
The org.cometd.java.annotation.AnnotationCometdServlet allows to specify a comma separared list of class names to instantiate and process using a ServerAnnotationProcessor.
This is a sample web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>cometd</servlet-name>
<servlet-class>org.cometd.java.annotation.AnnotationCometdServlet</servlet-class>
<init-param>
<param-name>services</param-name>
<param-value>com.acme.cometd.FooService, com.acme.cometd.BarService</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>cometd</servlet-name>
<url-pattern>/cometd/*</url-pattern>
</servlet-mapping>
</web-app>
In this example, the AnnotationCometdServlet will instantiate and process the annotations of one object of class com.acme.cometd.FooService and of one object of class com.acme.cometd.BarService.
The services created will be deprocessed when AnnotationCometdServlet is destroyed.
- Printer-friendly version
- Login to post comments