Class ActorSystem

java.lang.Object
com.scivicslab.pojoactor.core.ActorSystem
Direct Known Subclasses:
IIActorSystem

public class ActorSystem extends Object
A lightweight actor system implementation that manages actor lifecycles and thread pools. This system provides a framework for creating and managing actors with concurrent message processing.
Since:
1.0.0
Author:
devteam@scivics-lab.com
  • Field Details

  • Constructor Details

    • ActorSystem

      public ActorSystem(String systemName)
      Constructs an ActorSystem with the specified name and default thread pool.
      Parameters:
      systemName - the name of the actor system
    • ActorSystem

      public ActorSystem(String systemName, int threadNum)
      Constructs an ActorSystem with the specified name and thread count.
      Parameters:
      systemName - the name of the actor system
      threadNum - the number of threads in the worker pool
  • Method Details

    • terminate

      public void terminate()
      Terminates the actor system by closing all actors and shutting down thread pools. Waits up to 60 seconds for graceful termination.
    • actorOf

      public <T> ActorRef<T> actorOf(String actorName, T object)
      Creates a new actor with the specified name and object.
      Type Parameters:
      T - the type of the actor object
      Parameters:
      actorName - the unique name for the actor
      object - the object that will handle messages for this actor
      Returns:
      a reference to the created actor
    • addActor

      @Deprecated public <T> ActorRef<T> addActor(String actorName, ActorRef<T> actor)
      Deprecated.
      Use addActor(ActorRef) instead
      Adds an existing actor to the system with the specified name.
      Type Parameters:
      T - the type of the actor object
      Parameters:
      actorName - the name to associate with the actor
      actor - the actor reference to add
      Returns:
      the added actor reference
    • addActor

      public <T> ActorRef<T> addActor(ActorRef<T> actor)
      Add the directly generated Actor to the ActorSystem.
      Parameters:
      actor - An ActorRef object to be added to the ActorSystem.
      Returns:
      The same ActorRef object as the argument.
    • addActor

      public <T> ActorRef<T> addActor(ActorRef<T> actor, String actorName)
      Add the directly generated Actor to the ActorSystem while changing the name used within the ActorSystem.
      Parameters:
      actor - An ActorRef object to be added to the ActorSystem.
      actorName - A name to be associated with the ActorRef object.
      Returns:
      The same ActorRef object as the argument.
    • addWorkStealingPool

      public boolean addWorkStealingPool(int threadNum)
      Adds a new work stealing pool to the actor system.
      Parameters:
      threadNum - the number of threads in the new pool
      Returns:
      true if the pool was successfully added
    • hasActor

      public boolean hasActor(String actorName)
      Checks if an actor with the specified name exists in the system.
      Parameters:
      actorName - the name of the actor to check
      Returns:
      true if the actor exists, false otherwise
    • initLogger

      public void initLogger(String loggerName)
      Initializes the system logger with the specified name.
      Parameters:
      loggerName - the name for the logger
    • isAlive

      public boolean isAlive()
      Checks if the actor system is alive (no thread pools are shut down).
      Returns:
      true if the system is alive, false otherwise
    • isAlive

      public boolean isAlive(String actorName)
      Checks if a specific actor is alive.
      Parameters:
      actorName - the name of the actor to check
      Returns:
      true if the actor exists and is alive, false otherwise
    • listActorNames

      public List<String> listActorNames()
      Returns a list of all actor names in the system.
      Returns:
      a list containing the names of all actors
    • removeActor

      public void removeActor(String actorName)
      Removes an actor from the system.
      Parameters:
      actorName - the name of the actor to remove
    • root

      @Deprecated public ActorRef<Root> root()
      Deprecated.
      Use getActor(String) with "ROOT" instead
      Returns the root actor of the system.
      Returns:
      the root actor reference
    • toString

      public String toString()
      Returns a string representation of the actor system.
      Overrides:
      toString in class Object
      Returns:
      a string containing the system name
    • setLogger

      public void setLogger(Logger logger)
      Sets the logger for the actor system.
      Parameters:
      logger - the logger instance to use
    • getLogger

      public Logger getLogger()
      Gets the current logger instance.
      Returns:
      the current logger
    • getActor

      public <T> ActorRef<T> getActor(String actorName)
      Retrieves an actor by name.
      Type Parameters:
      T - the type of the actor object
      Parameters:
      actorName - the name of the actor to retrieve
      Returns:
      the actor reference, or null if not found
    • getWorkStealingPool

      public ExecutorService getWorkStealingPool()
      Returns the default work stealing pool of the actor system.
      Returns:
      the default work stealing pool (index 0)
    • getWorkStealingPool

      public ExecutorService getWorkStealingPool(int n)
      Returns a work stealing pool at the specified index.
      Parameters:
      n - the index of the work stealing pool to return
      Returns:
      the work stealing pool at the specified index