Class BayeuxClient
- java.lang.Object
-
- org.cometd.common.AbstractClientSession
-
- org.cometd.client.BayeuxClient
-
- All Implemented Interfaces:
org.cometd.bayeux.Bayeux,org.cometd.bayeux.client.ClientSession,org.cometd.bayeux.Session,org.eclipse.jetty.util.component.Dumpable
public class BayeuxClient extends org.cometd.common.AbstractClientSession implements org.cometd.bayeux.BayeuxBayeuxClient 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.MessageListenerto 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, and the most common transport is
LongPollingTransport, which uses HTTP to transport Bayeux messages and it is based on Jetty's HTTP client.When the communication with the server is finished, the BayeuxClient can be disconnected from the Bayeux server.
Typical usage:
// Handshake String url = "http://localhost:8080/cometd"; BayeuxClient client = new BayeuxClient(url, LongPollingTransport.create(null)); client.handshake(); client.waitFor(1000, BayeuxClient.State.CONNECTED); // Subscription to channels ClientSessionChannel channel = client.getChannel("/foo"); channel.subscribe(new ClientSessionChannel.MessageListener() { public void onMessage(ClientSessionChannel channel, Message message) { // Handle the message } }); // Publishing to channels Map<String, Object> data = new HashMap<String, Object>(); data.put("bar", "baz"); channel.publish(data); // Disconnecting client.disconnect(); client.waitFor(1000, BayeuxClient.State.DISCONNECTED);
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected classBayeuxClient.BayeuxClientChannelA channel scoped to this BayeuxClient.static classBayeuxClient.StateThe states that a BayeuxClient may assume-
Nested classes/interfaces inherited from class org.cometd.common.AbstractClientSession
org.cometd.common.AbstractClientSession.AbstractSessionChannel
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringBACKOFF_INCREMENT_OPTIONstatic java.lang.StringBAYEUX_VERSIONprotected org.slf4j.Loggerloggerstatic java.lang.StringMAX_BACKOFF_OPTION
-
Constructor Summary
Constructors Constructor Description BayeuxClient(java.lang.String url, java.util.concurrent.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(java.lang.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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidabort()Interrupts abruptly the communication with the Bayeux server.voiddisconnect()booleandisconnect(long timeout)Performs adisconnectand uses the giventimeoutto wait for the disconnect to complete.voiddisconnect(org.cometd.bayeux.client.ClientSessionChannel.MessageListener callback)protected voidenqueueSend(org.cometd.bayeux.Message.Mutable message)java.util.List<java.lang.String>getAllowedTransports()longgetBackoff()longgetBackoffIncrement()java.net.HttpCookiegetCookie(java.lang.String name)Retrieves the first cookie with the given name, if available.java.net.CookieStoregetCookieStore()java.lang.StringgetId()java.util.Set<java.lang.String>getKnownTransportNames()longgetMaxBackoff()java.lang.ObjectgetOption(java.lang.String qualifiedName)java.util.Set<java.lang.String>getOptionNames()java.util.Map<java.lang.String,java.lang.Object>getOptions()protected BayeuxClient.StategetState()ClientTransportgetTransport()ClientTransportgetTransport(java.lang.String transport)java.lang.StringgetURL()voidhandshake()BayeuxClient.Statehandshake(long waitMs)Performs the handshake and waits at most the given time for the handshake to complete.voidhandshake(java.util.Map<java.lang.String,java.lang.Object> handshakeFields)BayeuxClient.Statehandshake(java.util.Map<java.lang.String,java.lang.Object> template, long waitMs)Performs the handshake with the given template and waits at most the given time for the handshake to complete.voidhandshake(java.util.Map<java.lang.String,java.lang.Object> fields, org.cometd.bayeux.client.ClientSessionChannel.MessageListener callback)voidhandshake(org.cometd.bayeux.client.ClientSessionChannel.MessageListener callback)protected voidinitialize()booleanisConnected()booleanisDisconnected()booleanisHandshook()protected voidmessagesFailure(java.lang.Throwable cause, java.util.List<? extends org.cometd.bayeux.Message> messages)protected org.cometd.common.AbstractClientSession.AbstractSessionChannelnewChannel(org.cometd.bayeux.ChannelId channelId)protected org.cometd.bayeux.ChannelIdnewChannelId(java.lang.String channelId)voidonFailure(java.lang.Throwable failure, java.util.List<? extends org.cometd.bayeux.Message> messages)Callback method invoked when the given messages have failed to be sent.voidonMessages(java.util.List<org.cometd.bayeux.Message.Mutable> messages)Callback method invoke when the given messages have just arrived from the Bayeux server.voidonSending(java.util.List<? extends org.cometd.bayeux.Message> messages)Callback method invoked when the given messages have hit the network towards the Bayeux server.protected voidonTransportFailure(java.lang.String oldTransportName, java.lang.String newTransportName, java.lang.Throwable failure)protected voidonTransportFailure(org.cometd.bayeux.Message message, ClientTransport.FailureInfo failureInfo, ClientTransport.FailureHandler handler)protected voidprocessConnect(org.cometd.bayeux.Message.Mutable connect)protected voidprocessDisconnect(org.cometd.bayeux.Message.Mutable disconnect)protected voidprocessHandshake(org.cometd.bayeux.Message.Mutable handshake)protected voidprocessMessage(org.cometd.bayeux.Message.Mutable message)voidputCookie(java.net.HttpCookie cookie)protected booleanscheduleConnect(long interval, long backOff)protected booleanscheduleHandshake(long interval, long backOff)protected voidsend(org.cometd.bayeux.Message.Mutable message)protected voidsendBatch()protected booleansendConnect()protected booleansendHandshake()protected booleansendMessages(java.util.List<org.cometd.bayeux.Message.Mutable> messages)voidsetOption(java.lang.String qualifiedName, java.lang.Object value)protected voidterminate()java.lang.StringtoString()booleanwaitFor(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, dump, endBatch, extendRcv, extendSend, getAttribute, getAttributeNames, getChannel, getChannel, getChannels, getExtensions, isBatching, newMessage, newMessageId, notifyListener, notifyListeners, receive, registerCallback, registerSubscriber, remoteCall, removeAttribute, removeExtension, resetSubscriptions, setAttribute, startBatch, unregisterCallback, unregisterSubscriber
-
-
-
-
Field Detail
-
BACKOFF_INCREMENT_OPTION
public static final java.lang.String BACKOFF_INCREMENT_OPTION
- See Also:
- Constant Field Values
-
MAX_BACKOFF_OPTION
public static final java.lang.String MAX_BACKOFF_OPTION
- See Also:
- Constant Field Values
-
BAYEUX_VERSION
public static final java.lang.String BAYEUX_VERSION
- See Also:
- Constant Field Values
-
logger
protected final org.slf4j.Logger logger
-
-
Constructor Detail
-
BayeuxClient
public BayeuxClient(java.lang.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.- 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(java.lang.String url, java.util.concurrent.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 Detail
-
getURL
public java.lang.String getURL()
- Returns:
- the URL passed when constructing this instance
-
getBackoff
public long getBackoff()
- Returns:
- the current period of time to wait before trying to reconnect
-
getBackoffIncrement
public long getBackoffIncrement()
- Returns:
- the period of time that increments the pause to wait before trying to reconnect after each failed attempt to connect to the Bayeux server
- See Also:
getMaxBackoff()
-
getMaxBackoff
public long getMaxBackoff()
- Returns:
- the maximum pause to wait before trying to reconnect after each failed attempt to connect to the Bayeux server
- See Also:
getBackoffIncrement()
-
getCookieStore
public java.net.CookieStore getCookieStore()
-
getCookie
public java.net.HttpCookie getCookie(java.lang.String name)
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
public void putCookie(java.net.HttpCookie cookie)
-
getId
public java.lang.String getId()
- Specified by:
getIdin interfaceorg.cometd.bayeux.Session
-
isHandshook
public boolean isHandshook()
- Specified by:
isHandshookin interfaceorg.cometd.bayeux.Session
-
isConnected
public boolean isConnected()
- Specified by:
isConnectedin interfaceorg.cometd.bayeux.Session
-
isDisconnected
public boolean isDisconnected()
- Returns:
- whether this BayeuxClient is terminating or disconnected
-
getState
protected BayeuxClient.State getState()
- Returns:
- the current state of this BayeuxClient
-
handshake
public void handshake()
- Specified by:
handshakein interfaceorg.cometd.bayeux.client.ClientSession
-
handshake
public void handshake(java.util.Map<java.lang.String,java.lang.Object> handshakeFields)
- Specified by:
handshakein interfaceorg.cometd.bayeux.client.ClientSession
-
handshake
public void handshake(org.cometd.bayeux.client.ClientSessionChannel.MessageListener callback)
-
handshake
public void handshake(java.util.Map<java.lang.String,java.lang.Object> fields, org.cometd.bayeux.client.ClientSessionChannel.MessageListener callback)- Specified by:
handshakein interfaceorg.cometd.bayeux.client.ClientSession
-
handshake
public 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.
- Parameters:
waitMs- the time to wait for the handshake to complete- Returns:
- the state of this BayeuxClient
- See Also:
handshake(Map, long)
-
handshake
public BayeuxClient.State handshake(java.util.Map<java.lang.String,java.lang.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.
- 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 boolean sendHandshake()
-
waitFor
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.
- 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 boolean sendConnect()
-
newChannelId
protected org.cometd.bayeux.ChannelId newChannelId(java.lang.String channelId)
- Specified by:
newChannelIdin classorg.cometd.common.AbstractClientSession
-
newChannel
protected org.cometd.common.AbstractClientSession.AbstractSessionChannel newChannel(org.cometd.bayeux.ChannelId channelId)
- Specified by:
newChannelin classorg.cometd.common.AbstractClientSession
-
sendBatch
protected void sendBatch()
- Specified by:
sendBatchin classorg.cometd.common.AbstractClientSession
-
sendMessages
protected boolean sendMessages(java.util.List<org.cometd.bayeux.Message.Mutable> messages)
-
disconnect
public void disconnect()
- Specified by:
disconnectin interfaceorg.cometd.bayeux.Session- See Also:
disconnect(long)
-
disconnect
public void disconnect(org.cometd.bayeux.client.ClientSessionChannel.MessageListener callback)
- Specified by:
disconnectin interfaceorg.cometd.bayeux.client.ClientSession
-
disconnect
public boolean disconnect(long timeout)
Performs a
disconnectand uses the giventimeoutto 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
timeoutfor 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 giventimeoutparameter.This method returns true if the disconnect reply message arrived within the given
timeoutparameter, 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:
disconnect()
-
messagesFailure
protected void messagesFailure(java.lang.Throwable cause, java.util.List<? extends org.cometd.bayeux.Message> messages)
-
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
public java.util.List<java.lang.String> getAllowedTransports()
- Specified by:
getAllowedTransportsin interfaceorg.cometd.bayeux.Bayeux
-
getKnownTransportNames
public java.util.Set<java.lang.String> getKnownTransportNames()
- Specified by:
getKnownTransportNamesin interfaceorg.cometd.bayeux.Bayeux
-
getTransport
public ClientTransport getTransport(java.lang.String transport)
- Specified by:
getTransportin interfaceorg.cometd.bayeux.Bayeux
-
getTransport
public ClientTransport getTransport()
-
initialize
protected void initialize()
-
terminate
protected void terminate()
-
getOption
public java.lang.Object getOption(java.lang.String qualifiedName)
- Specified by:
getOptionin interfaceorg.cometd.bayeux.Bayeux
-
setOption
public void setOption(java.lang.String qualifiedName, java.lang.Object value)- Specified by:
setOptionin interfaceorg.cometd.bayeux.Bayeux
-
getOptionNames
public java.util.Set<java.lang.String> getOptionNames()
- Specified by:
getOptionNamesin interfaceorg.cometd.bayeux.Bayeux
-
getOptions
public java.util.Map<java.lang.String,java.lang.Object> getOptions()
- Returns:
- the options that configure with BayeuxClient.
-
send
protected void send(org.cometd.bayeux.Message.Mutable message)
- Specified by:
sendin classorg.cometd.common.AbstractClientSession
-
enqueueSend
protected void enqueueSend(org.cometd.bayeux.Message.Mutable message)
-
onSending
public void onSending(java.util.List<? extends org.cometd.bayeux.Message> messages)
Callback 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.
- Parameters:
messages- the messages sent
-
onMessages
public void onMessages(java.util.List<org.cometd.bayeux.Message.Mutable> messages)
Callback method invoke when the given messages have just arrived from the Bayeux server.
The messages may be modified, but it's suggested to use
ClientSession.Extensions instead.Extensions will be processed after the invocation of this method.
- Parameters:
messages- the messages arrived
-
onFailure
public void onFailure(java.lang.Throwable failure, java.util.List<? extends org.cometd.bayeux.Message> messages)Callback method invoked when the given messages have failed to be sent.
The default implementation logs the failure at DEBUG level.
- Parameters:
failure- the exception that caused the failuremessages- the messages being sent
-
onTransportFailure
protected void onTransportFailure(org.cometd.bayeux.Message message, ClientTransport.FailureInfo failureInfo, ClientTransport.FailureHandler handler)
-
onTransportFailure
protected void onTransportFailure(java.lang.String oldTransportName, java.lang.String newTransportName, java.lang.Throwable failure)
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-