concept actor in category system administration

appears as: Actors, ctors, An actor, n actor, actor, The actor, Actor, actors, ctor
Functional and Reactive Domain Modeling

This is an excerpt from Manning's book Functional and Reactive Domain Modeling.

This book focuses on messaging systems using the actor model of computation. Actors provide a fairly high level of concurrency construct that helps you organize your model in terms of loosely coupled modules (they can be bounded contexts as well) that interact using asynchronous messages. We’ll discuss how a well-engineered actor system can provide resilience to your model, help you scale out and back through proper handling of back pressure, and provide overall responsiveness to your system.

Akka actors aren’t type-safe in their behavior. If you look at the contract for receive, which is the primary method that defines the behavior of the actor, it’s defined as PartialFunction[Any, Unit], which means that you don’t have any static guarantee of what an actor might end up doing. And there are a couple of reasons for this design:

  • FSM— Akka actors can model a finite-state machine (FSM) by using the become method, which implies that the actor should be able to transform its behavior depending on the current state and the message that it receives. This necessitates an intrinsically dynamic behavioral contract. You have more power at the expense of type safety. Keep this in mind when you design your APIs, and try to make them reactive by using Akka actors. This isn’t universally evil; there are valid use cases for using this additional power to implement state machines in a succinct, domain-specific language.
  • Distribution— Akka actors can be distributed across nodes transparently. This requires the ability for actors to be dynamically serializable and hence becomes difficult to impose any static typing guarantees on them.
  • Lack of type safety and composability make actors a not-so-good choice for designing end-user APIs, especially when you’re using a statically typed programming language such as Scala. But for some specific use cases, you can have actor-based implementation of some end-user APIs. This is especially true when you need to enforce mutual exclusion on a mutable state, and it works like a charm because every actor follows a single-threaded model of execution. This is one of the most recommended use cases for using actors.

    Consider the following example, where you’d need to track the maximum valued debit transaction from an asynchronous stream of messages. You can model this tracker as an actor with the maximum valued transaction seen so far being kept as a mutable state. With every message that you receive, you do the checks and change the maximum value if required:

    class MaxTransactionTracker extends Actor {
      var max: Transaction = _
    
      def receive = {
        case t @ Transaction(_, _, transType, amount, _) =>
          if (transType == Debit && amount > max.amount) max = t
        case MaxTransactionSoFar => { sender ! max }
      }
    }

    You can now define an end-user API, where the implementation of the API is based on the preceding actor. The actor ensures that your mutable state (the maximum valued transaction) is protected through the single thread of execution and is published only externally through a message to the sender.

    In some use cases, you’ll find a strong parallel between implementing some behaviors through actors and through futures. Consider the following example of an actor that fetches the portfolio of client accounts for specific instruments:

    Open Source SOA

    This is an excerpt from Manning's book Open Source SOA.

    One source of confusion that often arises is the interplay between the jBPM identity component that’s used by the jBPM Console and the actors within a process. The identity component that comes with jBPM Console is used only for purposes of managing the login process and for controlling the permission-based menu options that are used within jBPM Console. It’s not referenced, directly or otherwise, by the process engine itself (with one apparent caveat: email resolution requires it). For example, when you log in to jBPM Console, the identity component is used to validate your credentials. Then, once completed, your username acts as your actorId for determining which tasks you’ve been assigned. The identity component also uses groups to determine which menu options exist through jBPM Console, such as user, manager, and admin. A user is associated with one or more of these groups through a membership. Thus, the identity component is used by the jBPM Console to determine which menu options and privileges the user has, and effectively associates the user’s login username with an actorId when interacting with the jBPM engine. The jBPM User Guide and some community forum entries describe, in some detail, how you can replace the default identity component with a custom alternative.

    While assigning the actors within a jPDL is simple, in many cases a dynamic assignment may be required. To further expand on our example of the PO process, the individual assigned to the task would likely depend on whom the requestor is reporting, from an organizational perspective. Perhaps jdoe would require djohnson’s approval, but msmith would need ltaylor. While you could attempt to create separate process flows to accommodate this, a far easier solution would be to use a custom assignment class or script. This is referred to as creating a custom assignment handler. An assignment handler class must implement the AssignmentHandler interface, and with it the required method assign. Listing 6.1 shows a simple assignment handler that implements the rules we defined earlier (this class can also found in the sample code).

    Listing 6.1. An example of a jBPM AssignmentHandler class

    In the highlighted code, you see how we are just hard-coding (for demonstration purposes) some logic for how to perform an assignment. In the first case, we’re simply checking to see whether the submitter’s name (submitter was retrieved as a process variable) is jdoe, and if so, we assign the task to djohnson (using the setActorId method). In a real-life scenario, a lookup to an LDAP directory server might be used to retrieve the organizational approvals that are required. Also, instead of using Assignable.setActorId(), you could just as easily assign a pooled group of actors using Assignable.setPooledActors(), which takes an array of actorId Strings.

    The TaskMgmtSession.findPooledTaskInstances method won’t return a task result if the task has already been assigned from the pool of users to a single actor. The purpose of this call is to identify unassigned tasks where an actor is a principal.