org.cometd.bayeux.Bayeux
public class BayeuxClient
extends org.cometd.common.AbstractClientSession
implements org.cometd.bayeux.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, 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);
Modifier and Type | Class | Description |
---|---|---|
protected class |
BayeuxClient.BayeuxClientChannel |
A channel scoped to this BayeuxClient.
|
static class |
BayeuxClient.State |
The states that a
BayeuxClient may assume |
org.cometd.common.AbstractClientSession.AbstractSessionChannel
Modifier and Type | Field | Description |
---|---|---|
static java.lang.String |
BACKOFF_INCREMENT_OPTION |
|
static java.lang.String |
BAYEUX_VERSION |
|
protected org.slf4j.Logger |
logger |
|
static java.lang.String |
MAX_BACKOFF_OPTION |
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).
|
Modifier and Type | Method | Description |
---|---|---|
void |
abort() |
Interrupts abruptly the communication with the Bayeux server.
|
boolean |
disconnect(long timeout) |
Performs a
disconnect and uses the given timeout
to wait for the disconnect to complete. |
void |
disconnect(org.cometd.bayeux.client.ClientSession.MessageListener callback) |
|
protected void |
enqueueSend(org.cometd.bayeux.Message.Mutable message) |
|
java.util.List<java.lang.String> |
getAllowedTransports() |
|
long |
getBackoffIncrement() |
|
java.net.HttpCookie |
getCookie(java.lang.String name) |
Retrieves the first cookie with the given name, if available.
|
java.net.CookieStore |
getCookieStore() |
|
java.lang.String |
getId() |
|
java.util.Set<java.lang.String> |
getKnownTransportNames() |
|
long |
getMaxBackoff() |
|
java.lang.Object |
getOption(java.lang.String qualifiedName) |
|
java.util.Set<java.lang.String> |
getOptionNames() |
|
java.util.Map<java.lang.String,java.lang.Object> |
getOptions() |
|
protected BayeuxClient.State |
getState() |
|
ClientTransport |
getTransport() |
|
ClientTransport |
getTransport(java.lang.String transport) |
|
java.lang.String |
getURL() |
|
BayeuxClient.State |
handshake(long waitMs) |
Performs the handshake and waits at most the given time for the handshake to complete.
|
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.
|
void |
handshake(java.util.Map<java.lang.String,java.lang.Object> fields,
org.cometd.bayeux.client.ClientSession.MessageListener callback) |
|
void |
handshake(org.cometd.bayeux.client.ClientSession.MessageListener callback) |
|
void |
handshake(org.cometd.bayeux.client.ClientSessionChannel.MessageListener callback) |
Deprecated.
use
handshake(MessageListener) instead |
protected void |
initialize() |
|
boolean |
isConnected() |
|
boolean |
isDisconnected() |
|
boolean |
isHandshook() |
|
protected void |
messagesFailure(java.lang.Throwable cause,
java.util.List<? extends org.cometd.bayeux.Message> messages) |
|
protected org.cometd.common.AbstractClientSession.AbstractSessionChannel |
newChannel(org.cometd.bayeux.ChannelId channelId) |
|
protected org.cometd.bayeux.ChannelId |
newChannelId(java.lang.String channelId) |
|
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.
|
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.
|
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.
|
protected void |
onTransportFailure(java.lang.String oldTransportName,
java.lang.String newTransportName,
java.lang.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(java.net.HttpCookie cookie) |
|
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 |
sendBatch() |
|
protected boolean |
sendConnect() |
|
protected boolean |
sendHandshake() |
|
protected boolean |
sendMessages(java.util.List<org.cometd.bayeux.Message.Mutable> messages) |
|
void |
setOption(java.lang.String qualifiedName,
java.lang.Object value) |
|
protected void |
terminate() |
|
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, dump, endBatch, extendIncoming, extendOutgoing, extendRcv, extendSend, getAttribute, getAttributeNames, getChannel, getChannel, getChannels, getExtensions, isBatching, newMessage, newMessageId, notifyCallback, notifyListeners, receive, registerCallback, registerSubscriber, remoteCall, removeAttribute, removeExtension, resetSubscriptions, setAttribute, startBatch, unregisterCallback, unregisterSubscriber
public static final java.lang.String BACKOFF_INCREMENT_OPTION
public static final java.lang.String MAX_BACKOFF_OPTION
public static final java.lang.String BAYEUX_VERSION
protected final org.slf4j.Logger logger
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.
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(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).
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 java.lang.String getURL()
public long getBackoffIncrement()
getMaxBackoff()
public long getMaxBackoff()
getBackoffIncrement()
public java.net.CookieStore getCookieStore()
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.
name
- the cookie nameputCookie(HttpCookie)
public void putCookie(java.net.HttpCookie cookie)
public java.lang.String getId()
public boolean isHandshook()
public boolean isConnected()
public boolean isDisconnected()
protected BayeuxClient.State getState()
@Deprecated public void handshake(org.cometd.bayeux.client.ClientSessionChannel.MessageListener callback)
handshake(MessageListener)
insteadpublic void handshake(org.cometd.bayeux.client.ClientSession.MessageListener callback)
public void handshake(java.util.Map<java.lang.String,java.lang.Object> fields, org.cometd.bayeux.client.ClientSession.MessageListener callback)
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.
waitMs
- the time to wait for the handshake to completehandshake(Map, long)
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.
template
- the template object to be merged with the handshake messagewaitMs
- the time to wait for the handshake to completehandshake(long)
protected boolean 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 boolean sendConnect()
protected org.cometd.bayeux.ChannelId newChannelId(java.lang.String channelId)
newChannelId
in class org.cometd.common.AbstractClientSession
protected org.cometd.common.AbstractClientSession.AbstractSessionChannel newChannel(org.cometd.bayeux.ChannelId channelId)
newChannel
in class org.cometd.common.AbstractClientSession
protected void sendBatch()
sendBatch
in class org.cometd.common.AbstractClientSession
protected boolean sendMessages(java.util.List<org.cometd.bayeux.Message.Mutable> messages)
public void disconnect(org.cometd.bayeux.client.ClientSession.MessageListener callback)
public 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 messagesFailure(java.lang.Throwable cause, java.util.List<? extends org.cometd.bayeux.Message> messages)
protected void processHandshake(org.cometd.bayeux.Message.Mutable handshake)
protected void processConnect(org.cometd.bayeux.Message.Mutable connect)
protected void processDisconnect(org.cometd.bayeux.Message.Mutable disconnect)
protected void processMessage(org.cometd.bayeux.Message.Mutable message)
protected boolean scheduleHandshake(long interval, long backOff)
protected boolean scheduleConnect(long interval, long backOff)
public java.util.List<java.lang.String> getAllowedTransports()
getAllowedTransports
in interface org.cometd.bayeux.Bayeux
public java.util.Set<java.lang.String> getKnownTransportNames()
getKnownTransportNames
in interface org.cometd.bayeux.Bayeux
public ClientTransport getTransport(java.lang.String transport)
getTransport
in interface org.cometd.bayeux.Bayeux
public ClientTransport getTransport()
protected void initialize()
protected void terminate()
public java.lang.Object getOption(java.lang.String qualifiedName)
getOption
in interface org.cometd.bayeux.Bayeux
public void setOption(java.lang.String qualifiedName, java.lang.Object value)
setOption
in interface org.cometd.bayeux.Bayeux
public java.util.Set<java.lang.String> getOptionNames()
getOptionNames
in interface org.cometd.bayeux.Bayeux
public java.util.Map<java.lang.String,java.lang.Object> getOptions()
protected void send(org.cometd.bayeux.Message.Mutable message)
send
in class org.cometd.common.AbstractClientSession
protected void enqueueSend(org.cometd.bayeux.Message.Mutable message)
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.
messages
- the messages sentpublic 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.Extension
s instead.
Extensions will be processed after the invocation of this method.
messages
- the messages arrivedpublic 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.
failure
- the exception that caused the failuremessages
- the messages being sentprotected void onTransportFailure(org.cometd.bayeux.Message message, ClientTransport.FailureInfo failureInfo, ClientTransport.FailureHandler handler)
protected void onTransportFailure(java.lang.String oldTransportName, java.lang.String newTransportName, java.lang.Throwable failure)
Copyright © 2008–2017 The CometD Project. All rights reserved.