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);
Modifier and Type | Class and Description |
---|---|
static interface |
BayeuxClient.BackOffStrategy
A strategy to controls wait times of the retry attempts in case of heartbeat failures.
|
protected class |
BayeuxClient.BayeuxClientChannel
A channel scoped to this BayeuxClient.
|
static class |
BayeuxClient.Scheduler |
static class |
BayeuxClient.State
The states that a BayeuxClient may assume.
|
AbstractClientSession.AbstractSessionChannel
Bayeux.BayeuxListener, Bayeux.Validator
ClientSession.Extension, ClientSession.MessageListener
Modifier and Type | Field and Description |
---|---|
static String |
BACKOFF_INCREMENT_OPTION |
static String |
BAYEUX_VERSION |
static String |
MAX_BACKOFF_OPTION |
Constructor and Description |
---|
BayeuxClient(String url,
ClientTransport transport,
ClientTransport... transports)
Creates a BayeuxClient that will connect to the Bayeux server at the given URL
and with the given transport(s).
|
BayeuxClient(String url,
ScheduledExecutorService scheduler,
ClientTransport transport,
ClientTransport... transports)
Creates a BayeuxClient that will connect to the Bayeux server at the given URL,
with the given scheduler and with the given transport(s).
|
Modifier and Type | Method and Description |
---|---|
void |
abort()
Interrupts abruptly the communication with the Bayeux server.
|
void |
addTransportListener(TransportListener listener) |
void |
disconnect(ClientSession.MessageListener callback)
Disconnects this session, ending the link between the client and the server peers.
|
boolean |
disconnect(long timeout)
Performs a
disconnect and uses the given timeout
to wait for the disconnect to complete. |
protected void |
enqueueSend(Message.Mutable message) |
List<String> |
getAllowedTransports() |
BayeuxClient.BackOffStrategy |
getBackOffStrategy() |
HttpCookie |
getCookie(String name)
Retrieves the first cookie with the given name, if available.
|
CookieStore |
getCookieStore() |
String |
getId()
The clientId of the session.
|
Set<String> |
getKnownTransportNames() |
Object |
getOption(String qualifiedName) |
Set<String> |
getOptionNames() |
Map<String,Object> |
getOptions() |
protected BayeuxClient.State |
getState() |
ClientTransport |
getTransport() |
ClientTransport |
getTransport(String transport) |
String |
getURL() |
void |
handshake(ClientSession.MessageListener callback) |
BayeuxClient.State |
handshake(long waitMs)
Performs the handshake and waits at most the given time for the handshake to complete.
|
void |
handshake(Map<String,Object> fields,
ClientSession.MessageListener callback)
Initiates the bayeux protocol handshake with the server(s).
|
BayeuxClient.State |
handshake(Map<String,Object> template,
long waitMs)
Performs the handshake with the given template and waits at most the given time for the handshake to complete.
|
protected void |
initialize() |
boolean |
isConnected()
A connected session is a session where the link between the client and the server
has been established.
|
boolean |
isDisconnected() |
boolean |
isHandshook()
A handshook session is a session where the handshake has successfully completed
|
protected void |
messagesFailure(Throwable cause,
List<? extends Message> messages) |
protected AbstractClientSession.AbstractSessionChannel |
newChannel(ChannelId channelId) |
protected ChannelId |
newChannelId(String channelId) |
void |
onFailure(Throwable failure,
List<? extends Message> messages)
Deprecated.
use
addTransportListener(TransportListener) instead |
void |
onMessages(List<Message.Mutable> messages)
Deprecated.
use
addTransportListener(TransportListener) instead |
void |
onSending(List<? extends Message> messages)
Deprecated.
use
addTransportListener(TransportListener) instead |
protected void |
onTransportFailure(Message message,
ClientTransport.FailureInfo failureInfo,
ClientTransport.FailureHandler handler) |
protected void |
onTransportFailure(String oldTransportName,
String newTransportName,
Throwable failure) |
protected void |
processConnect(Message.Mutable connect) |
protected void |
processDisconnect(Message.Mutable disconnect) |
protected void |
processHandshake(Message.Mutable handshake) |
protected void |
processMessage(Message.Mutable message) |
void |
putCookie(HttpCookie cookie) |
void |
removeTransportListener(TransportListener listener) |
protected boolean |
scheduleConnect(long interval,
long backOff) |
protected boolean |
scheduleHandshake(long interval,
long backOff) |
protected void |
send(Message.Mutable message) |
protected void |
sendBatch() |
protected void |
sendConnect() |
protected void |
sendHandshake() |
protected void |
sendMessages(List<Message.Mutable> messages,
Promise<Boolean> promise) |
void |
setBackOffStrategy(BayeuxClient.BackOffStrategy backOffStrategy) |
void |
setOption(String qualifiedName,
Object value) |
protected void |
terminate(Throwable failure) |
String |
toString() |
boolean |
waitFor(long waitMs,
BayeuxClient.State state,
BayeuxClient.State... states)
Waits for this BayeuxClient to reach the given state(s) within the given time.
|
addExtension, batch, dump, endBatch, extendIncoming, extendOutgoing, getAttribute, getAttributeNames, getChannel, getChannel, getChannels, getExtensions, isBatching, newMessage, newMessageId, notifyCallback, notifyListeners, receive, registerCallback, registerSubscriber, remoteCall, removeAttribute, removeExtension, resetSubscriptions, setAttribute, startBatch, unregisterCallback, unregisterSubscriber
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
disconnect, handshake, handshake
public static final String BACKOFF_INCREMENT_OPTION
public static final String MAX_BACKOFF_OPTION
public static final String BAYEUX_VERSION
public BayeuxClient(String url, ClientTransport transport, ClientTransport... transports)
Creates a BayeuxClient that will connect to the Bayeux server at the given URL and with the given transport(s).
This constructor allocates a new scheduler
; it is recommended that
when creating a large number of BayeuxClients a shared scheduler is used.
url
- the Bayeux server URL to connect totransport
- the default (mandatory) transport to usetransports
- additional optional transports to use in case the default transport cannot be usedBayeuxClient(String, ScheduledExecutorService, ClientTransport, ClientTransport...)
public BayeuxClient(String url, ScheduledExecutorService scheduler, ClientTransport transport, ClientTransport... transports)
Creates a BayeuxClient that will connect to the Bayeux server at the given URL, with the given scheduler and with the given transport(s).
url
- the Bayeux server URL to connect toscheduler
- the scheduler to use for scheduling timed operationstransport
- the default (mandatory) transport to usetransports
- additional optional transports to use in case the default transport cannot be usedpublic String getURL()
public BayeuxClient.BackOffStrategy getBackOffStrategy()
public void setBackOffStrategy(BayeuxClient.BackOffStrategy backOffStrategy)
public CookieStore getCookieStore()
public HttpCookie getCookie(String name)
Retrieves the first cookie with the given name, if available.
Note that currently only HTTP transports support cookies.
name
- the cookie nameputCookie(HttpCookie)
public void putCookie(HttpCookie cookie)
public String getId()
Session
The clientId of the session.
This would more correctly be called a "sessionId", but for backwards compatibility with the Bayeux protocol, it is a field called "clientId" that identifies a session.
public boolean isHandshook()
Session
A handshook session is a session where the handshake has successfully completed
isHandshook
in interface Session
public boolean isConnected()
Session
A connected session is a session where the link between the client and the server has been established.
isConnected
in interface Session
Session.disconnect()
public boolean isDisconnected()
protected BayeuxClient.State getState()
public void addTransportListener(TransportListener listener)
listener
- the TransportListener
to addpublic void removeTransportListener(TransportListener listener)
listener
- the TransportListener
to removepublic void handshake(ClientSession.MessageListener callback)
callback
- the message listener to notify of the handshake resultpublic void handshake(Map<String,Object> fields, ClientSession.MessageListener callback)
ClientSession
Initiates the bayeux protocol handshake with the server(s).
The handshake initiated by this method is asynchronous and does not wait for the handshake response.
handshake
in interface ClientSession
fields
- additional fields to add to the handshake message.callback
- the message listener to notify of the handshake resultpublic BayeuxClient.State handshake(long waitMs)
Performs the handshake and waits at most the given time for the handshake to complete.
When this method returns, the handshake may have failed (for example because the Bayeux server denied it), so it is important to check the return value to know whether the handshake completed or not.
waitMs
- the time to wait for the handshake to completehandshake(Map, long)
public BayeuxClient.State handshake(Map<String,Object> template, long waitMs)
Performs the handshake with the given template and waits at most the given time for the handshake to complete.
When this method returns, the handshake may have failed (for example because the Bayeux server denied it), so it is important to check the return value to know whether the handshake completed or not.
template
- the template object to be merged with the handshake messagewaitMs
- the time to wait for the handshake to completehandshake(long)
protected void sendHandshake()
public boolean waitFor(long waitMs, BayeuxClient.State state, BayeuxClient.State... states)
Waits for this BayeuxClient to reach the given state(s) within the given time.
waitMs
- the time to wait to reach the given state(s)state
- the state to reachstates
- additional states to reach in alternativeprotected void sendConnect()
protected ChannelId newChannelId(String channelId)
newChannelId
in class AbstractClientSession
protected AbstractClientSession.AbstractSessionChannel newChannel(ChannelId channelId)
newChannel
in class AbstractClientSession
protected void sendBatch()
sendBatch
in class AbstractClientSession
protected void sendMessages(List<Message.Mutable> messages, Promise<Boolean> promise)
public void disconnect(ClientSession.MessageListener callback)
ClientSession
Disconnects this session, ending the link between the client and the server peers.
disconnect
in interface ClientSession
callback
- the message listener to notify of the disconnect resultpublic boolean disconnect(long timeout)
Performs a disconnect
and uses the given timeout
to wait for the disconnect to complete.
When a disconnect is sent to the server, the server also wakes up the long poll that may be outstanding, so that a connect reply message may arrive to the client later than the disconnect reply message.
This method waits for the given timeout
for the disconnect reply, but also
waits the same timeout for the last connect reply; in the worst case the
maximum time waited will therefore be twice the given timeout
parameter.
This method returns true if the disconnect reply message arrived within the
given timeout
parameter, no matter if the connect reply message arrived or not.
timeout
- the timeout to wait for the disconnect to completepublic void abort()
Interrupts abruptly the communication with the Bayeux server.
This method may be useful to simulate network failures.
ClientSession.disconnect()
protected void processHandshake(Message.Mutable handshake)
protected void processConnect(Message.Mutable connect)
protected void processDisconnect(Message.Mutable disconnect)
protected void processMessage(Message.Mutable message)
protected boolean scheduleHandshake(long interval, long backOff)
protected boolean scheduleConnect(long interval, long backOff)
public List<String> getAllowedTransports()
getAllowedTransports
in interface Bayeux
Bayeux.getKnownTransportNames()
public Set<String> getKnownTransportNames()
getKnownTransportNames
in interface Bayeux
Bayeux.getAllowedTransports()
public ClientTransport getTransport(String transport)
getTransport
in interface Bayeux
transport
- the transport namepublic ClientTransport getTransport()
protected void initialize()
protected void terminate(Throwable failure)
public Object getOption(String qualifiedName)
getOption
in interface Bayeux
qualifiedName
- the configuration option namequalifiedName
Bayeux.setOption(String, Object)
,
Bayeux.getOptionNames()
public void setOption(String qualifiedName, Object value)
setOption
in interface Bayeux
qualifiedName
- the configuration option namevalue
- the configuration option valueBayeux.getOption(String)
public Set<String> getOptionNames()
getOptionNames
in interface Bayeux
Bayeux.getOption(String)
public Map<String,Object> getOptions()
protected void send(Message.Mutable message)
send
in class AbstractClientSession
protected void enqueueSend(Message.Mutable message)
@Deprecated public void onSending(List<? extends Message> messages)
addTransportListener(TransportListener)
insteadCallback method invoked when the given messages have hit the network towards the Bayeux server.
The messages may not be modified, and any modification will be useless because the message have already been sent.
messages
- the messages sent@Deprecated public void onMessages(List<Message.Mutable> messages)
addTransportListener(TransportListener)
insteadCallback method invoke when the given messages have just arrived from the Bayeux server.
The messages may be modified, but it's suggested to use Extension
s instead.
Extensions will be processed after the invocation of this method.
messages
- the messages arrived@Deprecated public void onFailure(Throwable failure, List<? extends Message> messages)
addTransportListener(TransportListener)
insteadCallback method invoked when the given messages have failed to be sent.
The default implementation logs the failure at DEBUG level.
failure
- the exception that caused the failuremessages
- the messages being sentprotected void onTransportFailure(Message message, ClientTransport.FailureInfo failureInfo, ClientTransport.FailureHandler handler)
protected void onTransportFailure(String oldTransportName, String newTransportName, Throwable failure)
Copyright © 2008–2024 The CometD Project. All rights reserved.