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, ServerMessage.Mutable, Promise)
or
ServerSession.deliver(Session, ServerMessage.Mutable, Promise)
.
BayeuxServer.newLocalSession(String)
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 session,
ServerMessage message) |
protected void |
exception(String method,
ServerSession session,
LocalSession local,
ServerMessage message,
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)
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 the following signature:
myMethod(ServerSession from, ServerMessage message)
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(org.cometd.bayeux.server.ServerSession, String, Object)
to deliver message(s) to specific clients and/or channels.
A mapped method may also publish to different channels via
ServerChannel.publish(Session, ServerMessage.Mutable, Promise)
.
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)
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. 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 messageprotected void exception(String method, ServerSession session, LocalSession local, ServerMessage message, 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 exceptionsession
- the remote session that sent the messagelocal
- the local session associated to this servicemessage
- the message sent by the remote sessionx
- the exception thrownprotected void doInvoke(Method method, ServerSession session, ServerMessage message)
Copyright © 2008–2024 The CometD Project. All rights reserved.