Class BayeuxClient

java.lang.Object
org.cometd.common.AbstractClientSession
org.cometd.client.BayeuxClient
All Implemented Interfaces:
Bayeux, ClientSession, Session, org.eclipse.jetty.util.component.Dumpable
Direct Known Subclasses:
OortComet

public class BayeuxClient extends AbstractClientSession implements Bayeux

BayeuxClient is the implementation of a client for the Bayeux protocol.

A BayeuxClient can receive/publish messages from/to a Bayeux server, and it is the counterpart in Java of the JavaScript library used in browsers (and as such it is ideal for Swing applications, load testing tools, etc.).

A BayeuxClient handshakes with a Bayeux server and then subscribes ClientSessionChannel.MessageListener to channels in order to receive messages, and may also publish messages to the Bayeux server.

BayeuxClient relies on pluggable transports for communication with the Bayeux server.

When the communication with the server is finished, the BayeuxClient can be disconnected from the Bayeux server.

Typical usage:

 // Setup Jetty's HttpClient.
 HttpClient httpClient = new HttpClient();
 httpClient.start();

 // Handshake
 String url = "http://localhost:8080/cometd";
 BayeuxClient client = new BayeuxClient(url, new JettyHttpClientTransport(null, httpClient));
 client.handshake();
 client.waitFor(1000, BayeuxClient.State.CONNECTED);

 // Subscription to channels
 ClientSessionChannel channel = client.getChannel("/foo");
 channel.subscribe((channel, message) -> {
     // Handle the message
 });

 // Publishing to channels
 Map<String, Object> data = new HashMap<>();
 data.put("bar", "baz");
 channel.publish(data);

 // Disconnecting
 client.disconnect();
 client.waitFor(1000, BayeuxClient.State.DISCONNECTED);