Class BayeuxClient
- All Implemented Interfaces:
org.cometd.bayeux.Bayeux
,ClientSession
,org.cometd.bayeux.Session
,org.eclipse.jetty.util.component.Dumpable
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);
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
A strategy to controls wait times of the retry attempts in case of heartbeat failures.protected class
A channel scoped to this BayeuxClient.static class
static class
The states that a BayeuxClient may assume.Nested classes/interfaces inherited from class org.cometd.common.AbstractClientSession
AbstractClientSession.AbstractSessionChannel
Nested classes/interfaces inherited from interface org.cometd.bayeux.Bayeux
org.cometd.bayeux.Bayeux.BayeuxListener, org.cometd.bayeux.Bayeux.Validator
Nested classes/interfaces inherited from interface org.cometd.bayeux.client.ClientSession
ClientSession.Extension, ClientSession.MessageListener
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.Dumpable
org.eclipse.jetty.util.component.Dumpable.DumpableContainer
-
Field Summary
FieldsFields inherited from interface org.eclipse.jetty.util.component.Dumpable
KEY
-
Constructor Summary
ConstructorsConstructorDescriptionBayeuxClient(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).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). -
Method Summary
Modifier and TypeMethodDescriptionvoid
abort()
Interrupts abruptly the communication with the Bayeux server.void
addTransportListener(TransportListener listener)
boolean
disconnect(long timeout)
Performs adisconnect
and uses the giventimeout
to wait for the disconnect to complete.void
disconnect(ClientSession.MessageListener callback)
protected void
enqueueSend(org.cometd.bayeux.Message.Mutable message)
Retrieves the first cookie with the given name, if available.getId()
protected BayeuxClient.State
getState()
getTransport(String transport)
getURL()
handshake(long waitMs)
Performs the handshake and waits at most the given time for the handshake to complete.Performs the handshake with the given template and waits at most the given time for the handshake to complete.void
handshake(Map<String,Object> fields, ClientSession.MessageListener callback)
void
handshake(ClientSession.MessageListener callback)
protected void
boolean
boolean
boolean
protected void
messagesFailure(Throwable cause, List<? extends org.cometd.bayeux.Message> messages)
newChannel(org.cometd.bayeux.ChannelId channelId)
protected org.cometd.bayeux.ChannelId
newChannelId(String channelId)
protected void
onTransportFailure(String oldTransportName, String newTransportName, Throwable failure)
protected void
onTransportFailure(org.cometd.bayeux.Message message, ClientTransport.FailureInfo failureInfo, ClientTransport.FailureHandler handler)
protected void
processConnect(org.cometd.bayeux.Message.Mutable connect)
protected void
processDisconnect(org.cometd.bayeux.Message.Mutable disconnect)
protected void
processHandshake(org.cometd.bayeux.Message.Mutable handshake)
protected void
processMessage(org.cometd.bayeux.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(org.cometd.bayeux.Message.Mutable message)
protected void
protected void
protected void
protected void
sendMessages(List<org.cometd.bayeux.Message.Mutable> messages, org.cometd.bayeux.Promise<Boolean> promise)
void
setBackOffStrategy(BayeuxClient.BackOffStrategy backOffStrategy)
void
protected void
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.Methods inherited from class org.cometd.common.AbstractClientSession
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
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.cometd.bayeux.client.ClientSession
disconnect, handshake, handshake
Methods inherited from interface org.eclipse.jetty.util.component.Dumpable
dump, dumpSelf
-
Field Details
-
BACKOFF_INCREMENT_OPTION
- See Also:
- Constant Field Values
-
MAX_BACKOFF_OPTION
- See Also:
- Constant Field Values
-
BAYEUX_VERSION
- See Also:
- Constant Field Values
-
-
Constructor Details
-
BayeuxClient
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.- Parameters:
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 used- See Also:
BayeuxClient(String, ScheduledExecutorService, ClientTransport, ClientTransport...)
-
BayeuxClient
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).
- Parameters:
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 used
-
-
Method Details
-
getURL
- Returns:
- the URL passed when constructing this instance
-
getBackOffStrategy
-
setBackOffStrategy
-
getCookieStore
-
getCookie
Retrieves the first cookie with the given name, if available.
Note that currently only HTTP transports support cookies.
- Parameters:
name
- the cookie name- Returns:
- the cookie, or null if no such cookie is found
- See Also:
putCookie(HttpCookie)
-
putCookie
-
getId
- Specified by:
getId
in interfaceorg.cometd.bayeux.Session
-
isHandshook
public boolean isHandshook()- Specified by:
isHandshook
in interfaceorg.cometd.bayeux.Session
-
isConnected
public boolean isConnected()- Specified by:
isConnected
in interfaceorg.cometd.bayeux.Session
-
isDisconnected
public boolean isDisconnected()- Returns:
- whether this BayeuxClient is terminating or disconnected
-
getState
- Returns:
- the current state of this BayeuxClient
-
addTransportListener
- Parameters:
listener
- theTransportListener
to add
-
removeTransportListener
- Parameters:
listener
- theTransportListener
to remove
-
handshake
- Parameters:
callback
- the message listener to notify of the handshake result
-
handshake
- Specified by:
handshake
in interfaceClientSession
-
handshake
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.
- Parameters:
waitMs
- the time to wait for the handshake to complete- Returns:
- the state of this BayeuxClient
- See Also:
handshake(Map, long)
-
handshake
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.
- Parameters:
template
- the template object to be merged with the handshake messagewaitMs
- the time to wait for the handshake to complete- Returns:
- the state of this BayeuxClient
- See Also:
handshake(long)
-
sendHandshake
protected void sendHandshake() -
waitFor
Waits for this BayeuxClient to reach the given state(s) within the given time.
- Parameters:
waitMs
- the time to wait to reach the given state(s)state
- the state to reachstates
- additional states to reach in alternative- Returns:
- true if one of the state(s) has been reached within the given time, false otherwise
-
sendConnect
protected void sendConnect() -
newChannelId
- Specified by:
newChannelId
in classAbstractClientSession
-
newChannel
protected AbstractClientSession.AbstractSessionChannel newChannel(org.cometd.bayeux.ChannelId channelId)- Specified by:
newChannel
in classAbstractClientSession
-
sendBatch
protected void sendBatch()- Specified by:
sendBatch
in classAbstractClientSession
-
sendMessages
-
disconnect
- Specified by:
disconnect
in interfaceClientSession
-
disconnect
public boolean disconnect(long timeout)Performs a
disconnect
and uses the giventimeout
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 giventimeout
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.- Parameters:
timeout
- the timeout to wait for the disconnect to complete- Returns:
- true if the disconnect completed within the given timeout
-
abort
public void abort()Interrupts abruptly the communication with the Bayeux server.
This method may be useful to simulate network failures.
- See Also:
ClientSession.disconnect()
-
messagesFailure
-
processHandshake
protected void processHandshake(org.cometd.bayeux.Message.Mutable handshake) -
processConnect
protected void processConnect(org.cometd.bayeux.Message.Mutable connect) -
processDisconnect
protected void processDisconnect(org.cometd.bayeux.Message.Mutable disconnect) -
processMessage
protected void processMessage(org.cometd.bayeux.Message.Mutable message) -
scheduleHandshake
protected boolean scheduleHandshake(long interval, long backOff) -
scheduleConnect
protected boolean scheduleConnect(long interval, long backOff) -
getAllowedTransports
- Specified by:
getAllowedTransports
in interfaceorg.cometd.bayeux.Bayeux
-
getKnownTransportNames
- Specified by:
getKnownTransportNames
in interfaceorg.cometd.bayeux.Bayeux
-
getTransport
- Specified by:
getTransport
in interfaceorg.cometd.bayeux.Bayeux
-
getTransport
-
initialize
protected void initialize() -
terminate
-
getOption
- Specified by:
getOption
in interfaceorg.cometd.bayeux.Bayeux
-
setOption
- Specified by:
setOption
in interfaceorg.cometd.bayeux.Bayeux
-
getOptionNames
- Specified by:
getOptionNames
in interfaceorg.cometd.bayeux.Bayeux
-
getOptions
- Returns:
- the options that configure with BayeuxClient.
-
send
protected void send(org.cometd.bayeux.Message.Mutable message)- Specified by:
send
in classAbstractClientSession
-
enqueueSend
protected void enqueueSend(org.cometd.bayeux.Message.Mutable message) -
onTransportFailure
protected void onTransportFailure(org.cometd.bayeux.Message message, ClientTransport.FailureInfo failureInfo, ClientTransport.FailureHandler handler) -
onTransportFailure
-
toString
-