co.paralleluniverse.actors.behaviors

Class ServerActor<CallMessage,V,CastMessage>

    • Field Detail

      • NULL_RETURN_VALUE

        protected static Object NULL_RETURN_VALUE
    • Constructor Detail

      • ServerActor

        public ServerActor(String name,
                   ServerHandler<CallMessage,V,CastMessage> server,
                   long timeout,
                   TimeUnit unit,
                   Strand strand,
                   MailboxConfig mailboxConfig)
        Creates a new server actor
        Parameters:
        name - the actor name (may be null).
        server - an optional delegate object that implements this server actor's behavior if this class is not subclassed. May be null.
        timeout - the duration after which, if a request has not been received, the handleTimeout() 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(String name,
                   ServerHandler<CallMessage,V,CastMessage> server,
                   MailboxConfig mailboxConfig)
        Creates a new server actor
        Parameters:
        name - the actor name (may be null).
        server - an optional delegate object that implements this server actor's behavior if this class is not subclassed. May be null.
        mailboxConfig - this actor's mailbox settings.
      • ServerActor

        public ServerActor(String name,
                   ServerHandler<CallMessage,V,CastMessage> server)
        Creates a new server actor
        Parameters:
        name - the actor name (may be null).
        server - an optional delegate object that implements this server actor's behavior if this class is not subclassed. May be null.
      • 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 be null.
        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 be null.
      • ServerActor

        public ServerActor(String name,
                   MailboxConfig mailboxConfig)
        Creates a new server actor
        Parameters:
        name - the actor name (may be null).
        mailboxConfig - this actor's mailbox settings.
      • ServerActor

        public ServerActor(String name)
        Creates a new server actor
        Parameters:
        name - the actor name (may be null).
      • 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

      • 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 class BehaviorActor
        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 class BehaviorActor
        Returns:
        the ActorRef of this actor if it has been started, or null otherwise.
      • 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 class BehaviorActor
        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
        The Logger object associated with this actor.
        Specified by:
        log in class BehaviorActor
      • setTimeout

        public final void setTimeout(long timeout,
                      TimeUnit unit)
        Sets a duration after which, if a request has not been received, the handleTimeout() 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 duration
        unit - timeout's time unit; null if the timeout is to be unset.
      • handleCall

        protected V handleCall(ActorRef<?> from,
                   Object id,
                   CallMessage m)
                        throws 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 an UnsupportedOperationException, 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 via Server.call (which is how it's usually done), this value will be returned by the call 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 via Server.call, the exception will be thrown by the call method, possibly wrapped in a RuntimeException.
        • If this method returns null, then a reply is not immediately sent, and the call method will remain blocked until a reply is sent manually with reply or replyError.
        Parameters:
        from - the sender of the request
        id - the request's unique id
        m - the request
        Returns:
        a value that will be sent as a response to the sender of the request.
        Throws:
        Exception - if thrown, it will be sent back to the sender of the request.
        SuspendExecution
      • reply

        public final void reply(ActorRef<?> to,
                 Object id,
                 V value)
                         throws SuspendExecution
        Replies with a result to a call request, if the handleCall method returned null.

        If the request has been sent by a call to Server.call (which is how it's usually done), the result argument will be the value returned by call.

        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 identifier
        value - the result of the request
        Throws:
        SuspendExecution
      • replyError

        public final void replyError(ActorRef<?> to,
                      Object id,
                      Throwable error)
                              throws SuspendExecution
        Replies with an exception to a call request, if the handleCall method returned null. If the request has been sent by a call to Server.call (which is how it's usually done), the e argument will be the exception thrown by call (possibly wrapped by a RuntimeException).

        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 identifier
        error - the error the request has caused
        Throws:
        SuspendExecution
      • handleCast

        protected void handleCast(ActorRef<?> from,
                      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 an UnsupportedOperationException, which will result in this actor's death, unless caught.

        Parameters:
        from - the sender of the request
        id - the request's unique id
        m - the request
        Throws:
        SuspendExecution
      • handleInfo

        protected void handleInfo(Object m)
                           throws SuspendExecution
        Called to handle any message sent to this actor that is neither a call nor a cast.

        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 with setTimeout 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