CometD 2 Java Server Configuration
CometD 2 Java Server Configuration
BayeuxServer and server transport parameters can be specified in web.xml as init parameters of the org.cometd.server.CometdServlet.
If the CometD servlet creates the BayeuxServer instance, the servlet init parameters will be passed to the BayeuxServer instance, which will in turn configure the server transports.
If you followed the primer, then Maven has configured the web.xml file for you, but here we will detail its configuration.
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.server.CometdServlet</servlet-class>
<init-param>
<param-name>timeout</param-name>
<param-value>60000</param-value>
</init-param>
<init-param>
<param-name>logLevel</param-name>
<param-value>3</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>cometd</servlet-name>
<url-pattern>/cometd/*</url-pattern>
</servlet-mapping>
</web-app>
The org.cometd.server.CometdServlet must be defined and mapped in web.xml, or otherwise the server will not be able to interpret the Bayeux protocol.
It is normally mapped on /cometd/*, but you can change the mapping url-pattern at your wish.
BayeuxServer Configuration
Here is the list of configuration init parameters accepted by the BayeuxServer implementation:
| Parameter Name | Default Value | Since | Parameter Description |
|---|---|---|---|
| logLevel | 0 | 2.0.0 | The log level; 0 = off, 1 = config, 2 = info, 3 = debug |
| transports | empty string | 2.2.0 | A comma separated list of ServerTransport implementation class names (that take a org.cometd.server.BayeuxServerImpl as only constructor parameter) to add to the default server transports |
| allowedTransports | empty string | 2.2.0 | A comma separated list of ServerTransports names allowed. If not specified, the default server transports are allowed |
| jsonContext | org.cometd.server.JettyJSONContextServer |
2.4.0 | The full qualified name of a class implementing org.cometd.common.JSONContext.Server. The class is loaded and instantiated using the default constructor. |
Server Transport Configuration
In CometD 2 the server transports are pluggable and can be configured using parameters that may have a prefix that specifies the transport that the parameter refers to.
For example, the parameter timeout has no prefix, and hence it is valid for all transports; the parameter long-polling.jsonp.timeout overrides the timeout parameter for the callback polling transport only, while ws.timeout overrides it for the websocket transport (see org.cometd.bayeux.Transport javadocs for details).
Here is the list of configuration init parameters accepted by server transports:
| Parameter Name | Default Value | Parameter Description |
|---|---|---|
| timeout | 30000 | The time, in milliseconds, that a server will wait for a message before responding to a long poll with an empty response |
| ws.timeout | 15000 | Like the timeout parameter, but for the websocket transport |
| interval | 0 | The time, in milliseconds, that specifies how long the client must wait between the end of one long poll requests and the start of the next |
| ws.interval | 2500 | Like the interval parameter but for the websocket transport |
| maxInterval | 10000 | The max period of time, in milliseconds, that the server will wait for a new long poll from a client before that client is considered invalid and is removed |
| ws.maxInterval | 15000 | Like the maxInterval parameter, but for the websocket transport |
| maxLazyTimeout | 5000 | The max period of time, in milliseconds, that the server will wait before delivering or publishing lazy messages |
| metaConnectDeliverOnly | false | Whether the transport should deliver the messages only via long poll (enables strict message ordering) |
| jsonDebug | false | Whether or not the full JSON input should be kept for debugging purposes |
| maxSessionsPerBrowser | 1 | The max number of sessions (tab/frames) allowed to long poll from the same browser; a negative value allows unlimited sessions (see also here) |
| allowMultiSessionsNoBrowser | false | Whether to allow multiple sessions (tab/frames) in case the browser cannot be detected (see also here) |
| multiSessionInterval | 2000 | The period of time, in milliseconds, that specifies the client normal polling period in case the server detects more sessions (tabs/frames) connected from the same browser than allowed by the maxSessionsPerBrowser parameter. A non-positive value means that additional sessions will be disconnected. |
| long-polling.json.metaConnectDeliverOnly | false | Whether or not delivery of messages should only happen via /meta/connect. Enabling this option allows for strict message ordering at the cost of a slightly more chattier protocol (because delivery via /meta/connect requires waking up the long poll). |
Advanced Configuration
If you are using Jetty 7, you may want to configure also the CrossOriginFilter.
This filter implements the Cross-Origin Resource Sharing specification, and allows recent browsers that implements it (as of November 2009, Firefox 3.5.x, Chrome 3.x and Safari 4.x) to perform cross-domain JavaScript requests (see also the transport section).
Here is an example of web.xml configuration for the CrossOriginFilter:
<?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.continuation.ContinuationCometdServlet</servlet-class>
<init-param>
<param-name>timeout</param-name>
<param-value>60000</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>cometd</servlet-name>
<url-pattern>/cometd/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>cross-origin</filter-name>
<filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>cross-origin</filter-name>
<url-pattern>/cometd/*</url-pattern>
</filter-mapping>
</web-app>
Refer to this document for the filter configuration.
Servlet 3 configuration
Refer to this FAQ.
- Printer-friendly version
- Login to post comments