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
Abehaviorimplementing 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 ofServerHandlerwhich 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.ObjectNULL_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 voidbehavior()Called byBehaviorActor.doRun()as the body of the logic.static <CallMessage,V,CastMessage>
ServerActor<CallMessage,V,CastMessage>currentServerActor()protected VhandleCall(ActorRef<?> from, java.lang.Object id, CallMessage m)Called to handle a synchronous request (one waiting for a response).protected voidhandleCast(ActorRef<?> from, java.lang.Object id, CastMessage m)Called to handle an asynchronous request (one that does not for a response).protected voidhandleInfo(java.lang.Object m)protected voidhandleMessage(java.lang.Object m)Called by the defaultBehaviorActor.behavior()method to handle each incoming message.protected voidhandleTimeout()Called whenever the timeout set withsetTimeoutor supplied at construction expires without any message received.org.slf4j.Loggerlog()TheLoggerobject 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.voidreply(ActorRef<?> to, java.lang.Object id, V value)Replies with a result to a call request, if thehandleCallmethod returned null.voidreplyError(ActorRef<?> to, java.lang.Object id, java.lang.Throwable error)Replies with an exception to a call request, if thehandleCallmethod 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()TheServerHandlerpassed at construction, ornullif none was set.voidsetTimeout(long timeout, java.util.concurrent.TimeUnit unit)Sets a duration after which, if a request has not been received, thehandleTimeout()method will be called.voidshutdown()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.nullif 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:
makeRefin classBehaviorActor
-
ref
public Server<CallMessage,V,CastMessage> ref()
Description copied from class:ActorReturns the ActorRef to this actor, if it has been started.- Overrides:
refin classBehaviorActor- Returns:
- the ActorRef of this actor if it has been started, or
nullotherwise.
-
self
protected Server<CallMessage,V,CastMessage> self()
Description copied from class:ActorReturns the ActorRef to this actor, if it has been started.- Overrides:
selfin classBehaviorActor- Returns:
- the ActorRef of this actor if it has been started, or
nullotherwise.
-
spawn
public Server<CallMessage,V,CastMessage> spawn(StrandFactory sf)
Description copied from class:ActorStarts 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:
spawnin 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:ActorStarts 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:
spawnin 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:ActorStarts a new fiber and runs the actor in it. The fiber's name will be set to this actor's name.- Overrides:
spawnin classBehaviorActor- Returns:
- This actors' ActorRef
-
spawnThread
public Server<CallMessage,V,CastMessage> spawnThread()
Description copied from class:ActorStarts a new thread and runs the actor in it. The fiber's name will be set to this actor's name.- Overrides:
spawnThreadin 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:BehaviorActorTheLoggerobject associated with this actor.- Specified by:
login classBehaviorActor
-
server
protected ServerHandler<CallMessage,V,CastMessage> server()
TheServerHandlerpassed at construction, ornullif none was set.
-
behavior
protected final void behavior() throws java.lang.InterruptedException, SuspendExecutionDescription copied from class:BehaviorActorCalled byBehaviorActor.doRun()as the body of the logic. By default, this implementation runs code similar to:while (isRunning()) handleMessage(receive());- Overrides:
behaviorin classBehaviorActor- Throws:
java.lang.InterruptedExceptionSuspendExecution
-
handleMessage
protected void handleMessage(java.lang.Object m) throws java.lang.InterruptedException, SuspendExecutionCalled by the defaultBehaviorActor.behavior()method to handle each incoming message. By default, this method does nothing.This method implements the
ServerActorbehavior, dispatching message tohandleCall,handleCastorhandleInfoas appropriate.- Overrides:
handleMessagein classBehaviorActor- Parameters:
m- the message received by the actor.- Throws:
java.lang.InterruptedExceptionSuspendExecution
-
shutdown
public void shutdown()
Description copied from class:BehaviorActorCauses this actor to shut down.- Overrides:
shutdownin 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;nullif 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.handleCallif 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 thecallmethod. - 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 thecallmethod, possibly wrapped in aRuntimeException. - If this method returns
null, then a reply is not immediately sent, and thecallmethod will remain blocked until a reply is sent manually withreplyorreplyError.
- 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 thehandleCallmethod returned null.If the request has been sent by a call to
Server.call(which is how it's usually done), theresultargument will be the value returned bycall.This method can only be called by this actor.
Internally this method uses a
ValueResponseMessageto 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 thehandleCallmethod returned null. If the request has been sent by a call toServer.call(which is how it's usually done), theeargument will be the exception thrown bycall(possibly wrapped by aRuntimeException).This method can only be called by this actor.
Internally this method uses an
ErrorResponseMessageto 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.handleCastif 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 SuspendExecutionCalled to handle any message sent to this actor that is neither acallnor acast.By default, this method calls
server.handleInfoif a server object was supplied at construction time. Otherwise, it does nothing.- Parameters:
m- the message- Throws:
SuspendExecution
-
handleTimeout
protected void handleTimeout() throws SuspendExecutionCalled whenever the timeout set withsetTimeoutor 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.handleTimeoutif a server object was supplied at construction time. Otherwise, it does nothing.- Throws:
SuspendExecution
-
-