java.lang.Object
com.softwaremill.jox.structured.ActorRef<T>

public class ActorRef<T> extends Object
  • Constructor Details

  • Method Details

    • ask

      public <U> U ask(ThrowingFunction<T,U> f) throws Exception
      Send an invocation to the actor and await for the result.

      The `f` function should be an invocation of a method on `T` and should not directly or indirectly return the `T` value, as this might expose the actor's internal mutable state to other threads.

      Any non-fatal exceptions thrown by `f` will be propagated to the caller and the actor will continue processing other invocations. Fatal exceptions will be propagated to the actor's enclosing scope, and the actor will close.

      Throws:
      Exception
    • tell

      public void tell(ThrowingConsumer<T> f) throws InterruptedException
      Send an invocation to the actor that should be processed in the background (fire-and-forget). Might block until there's enough space in the actor's mailbox (incoming channel).

      Any exceptions thrown by `f` will be propagated to the actor's enclosing scope, and the actor will close.

      Throws:
      InterruptedException
    • create

      public static <T> ActorRef<T> create(Scope scope, T logic)
      The same as create(Scope, Object, Consumer) but with empty close action.
    • create

      public static <T> ActorRef<T> create(Scope scope, T logic, Consumer<T> close)
      Creates a new actor ref, that is a fork in the current concurrency scope, which protects a mutable resource (`logic`) and executes invocations on it serially, one after another. It is guaranteed that `logic` will be accessed by at most one thread at a time. The methods of `logic: T` define the actor's interface (the messages that can be "sent to the actor").

      Invocations can be scheduled using the returned `ActorRef`. When an invocation is an `ActorRef.ask`, any non-fatal exceptions are propagated to the caller, and the actor continues. Fatal exceptions, or exceptions that occur during `ActorRef.tell` invocations, cause the actor's channel to be closed with an error, and are propagated to the enclosing scope.

      The actor's mailbox (incoming channel) will have a capacity of Channel.DEFAULT_BUFFER_SIZE.