org.cometd.client
Class BayeuxClient

java.lang.Object
  extended by org.cometd.common.AbstractClientSession
      extended by org.cometd.client.BayeuxClient
All Implemented Interfaces:
Bayeux, ClientSession, Session

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, 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()
           
 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

BACKOFF_INCREMENT_OPTION

public static final String BACKOFF_INCREMENT_OPTION
See Also:
Constant Field Values

MAX_BACKOFF_OPTION

public static final String MAX_BACKOFF_OPTION
See Also:
Constant Field Values

BAYEUX_VERSION

public static final String BAYEUX_VERSION
See Also:
Constant Field Values
Constructor Detail

BayeuxClient

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.

Parameters:
url - the Bayeux server URL to connect to
transport - the default (mandatory) transport to use
transports - 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 to
scheduler - the scheduler to use for scheduling timed operations
transport - the default (mandatory) transport to use
transports - additional optional transports to use in case the default transport cannot be used
Method Detail

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()

getCookie

public String getCookie(String name)

Retrieves the cookie with the given name, if available.

Note that currently only HTTP transports support cookies.

Parameters:
name - the cookie name
Returns:
the cookie value
See Also:
setCookie(String, String)

setCookie

public void setCookie(String name,
                      String value)

Sets a cookie that never expires.

Parameters:
name - the cookie name
value - the cookie value
See Also:
setCookie(String, String, int)

setCookie

public void setCookie(String name,
                      String value,
                      int maxAge)

Sets a cookie with the given max age in seconds.

Parameters:
name - the cookie name
value - the cookie value
maxAge - the max age of the cookie, in seconds, before expiration

getId

public String getId()
Specified by:
getId in interface Session

isConnected

public boolean isConnected()
Specified by:
isConnected in interface Session

isHandshook

public boolean isHandshook()
Specified by:
isHandshook in interface Session

isDisconnected

public boolean isDisconnected()
Returns:
whether this BayeuxClient is disconnecting or disconnected

getState

protected BayeuxClient.State getState()
Returns:
the current state of this BayeuxClient

handshake

public void handshake()
Specified by:
handshake in interface ClientSession

handshake

public void handshake(Map<String,Object> handshakeFields)
Specified by:
handshake in interface 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(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.

Parameters:
template - the template object to be merged with the handshake message
waitMs - 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 reach
states - 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 ChannelId newChannelId(String channelId)
Specified by:
newChannelId in class AbstractClientSession

newChannel

protected AbstractClientSession.AbstractSessionChannel newChannel(ChannelId channelId)
Specified by:
newChannel in class AbstractClientSession

sendBatch

protected void sendBatch()
Specified by:
sendBatch in class AbstractClientSession

sendMessages

protected boolean sendMessages(Message.Mutable... messages)

disconnect

public void disconnect()
Specified by:
disconnect in interface Session

abort

public void abort()

Interrupts abruptly the communication with the Bayeux server.

This method may be useful to simulate network failures.

See Also:
disconnect()

processHandshake

protected void processHandshake(Message.Mutable handshake)

processConnect

protected void processConnect(Message.Mutable connect)

processDisconnect

protected void processDisconnect(Message.Mutable disconnect)

processMessage

protected void processMessage(Message.Mutable message)

scheduleHandshake

protected boolean scheduleHandshake(long interval,
                                    long backoff)

scheduleConnect

protected boolean scheduleConnect(long interval,
                                  long backoff)

getAllowedTransports

public List<String> getAllowedTransports()
Specified by:
getAllowedTransports in interface Bayeux

getKnownTransportNames

public Set<String> getKnownTransportNames()
Specified by:
getKnownTransportNames in interface Bayeux

getTransport

public Transport getTransport(String transport)
Specified by:
getTransport in interface Bayeux

setDebugEnabled

public void setDebugEnabled(boolean debug)
Parameters:
debug - whether the debug level for logging should be enabled
See Also:
isDebugEnabled()

isDebugEnabled

public boolean isDebugEnabled()
Returns:
whether the debug level for logging is enabled
See Also:
setDebugEnabled(boolean)

initialize

protected void initialize()

terminate

protected void terminate()

getOption

public Object getOption(String qualifiedName)
Specified by:
getOption in interface Bayeux

setOption

public void setOption(String qualifiedName,
                      Object value)
Specified by:
setOption in interface Bayeux

getOptionNames

public Set<String> getOptionNames()
Specified by:
getOptionNames in interface Bayeux

getOptions

public Map<String,Object> getOptions()
Returns:
the options that configure with BayeuxClient

newMessage

protected Message.Mutable newMessage()

enqueueSend

protected void enqueueSend(Message.Mutable message)

failMessages

protected void failMessages(Throwable x,
                            Message... messages)

onSending

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.

Parameters:
messages - the messages sent

onMessages

public 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.

Parameters:
messages - the messages arrived

onFailure

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.

Parameters:
x - the exception that caused the failure
messages - the messages being sent

dump

public String dump()


Copyright © 2008-2011 Dojo Foundation. All Rights Reserved.