|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.cometd.common.AbstractClientSession
org.cometd.client.BayeuxClient
public class BayeuxClient
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);
| Nested Class Summary | |
|---|---|
protected class |
BayeuxClient.BayeuxClientChannel
|
static class |
BayeuxClient.State
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 |
|---|
Bayeux.BayeuxListener |
| Nested classes/interfaces inherited from interface org.cometd.bayeux.client.ClientSession |
|---|
ClientSession.Extension |
| Field Summary | |
|---|---|
static String |
BACKOFF_INCREMENT_OPTION
|
static String |
BAYEUX_VERSION
|
static String |
MAX_BACKOFF_OPTION
|
| Constructor Summary | |
|---|---|
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). |
|
| Method Summary | |
|---|---|
void |
abort()
Interrupts abruptly the communication with the Bayeux server. |
void |
disconnect()
|
boolean |
disconnect(long timeout)
Performs a disconnect and uses the given timeout
to wait for the disconnect to complete. |
String |
dump()
|
protected void |
enqueueSend(Message.Mutable message)
|
protected void |
failMessages(Throwable x,
Message... messages)
|
List<String> |
getAllowedTransports()
|
long |
getBackoffIncrement()
|
String |
getCookie(String name)
Retrieves the cookie with the given name, if available. |
String |
getId()
|
Set<String> |
getKnownTransportNames()
|
long |
getMaxBackoff()
|
Object |
getOption(String qualifiedName)
|
Set<String> |
getOptionNames()
|
Map<String,Object> |
getOptions()
|
protected BayeuxClient.State |
getState()
|
Transport |
getTransport(String transport)
|
void |
handshake()
|
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> handshakeFields)
|
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()
|
boolean |
isDebugEnabled()
|
boolean |
isDisconnected()
|
boolean |
isHandshook()
|
protected AbstractClientSession.AbstractSessionChannel |
newChannel(ChannelId channelId)
|
protected ChannelId |
newChannelId(String channelId)
|
protected Message.Mutable |
newMessage()
|
void |
onFailure(Throwable x,
Message[] messages)
Callback method invoked when the given messages have failed to be sent. |
void |
onMessages(List<Message.Mutable> messages)
Callback method invoke when the given messages have just arrived from the Bayeux server. |
void |
onSending(Message[] messages)
Callback method invoked when the given messages have hit the network towards the Bayeux server. |
protected void |
processConnect(Message.Mutable connect)
|
protected void |
processDisconnect(Message.Mutable disconnect)
|
protected void |
processHandshake(Message.Mutable handshake)
|
protected void |
processMessage(Message.Mutable message)
|
protected boolean |
scheduleConnect(long interval,
long backoff)
|
protected boolean |
scheduleHandshake(long interval,
long backoff)
|
protected void |
sendBatch()
|
protected boolean |
sendConnect()
|
protected boolean |
sendHandshake()
|
protected boolean |
sendMessages(Message.Mutable... messages)
|
void |
setCookie(String name,
String value)
Sets a cookie that never expires. |
void |
setCookie(String name,
String value,
int maxAge)
Sets a cookie with the given max age in seconds. |
void |
setDebugEnabled(boolean debug)
|
void |
setOption(String qualifiedName,
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. |
| Methods inherited from class org.cometd.common.AbstractClientSession |
|---|
addExtension, batch, dump, endBatch, extendRcv, extendSend, getAttribute, getAttributeNames, getChannel, getChannels, isBatching, newMessageId, receive, removeAttribute, removeExtension, resetSubscriptions, setAttribute, startBatch |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final String BACKOFF_INCREMENT_OPTION
public static final String MAX_BACKOFF_OPTION
public static final String BAYEUX_VERSION
| Constructor Detail |
|---|
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 used| Method Detail |
|---|
public long getBackoffIncrement()
getMaxBackoff()public long getMaxBackoff()
getBackoffIncrement()public String getCookie(String name)
Retrieves the cookie with the given name, if available.
Note that currently only HTTP transports support cookies.
name - the cookie name
setCookie(String, String)
public void setCookie(String name,
String value)
Sets a cookie that never expires.
name - the cookie namevalue - the cookie valuesetCookie(String, String, int)
public void setCookie(String name,
String value,
int maxAge)
Sets a cookie with the given max age in seconds.
name - the cookie namevalue - the cookie valuemaxAge - the max age of the cookie, in seconds, before expirationpublic String getId()
getId in interface Sessionpublic boolean isHandshook()
isHandshook in interface Sessionpublic boolean isConnected()
isConnected in interface Sessionpublic boolean isDisconnected()
BayeuxClient is disconnecting or disconnectedprotected BayeuxClient.State getState()
BayeuxClientpublic void handshake()
handshake in interface ClientSessionpublic void handshake(Map<String,Object> handshakeFields)
handshake in interface ClientSessionpublic 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 complete
BayeuxClienthandshake(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 complete
BayeuxClienthandshake(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 alternative
protected boolean sendConnect()
protected ChannelId newChannelId(String channelId)
newChannelId in class AbstractClientSessionprotected AbstractClientSession.AbstractSessionChannel newChannel(ChannelId channelId)
newChannel in class AbstractClientSessionprotected void sendBatch()
sendBatch in class AbstractClientSessionprotected boolean sendMessages(Message.Mutable... messages)
public void disconnect()
disconnect in interface Sessiondisconnect(long)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 complete
public void abort()
Interrupts abruptly the communication with the Bayeux server.
This method may be useful to simulate network failures.
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 Bayeuxpublic Set<String> getKnownTransportNames()
getKnownTransportNames in interface Bayeuxpublic Transport getTransport(String transport)
getTransport in interface Bayeuxpublic void setDebugEnabled(boolean debug)
debug - whether the debug level for logging should be enabledisDebugEnabled()public boolean isDebugEnabled()
setDebugEnabled(boolean)protected void initialize()
protected void terminate()
public Object getOption(String qualifiedName)
getOption in interface Bayeux
public void setOption(String qualifiedName,
Object value)
setOption in interface Bayeuxpublic Set<String> getOptionNames()
getOptionNames in interface Bayeuxpublic Map<String,Object> getOptions()
BayeuxClientprotected Message.Mutable newMessage()
protected void enqueueSend(Message.Mutable message)
protected void failMessages(Throwable x,
Message... messages)
public void onSending(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(List<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 Extensions instead.
Extensions will be processed after the invocation of this method.
messages - the messages arrived
public void onFailure(Throwable x,
Message[] messages)
Callback method invoked when the given messages have failed to be sent.
The default implementation logs the failure at INFO level.
x - the exception that caused the failuremessages - the messages being sentpublic String dump()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||