Class AbstractClientSession
- java.lang.Object
-
- org.cometd.common.AbstractClientSession
-
- All Implemented Interfaces:
ClientSession,Session,org.eclipse.jetty.util.component.Dumpable
- Direct Known Subclasses:
BayeuxClient,LocalSessionImpl
public abstract class AbstractClientSession extends java.lang.Object implements ClientSession, org.eclipse.jetty.util.component.Dumpable
Partial implementation of
ClientSession.It handles extensions and batching, and provides utility methods to be used by subclasses.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected classAbstractClientSession.AbstractSessionChannelA channel scoped to aClientSession.-
Nested classes/interfaces inherited from interface org.cometd.bayeux.client.ClientSession
ClientSession.Extension, ClientSession.MessageListener
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractClientSession()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidaddExtension(ClientSession.Extension extension)Adds an extension to this session.voidbatch(java.lang.Runnable batch)Executes the given command in a batch so that any Bayeux message sent by the command (via the Bayeux API) is queued up until the end of the command and then all messages are sent at once.voiddump(java.lang.Appendable out, java.lang.String indent)booleanendBatch()Ends a batch started withSession.startBatch().protected voidextendIncoming(Message.Mutable message, Promise<java.lang.Boolean> promise)protected voidextendOutgoing(Message.Mutable message, Promise<java.lang.Boolean> promise)java.lang.ObjectgetAttribute(java.lang.String name)Retrieves the value of named session attribute.java.util.Set<java.lang.String>getAttributeNames()ClientSessionChannelgetChannel(java.lang.String channelName)Returns a client side channel scoped by this session.ClientSessionChannelgetChannel(ChannelId channelId)protected java.util.concurrent.ConcurrentMap<java.lang.String,AbstractClientSession.AbstractSessionChannel>getChannels()java.util.List<ClientSession.Extension>getExtensions()protected booleanisBatching()protected abstract AbstractClientSession.AbstractSessionChannelnewChannel(ChannelId channelId)protected abstract ChannelIdnewChannelId(java.lang.String channelId)protected Message.MutablenewMessage()protected java.lang.StringnewMessageId()protected voidnotifyCallback(ClientSession.MessageListener callback, Message.Mutable message)protected voidnotifyListeners(Message.Mutable message)voidreceive(Message.Mutable message, Promise<java.lang.Void> promise)Receives a message (from the server) and process it.protected voidregisterCallback(java.lang.String messageId, ClientSession.MessageListener callback)protected voidregisterSubscriber(java.lang.String messageId, ClientSessionChannel.MessageListener subscriber)voidremoteCall(java.lang.String target, java.lang.Object data, ClientSession.MessageListener callback)Performs a remote call to the server, to the specifiedtarget, and with the givendataas payload.java.lang.ObjectremoveAttribute(java.lang.String name)Removes a named session attribute.voidremoveExtension(ClientSession.Extension extension)Removes an extension from this session.protected voidresetSubscriptions()protected abstract voidsend(Message.Mutable message)protected abstract voidsendBatch()voidsetAttribute(java.lang.String name, java.lang.Object value)Sets a named session attribute value.voidstartBatch()Starts a batch, to be ended withSession.endBatch().protected ClientSession.MessageListenerunregisterCallback(java.lang.String messageId)protected ClientSessionChannel.MessageListenerunregisterSubscriber(java.lang.String messageId)-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.cometd.bayeux.client.ClientSession
disconnect, disconnect, handshake, handshake, handshake
-
Methods inherited from interface org.cometd.bayeux.Session
getId, isConnected, isHandshook
-
-
-
-
Method Detail
-
newMessageId
protected java.lang.String newMessageId()
-
addExtension
public void addExtension(ClientSession.Extension extension)
Description copied from interface:ClientSessionAdds an extension to this session.- Specified by:
addExtensionin interfaceClientSession- Parameters:
extension- the extension to add- See Also:
ClientSession.removeExtension(Extension)
-
removeExtension
public void removeExtension(ClientSession.Extension extension)
Description copied from interface:ClientSessionRemoves an extension from this session.- Specified by:
removeExtensionin interfaceClientSession- Parameters:
extension- the extension to remove- See Also:
ClientSession.addExtension(Extension)
-
getExtensions
public java.util.List<ClientSession.Extension> getExtensions()
- Specified by:
getExtensionsin interfaceClientSession- Returns:
- an immutable list of extensions present in this ClientSession instance
- See Also:
ClientSession.addExtension(Extension)
-
extendOutgoing
protected void extendOutgoing(Message.Mutable message, Promise<java.lang.Boolean> promise)
-
extendIncoming
protected void extendIncoming(Message.Mutable message, Promise<java.lang.Boolean> promise)
-
newChannelId
protected abstract ChannelId newChannelId(java.lang.String channelId)
-
newChannel
protected abstract AbstractClientSession.AbstractSessionChannel newChannel(ChannelId channelId)
-
getChannel
public ClientSessionChannel getChannel(java.lang.String channelName)
Description copied from interface:ClientSessionReturns a client side channel scoped by this session.
The channel name may be for a specific channel (e.g. "/foo/bar") or for a wild channel (e.g. "/meta/**" or "/foo/*").
This method will always return a channel, even if the the channel has not been created on the server side. The server side channel is only involved once a publish or subscribe method is called on the channel returned by this method.
Typical usage examples are:
clientSession.getChannel("/foo/bar").subscribe(mySubscriptionListener); clientSession.getChannel("/foo/bar").publish("Hello"); clientSession.getChannel("/meta/*").addListener(myMetaChannelListener);- Specified by:
getChannelin interfaceClientSession- Parameters:
channelName- specific or wild channel name.- Returns:
- a channel scoped by this session.
-
getChannel
public ClientSessionChannel getChannel(ChannelId channelId)
-
getChannels
protected java.util.concurrent.ConcurrentMap<java.lang.String,AbstractClientSession.AbstractSessionChannel> getChannels()
-
startBatch
public void startBatch()
Description copied from interface:SessionStarts a batch, to be ended with
Session.endBatch().The
Session.batch(Runnable)method should be preferred since it automatically starts and ends a batch without relying on a try/finally block.This method is to be used in the cases where the use of
Session.batch(Runnable)is not possible or would make the code more complex.- Specified by:
startBatchin interfaceSession- See Also:
Session.endBatch(),Session.batch(Runnable)
-
sendBatch
protected abstract void sendBatch()
-
endBatch
public boolean endBatch()
Description copied from interface:SessionEnds a batch started with
Session.startBatch().- Specified by:
endBatchin interfaceSession- Returns:
- true if the batch ended and there were messages to send.
- See Also:
Session.startBatch()
-
batch
public void batch(java.lang.Runnable batch)
Description copied from interface:SessionExecutes the given command in a batch so that any Bayeux message sent by the command (via the Bayeux API) is queued up until the end of the command and then all messages are sent at once.
-
isBatching
protected boolean isBatching()
-
getAttribute
public java.lang.Object getAttribute(java.lang.String name)
Description copied from interface:SessionRetrieves the value of named session attribute.
- Specified by:
getAttributein interfaceSession- Parameters:
name- the name of the attribute- Returns:
- the attribute value or null if the attribute is not present
-
getAttributeNames
public java.util.Set<java.lang.String> getAttributeNames()
- Specified by:
getAttributeNamesin interfaceSession- Returns:
- the list of session attribute names.
-
removeAttribute
public java.lang.Object removeAttribute(java.lang.String name)
Description copied from interface:SessionRemoves a named session attribute.
- Specified by:
removeAttributein interfaceSession- Parameters:
name- the name of the attribute- Returns:
- the value of the attribute
-
setAttribute
public void setAttribute(java.lang.String name, java.lang.Object value)Description copied from interface:SessionSets a named session attribute value.
Session attributes are convenience data that allows arbitrary application data to be associated with a session.
- Specified by:
setAttributein interfaceSession- Parameters:
name- the attribute namevalue- the attribute value
-
remoteCall
public void remoteCall(java.lang.String target, java.lang.Object data, ClientSession.MessageListener callback)Description copied from interface:ClientSessionPerforms a remote call to the server, to the specified
target, and with the givendataas payload.The remote call response will be delivered via the
callbackparameter.Typical usage:
clientSession.remoteCall("getOnlineStatus", userId, new MessageListener() { @Override public void onMessage(Message message) { if (message.isSuccessful()) { String status = (String)message.getData(); // Update UI with online status. } else { // Remote call failed. } } });- Specified by:
remoteCallin interfaceClientSession- Parameters:
target- the remote call targetdata- the remote call parameterscallback- the listener that receives the remote call response
-
send
protected abstract void send(Message.Mutable message)
-
newMessage
protected Message.Mutable newMessage()
-
resetSubscriptions
protected void resetSubscriptions()
-
receive
public void receive(Message.Mutable message, Promise<java.lang.Void> promise)
Receives a message (from the server) and process it.
Processing the message involves calling the receive
extensionsand the channellisteners.- Parameters:
message- the message received.promise- the promise notified of the receive processing
-
notifyListeners
protected void notifyListeners(Message.Mutable message)
-
notifyCallback
protected void notifyCallback(ClientSession.MessageListener callback, Message.Mutable message)
-
registerCallback
protected void registerCallback(java.lang.String messageId, ClientSession.MessageListener callback)
-
unregisterCallback
protected ClientSession.MessageListener unregisterCallback(java.lang.String messageId)
-
registerSubscriber
protected void registerSubscriber(java.lang.String messageId, ClientSessionChannel.MessageListener subscriber)
-
unregisterSubscriber
protected ClientSessionChannel.MessageListener unregisterSubscriber(java.lang.String messageId)
-
dump
public void dump(java.lang.Appendable out, java.lang.String indent) throws java.io.IOException- Specified by:
dumpin interfaceorg.eclipse.jetty.util.component.Dumpable- Throws:
java.io.IOException
-
-