5. Java Libraries
5.3. Client Library
We can use CometD client implementation in any J2EE application. It consists of one main class, org.cometd.client.BayeuxClient, which implements the org.cometd.bayeux.client.ClientSession interface.
5.3.1. Handshaking
BayeuxClient.handshake()
HttpClient httpClient = new HttpClient();
//Here set up Jetty's HttpClient, for example:
// httpClient.setMaxConnectionsPerAddress(2);
httpClient.start();
// Prepare the transport
Map<String, Object> options = new HashMap<String, Object>();
ClientTransport transport = LongPollingTransport.create(options, httpClient);
ClientSession client = new BayeuxClient("http://localhost:8080/cometd", transport);
5.3.2. Subscribing and Unsubscribing
client.getChannel(CHANNEL).subscribe(fooListener);
client.getChannel(CHANNEL).unsubscribe(fooListener);
5.3.3. Publishing
Map<String, Object> data1 = new HashMap<String, Object>();
// Fill in the data1 map object
client.getChannel("/game/table/1").publish(data1);
5.3.5. Client Transports
// Prepare the WebSocket transport
WebSocketClientFactory wsFactory = new WebSocketClientFactory();
wsFactory.start();
ClientTransport wsTransport = new WebSocketTransport(null, wsFactory, null);
// Prepare the HTTP transport
HttpClient httpClient = new HttpClient();
httpClient.start();
ClientTransport httpTransport = new LongPollingTransport(null, httpClient);
// Configure the BayeuxClient, with the websocket transport listed before the http transport
BayeuxClient client = new BayeuxClient("http://localhost:8080/cometd", wsTransport, httpTransport);
// Handshake
client.handshake();
5.4. Server Library
<servlet-name>cometd</servlet-name>
<servlet-class>org.cometd.server.CometdServlet</servlet-class>
The purpose for this is to perform cross-domain JavaScript requests.
<filter-name>cross-origin</filter-name>
<filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class>
Annotated Style
@org.cometd.annotation.Service("echoService")
public class EchoService
{
@javax.inject.Inject
private BayeuxServer bayeux;
}
Inherited Style
public class EchoService extends AbstractService
{
public EchoService(BayeuxServer bayeux)
{
super(bayeux, "echoService");
}
}
5.4.2.4 Services Integration with Spring
<bean id="otherService" class="com.acme..." />
5.6. Scalability Clustering with Oort
Any CometD server can become an Oort comet by configuring an instance of org.cometd.oort.Oort.
Oort clustering is not a high availability clustering solution: if one of the nodes crashes, then all the clients are disconnected and reconnect to other nodes (with a new handshake).