Class ServerActor<CallMessage,V,CastMessage>
- java.lang.Object
-
- co.paralleluniverse.actors.ActorImpl<Message>
-
- co.paralleluniverse.actors.Actor<java.lang.Object,java.lang.Void>
-
- co.paralleluniverse.actors.behaviors.BehaviorActor
-
- co.paralleluniverse.actors.behaviors.ServerActor<CallMessage,V,CastMessage>
-
- All Implemented Interfaces:
ActorBuilder<java.lang.Object,java.lang.Void>
,Joinable<java.lang.Void>
,Port<java.lang.Object>
,PortAutoCloseable
,ReceivePort<java.lang.Object>
,Stranded
,SuspendableCallable<java.lang.Void>
,java.io.Serializable
,java.lang.AutoCloseable
- Direct Known Subclasses:
ProxyServerActor
public class ServerActor<CallMessage,V,CastMessage> extends BehaviorActor
Abehavior
implementing a server that responds to request messages. TheServerActor
's behavior is implemented by either 1) subclassing this class and overriding some or all of:init
,terminate
,handleCall
,handleCast
,handleInfo
, andhandleTimeout
; or 2) providing an instance ofServerHandler
which implements these methods to the constructor.- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class co.paralleluniverse.actors.ActorImpl
ActorImpl.ActorLifecycleListener
-
Nested classes/interfaces inherited from interface co.paralleluniverse.strands.channels.ReceivePort
ReceivePort.EOFException
-
-
Field Summary
Fields Modifier and Type Field Description protected static java.lang.Object
NULL_RETURN_VALUE
-
Fields inherited from class co.paralleluniverse.actors.ActorImpl
flightRecorder, ref
-
-
Constructor Summary
Constructors Constructor Description ServerActor()
Creates a new server actorServerActor(ServerHandler<CallMessage,V,CastMessage> server)
Creates a new server actorServerActor(ServerHandler<CallMessage,V,CastMessage> server, MailboxConfig mailboxConfig)
Creates a new server actorServerActor(MailboxConfig mailboxConfig)
Creates a new server actorServerActor(java.lang.String name)
Creates a new server actorServerActor(java.lang.String name, ServerHandler<CallMessage,V,CastMessage> server)
Creates a new server actorServerActor(java.lang.String name, ServerHandler<CallMessage,V,CastMessage> server, long timeout, java.util.concurrent.TimeUnit unit, Strand strand, MailboxConfig mailboxConfig)
Creates a new server actorServerActor(java.lang.String name, ServerHandler<CallMessage,V,CastMessage> server, MailboxConfig mailboxConfig)
Creates a new server actorServerActor(java.lang.String name, MailboxConfig mailboxConfig)
Creates a new server actor
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
behavior()
Called byBehaviorActor.doRun()
as the body of the logic.static <CallMessage,V,CastMessage>
ServerActor<CallMessage,V,CastMessage>currentServerActor()
protected V
handleCall(ActorRef<?> from, java.lang.Object id, CallMessage m)
Called to handle a synchronous request (one waiting for a response).protected void
handleCast(ActorRef<?> from, java.lang.Object id, CastMessage m)
Called to handle an asynchronous request (one that does not for a response).protected void
handleInfo(java.lang.Object m)
protected void
handleMessage(java.lang.Object m)
Called by the defaultBehaviorActor.behavior()
method to handle each incoming message.protected void
handleTimeout()
Called whenever the timeout set withsetTimeout
or supplied at construction expires without any message received.org.slf4j.Logger
log()
TheLogger
object associated with this actor.protected Server<CallMessage,V,CastMessage>
makeRef(ActorRef<java.lang.Object> ref)
Server<CallMessage,V,CastMessage>
ref()
Returns the ActorRef to this actor, if it has been started.void
reply(ActorRef<?> to, java.lang.Object id, V value)
Replies with a result to a call request, if thehandleCall
method returned null.void
replyError(ActorRef<?> to, java.lang.Object id, java.lang.Throwable error)
Replies with an exception to a call request, if thehandleCall
method returned null.protected Server<CallMessage,V,CastMessage>
self()
Returns the ActorRef to this actor, if it has been started.protected ServerHandler<CallMessage,V,CastMessage>
server()
TheServerHandler
passed at construction, ornull
if none was set.void
setTimeout(long timeout, java.util.concurrent.TimeUnit unit)
Sets a duration after which, if a request has not been received, thehandleTimeout()
method will be called.void
shutdown()
Causes this actor to shut down.Server<CallMessage,V,CastMessage>
spawn()
Starts a new fiber and runs the actor in it.Server<CallMessage,V,CastMessage>
spawn(FiberFactory ff)
Starts a new fiber using the given scheduler and runs the actor in it.Server<CallMessage,V,CastMessage>
spawn(StrandFactory sf)
Starts a new fiber using the given scheduler and runs the actor in it.Server<CallMessage,V,CastMessage>
spawnThread()
Starts a new thread and runs the actor in it.-
Methods inherited from class co.paralleluniverse.actors.behaviors.BehaviorActor
checkCodeSwap, doRun, getInitializer, handleLifecycleMessage, init, isRunning, onStart, onTerminate, terminate
-
Methods inherited from class co.paralleluniverse.actors.Actor
addLifecycleListener, build, checkThrownIn, close, currentActor, filterMessage, get, get, getActor, getDeathCause, getMonitor, getName, getQueueLength, getStrand, hire, hire, internalSend, internalSendNonSuspendable, interrupt, isClosed, isDone, isInActor, isRegistered, isStarted, join, join, link, linked, mailbox, migrate, migrateAndRestart, monitor, monitorAddDeath, monitorAddMessage, monitorResetSkippedMessages, monitorSkippedMessage, newActor, newActor, onCodeChange, readResolve, receive, receive, receive, register, register, reinstantiate, removeLifecycleListener, removeObserverListeners, run, sendSync, setForwardWatch, setMonitor, setName, setStrand, start, stopMonitor, throwIn, toString, tryReceive, trySend, unlink, unlinked, unregister, unwatch, verifyInActor, verifyOnActorStrand, watch, writeReplace
-
Methods inherited from class co.paralleluniverse.actors.ActorImpl
close, getLifecycleListener, getMailbox, isRecordingLevel, record, record, record, record, record, record, record, sendOrInterrupt
-
-
-
-
Constructor Detail
-
ServerActor
public ServerActor(java.lang.String name, ServerHandler<CallMessage,V,CastMessage> server, long timeout, java.util.concurrent.TimeUnit unit, Strand strand, MailboxConfig mailboxConfig)
Creates a new server actor- Parameters:
name
- the actor name (may benull
).server
- an optional delegate object that implements this server actor's behavior if this class is not subclassed. May benull
.timeout
- the duration after which, if a request has not been received, thehandleTimeout()
method will be called.unit
-timeout
's time unit.null
if no timeout is to be set.strand
- this actor's strand.mailboxConfig
- this actor's mailbox settings.
-
ServerActor
public ServerActor(java.lang.String name, ServerHandler<CallMessage,V,CastMessage> server, MailboxConfig mailboxConfig)
Creates a new server actor- Parameters:
name
- the actor name (may benull
).server
- an optional delegate object that implements this server actor's behavior if this class is not subclassed. May benull
.mailboxConfig
- this actor's mailbox settings.
-
ServerActor
public ServerActor(java.lang.String name, ServerHandler<CallMessage,V,CastMessage> server)
Creates a new server actor- Parameters:
name
- the actor name (may benull
).server
- an optional delegate object that implements this server actor's behavior if this class is not subclassed. May benull
.
-
ServerActor
public ServerActor(ServerHandler<CallMessage,V,CastMessage> server, MailboxConfig mailboxConfig)
Creates a new server actor- Parameters:
server
- an optional delegate object that implements this server actor's behavior if this class is not subclassed. May benull
.mailboxConfig
- this actor's mailbox settings.
-
ServerActor
public ServerActor(ServerHandler<CallMessage,V,CastMessage> server)
Creates a new server actor- Parameters:
server
- an optional delegate object that implements this server actor's behavior if this class is not subclassed. May benull
.
-
ServerActor
public ServerActor(java.lang.String name, MailboxConfig mailboxConfig)
Creates a new server actor- Parameters:
name
- the actor name (may benull
).mailboxConfig
- this actor's mailbox settings.
-
ServerActor
public ServerActor(java.lang.String name)
Creates a new server actor- Parameters:
name
- the actor name (may benull
).
-
ServerActor
public ServerActor(MailboxConfig mailboxConfig)
Creates a new server actor- Parameters:
mailboxConfig
- this actor's mailbox settings.
-
ServerActor
public ServerActor()
Creates a new server actor
-
-
Method Detail
-
makeRef
protected Server<CallMessage,V,CastMessage> makeRef(ActorRef<java.lang.Object> ref)
- Overrides:
makeRef
in classBehaviorActor
-
ref
public Server<CallMessage,V,CastMessage> ref()
Description copied from class:Actor
Returns the ActorRef to this actor, if it has been started.- Overrides:
ref
in classBehaviorActor
- Returns:
- the ActorRef of this actor if it has been started, or
null
otherwise.
-
self
protected Server<CallMessage,V,CastMessage> self()
Description copied from class:Actor
Returns the ActorRef to this actor, if it has been started.- Overrides:
self
in classBehaviorActor
- Returns:
- the ActorRef of this actor if it has been started, or
null
otherwise.
-
spawn
public Server<CallMessage,V,CastMessage> spawn(StrandFactory sf)
Description copied from class:Actor
Starts a new fiber using the given scheduler and runs the actor in it. The fiber's name will be set to this actor's name.- Overrides:
spawn
in classBehaviorActor
- Parameters:
sf
- thefactory
(orscheduler
) that will be used to create the actor's fiber.- Returns:
- This actors' ActorRef
-
spawn
public Server<CallMessage,V,CastMessage> spawn(FiberFactory ff)
Description copied from class:Actor
Starts a new fiber using the given scheduler and runs the actor in it. The fiber's name will be set to this actor's name.- Overrides:
spawn
in classBehaviorActor
- Parameters:
ff
- thefactory
(orscheduler
) that will be used to create the actor's fiber.- Returns:
- This actors' ActorRef
-
spawn
public Server<CallMessage,V,CastMessage> spawn()
Description copied from class:Actor
Starts a new fiber and runs the actor in it. The fiber's name will be set to this actor's name.- Overrides:
spawn
in classBehaviorActor
- Returns:
- This actors' ActorRef
-
spawnThread
public Server<CallMessage,V,CastMessage> spawnThread()
Description copied from class:Actor
Starts a new thread and runs the actor in it. The fiber's name will be set to this actor's name.- Overrides:
spawnThread
in classBehaviorActor
- Returns:
- This actors' ActorRef
-
currentServerActor
public static <CallMessage,V,CastMessage> ServerActor<CallMessage,V,CastMessage> currentServerActor()
-
log
public org.slf4j.Logger log()
Description copied from class:BehaviorActor
TheLogger
object associated with this actor.- Specified by:
log
in classBehaviorActor
-
server
protected ServerHandler<CallMessage,V,CastMessage> server()
TheServerHandler
passed at construction, ornull
if none was set.
-
behavior
protected final void behavior() throws java.lang.InterruptedException, SuspendExecution
Description copied from class:BehaviorActor
Called byBehaviorActor.doRun()
as the body of the logic. By default, this implementation runs code similar to:while (isRunning()) handleMessage(receive());
- Overrides:
behavior
in classBehaviorActor
- Throws:
java.lang.InterruptedException
SuspendExecution
-
handleMessage
protected void handleMessage(java.lang.Object m) throws java.lang.InterruptedException, SuspendExecution
Called by the defaultBehaviorActor.behavior()
method to handle each incoming message. By default, this method does nothing.This method implements the
ServerActor
behavior, dispatching message tohandleCall
,handleCast
orhandleInfo
as appropriate.- Overrides:
handleMessage
in classBehaviorActor
- Parameters:
m
- the message received by the actor.- Throws:
java.lang.InterruptedException
SuspendExecution
-
shutdown
public void shutdown()
Description copied from class:BehaviorActor
Causes this actor to shut down.- Overrides:
shutdown
in classBehaviorActor
-
setTimeout
public final void setTimeout(long timeout, java.util.concurrent.TimeUnit unit)
Sets a duration after which, if a request has not been received, thehandleTimeout()
method will be called. The time count is reset after every received message. This method will be triggered multiple times if a message is not received for a period of time longer than multiple timeout durations.- Parameters:
timeout
- the timeout durationunit
-timeout
's time unit;null
if the timeout is to be unset.
-
handleCall
protected V handleCall(ActorRef<?> from, java.lang.Object id, CallMessage m) throws java.lang.Exception, SuspendExecution
Called to handle a synchronous request (one waiting for a response).By default, this method calls
server
.handleCall
if a server object was supplied at construction time. Otherwise, it throws anUnsupportedOperationException
, which will be sent back to the requester.- If this method returns a non-null value, it will be sent back to the sender of the request wrapped by an
ErrorResponseMessage
; if the request was sent viaServer.call
(which is how it's usually done), this value will be returned by thecall
method. - If this method throws an exception, it will be sent back to the sender of the request wrapped by an
ErrorResponseMessage
; if the request was sent viaServer.call
, the exception will be thrown by thecall
method, possibly wrapped in aRuntimeException
. - If this method returns
null
, then a reply is not immediately sent, and thecall
method will remain blocked until a reply is sent manually withreply
orreplyError
.
- Parameters:
from
- the sender of the requestid
- the request's unique idm
- the request- Returns:
- a value that will be sent as a response to the sender of the request.
- Throws:
java.lang.Exception
- if thrown, it will be sent back to the sender of the request.SuspendExecution
- If this method returns a non-null value, it will be sent back to the sender of the request wrapped by an
-
reply
public final void reply(ActorRef<?> to, java.lang.Object id, V value) throws SuspendExecution
Replies with a result to a call request, if thehandleCall
method returned null.If the request has been sent by a call to
Server.call
(which is how it's usually done), theresult
argument will be the value returned bycall
.This method can only be called by this actor.
Internally this method uses a
ValueResponseMessage
to send the reply.- Parameters:
to
- the actor we're responding to (the request's sender)id
- the request's identifiervalue
- the result of the request- Throws:
SuspendExecution
-
replyError
public final void replyError(ActorRef<?> to, java.lang.Object id, java.lang.Throwable error) throws SuspendExecution
Replies with an exception to a call request, if thehandleCall
method returned null. If the request has been sent by a call toServer.call
(which is how it's usually done), thee
argument will be the exception thrown bycall
(possibly wrapped by aRuntimeException
).This method can only be called by this actor.
Internally this method uses an
ErrorResponseMessage
to send the reply.- Parameters:
to
- the actor we're responding to (the request's sender)id
- the request's identifiererror
- the error the request has caused- Throws:
SuspendExecution
-
handleCast
protected void handleCast(ActorRef<?> from, java.lang.Object id, CastMessage m) throws SuspendExecution
Called to handle an asynchronous request (one that does not for a response).By default, this method calls
server
.handleCast
if a server object was supplied at construction time. Otherwise, it throws anUnsupportedOperationException
, which will result in this actor's death, unless caught.- Parameters:
from
- the sender of the requestid
- the request's unique idm
- the request- Throws:
SuspendExecution
-
handleInfo
protected void handleInfo(java.lang.Object m) throws SuspendExecution
Called to handle any message sent to this actor that is neither acall
nor acast
.By default, this method calls
server
.handleInfo
if a server object was supplied at construction time. Otherwise, it does nothing.- Parameters:
m
- the message- Throws:
SuspendExecution
-
handleTimeout
protected void handleTimeout() throws SuspendExecution
Called whenever the timeout set withsetTimeout
or supplied at construction expires without any message received. The countdown is reset after every received message. This method will be triggered multiple times if a message is not received for a period of time longer than multiple timeout durations.By default, this method calls
server
.handleTimeout
if a server object was supplied at construction time. Otherwise, it does nothing.- Throws:
SuspendExecution
-
-