public abstract class AbstractService extends Object
AbstractService provides convenience methods to assist with the
creation of a CometD services.
A CometD service runs application code whenever a message is received on a particular channel.
Specifically it provides:
Subclasses should call addService(String, String) in order to
map channel subscriptions to method invocations, usually in the subclass
constructor.
Each CometD service has an associated LocalSession that can be
used as the source for messages published via
ServerChannel#publish(Session, Mutable) or
ServerSession#deliver(Session, Mutable).
BayeuxServer#newLocalSession(String)} as an alternative to {@link AbstractService}.| Modifier and Type | Field and Description |
|---|---|
protected org.slf4j.Logger |
_logger |
| Constructor and Description |
|---|
AbstractService(BayeuxServer bayeux,
String name)
Instantiates a CometD service with the given name.
|
AbstractService(BayeuxServer bayeux,
String name,
int maxThreads)
Instantiate a CometD service with the given name and max number of pooled threads.
|
| Modifier and Type | Method and Description |
|---|---|
protected void |
addService(String channelName,
String methodName)
Maps the method of a subclass with the given name to a
ServerChannel.MessageListener on the given channel, so that the method
is invoked for each message received on the channel. |
protected void |
doInvoke(Method method,
ServerSession fromClient,
ServerMessage msg) |
protected void |
exception(String method,
ServerSession fromClient,
LocalSession toClient,
ServerMessage msg,
Throwable x)
Handles exceptions during the invocation of a mapped method.
|
BayeuxServer |
getBayeux() |
LocalSession |
getLocalSession() |
String |
getName() |
ServerSession |
getServerSession() |
org.eclipse.jetty.util.thread.ThreadPool |
getThreadPool() |
boolean |
isSeeOwnPublishes() |
protected void |
removeService(String channelName)
Unmaps all the methods that have been mapped to the given channel.
|
protected void |
removeService(String channelName,
String methodName)
Unmaps the method with the given name that has been mapped to the given channel.
|
protected void |
send(ServerSession toClient,
String onChannel,
Object data,
String id)
Sends data to an individual remote client.
|
void |
setSeeOwnPublishes(boolean seeOwnPublishes) |
void |
setThreadPool(org.eclipse.jetty.util.thread.ThreadPool pool)
Sets the thread pool associated to this CometD service.
|
public AbstractService(BayeuxServer bayeux, String name)
Instantiates a CometD service with the given name.
bayeux - The BayeuxServer instance.name - The name of the service (used as client ID prefix).public AbstractService(BayeuxServer bayeux, String name, int maxThreads)
Instantiate a CometD service with the given name and max number of pooled threads.
bayeux - The BayeuxServer instance.name - The name of the service (used as client ID prefix).maxThreads - The max size of a ThreadPool to create to handle messages.public BayeuxServer getBayeux()
public String getName()
public LocalSession getLocalSession()
LocalSession associated with this CometD servicepublic ServerSession getServerSession()
ServerSession of the LocalSession associated
with this CometD servicepublic org.eclipse.jetty.util.thread.ThreadPool getThreadPool()
AbstractService(BayeuxServer, String, int)public void setThreadPool(org.eclipse.jetty.util.thread.ThreadPool pool)
Sets the thread pool associated to this CometD service.
If the ThreadPool is a LifeCycle instance,
and it is not already started, then it will started.
pool - The ThreadPoolpublic boolean isSeeOwnPublishes()
setSeeOwnPublishes(boolean)public void setSeeOwnPublishes(boolean seeOwnPublishes)
seeOwnPublishes - whether this CometD service receives messages published by itself
on channels it is subscribed to (defaults to false).isSeeOwnPublishes()protected void addService(String channelName, String methodName)
Maps the method of a subclass with the given name to a
ServerChannel.MessageListener on the given channel, so that the method
is invoked for each message received on the channel.
The channel name may be a wildcard channel name.
The method must have a unique name and one of the following signatures:
myMethod(ServerSession from, Object data)myMethod(ServerSession from, Object data, String messageId)myMethod(ServerSession from, String channel, Object data, String messageId)The data parameter can be a specific type if the type of
the data object published by the client is known by the server.
If it is not known will be Map<String, Object>.
If the type of the data parameter is Message (or a subinterface
such as ServerMessage.Mutable then the message object itself is
passed rather than just the message's data.
Typically a service will be used to a channel in the /service/**
space which is not a broadcast channel.
Any object returned by a mapped method is delivered back to the client that sent the message and not broadcast. If the method returns void or null, then no response is sent.
A mapped method may also call send(ServerSession, String, Object, String)
to deliver message(s) to specific clients and/or channels.
A mapped method may also publish to different channels via
ServerChannel#publish(Session, Mutable).
channelName - The channel to listen tomethodName - The name of the method on this subclass to call when messages
are received on the channelremoveService(String, String)protected void removeService(String channelName, String methodName)
Unmaps the method with the given name that has been mapped to the given channel.
channelName - The channel namemethodName - The name of the method to unmapaddService(String, String),
removeService(String)protected void removeService(String channelName)
Unmaps all the methods that have been mapped to the given channel.
channelName - The channel nameaddService(String, String),
removeService(String, String)protected void send(ServerSession toClient, String onChannel, Object data, String id)
Sends data to an individual remote client.
The data passed is sent to the client as the "data" member of a message with the given channel and id. The message is not published on the channel and is thus not broadcast to all channel subscribers, but instead delivered directly to the target client.
Typically this method is only required if a service method sends response(s) to clients other than the sender, or on different channels. If the response is to be sent to the sender on the same channel, then the data can simply be the return value of the method.
toClient - The target clientonChannel - The channel of the messagedata - The data of the messageid - The id of the message (or null for a random id).protected void exception(String method, ServerSession fromClient, LocalSession toClient, ServerMessage msg, Throwable x)
Handles exceptions during the invocation of a mapped method.
This method is called when a mapped method throws and exception while handling a message.
method - the name of the method invoked that threw an exceptionfromClient - the remote session that sent the messagetoClient - the local session associated to this servicemsg - the message sent by the remote sessionx - the exception thrownprotected void doInvoke(Method method, ServerSession fromClient, ServerMessage msg)
Copyright © 2008-2013 Dojo Foundation. All Rights Reserved.