Class WorkflowScheduler
- All Implemented Interfaces:
CallableByActionName,AutoCloseable
This class provides scheduling capabilities for workflow actors using
action names and string arguments. Tasks are executed on actors via
IIActorRef.callByActionName(String, String).
Usage Example:
IIActorSystem system = new IIActorSystem("my-system");
WorkflowScheduler scheduler = new WorkflowScheduler(system);
// Schedule a task to run every 10 seconds
scheduler.scheduleAtFixedRate("health-check", "serverActor", "checkHealth", "", 0, 10, TimeUnit.SECONDS);
// Cancel a task
scheduler.cancelTask("health-check");
// Cleanup
scheduler.close();
For lambda-based scheduling with ActorRef, use
Scheduler instead.
- Since:
- 2.11.0
- Author:
- devteam@scivics-lab.com
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionWorkflowScheduler(IIActorSystem actorSystem) Constructs a new WorkflowScheduler with the specified actor system.WorkflowScheduler(IIActorSystem actorSystem, int poolSize) Constructs a new WorkflowScheduler with the specified actor system and pool size. -
Method Summary
Modifier and TypeMethodDescriptioncallByActionName(String actionName, String arg) Executes an action by name with the given arguments.booleancancelTask(String taskId) Cancels a scheduled task.voidclose()Shuts down the scheduler and cancels all scheduled tasks.intReturns the number of currently scheduled tasks.booleanisScheduled(String taskId) Checks if a task with the given ID is currently scheduled.scheduleAtFixedRate(String taskId, String targetActorName, String actionName, String actionArgs, long initialDelay, long period, TimeUnit unit) Schedules a task to execute periodically at a fixed rate.scheduleOnce(String taskId, String targetActorName, String actionName, String actionArgs, long delay, TimeUnit unit) Schedules a task to execute once after a specified delay.scheduleWithFixedDelay(String taskId, String targetActorName, String actionName, String actionArgs, long initialDelay, long delay, TimeUnit unit) Schedules a task to execute periodically with a fixed delay between executions.
-
Constructor Details
-
WorkflowScheduler
Constructs a new WorkflowScheduler with the specified actor system.- Parameters:
actorSystem- the actor system containing target actors
-
WorkflowScheduler
Constructs a new WorkflowScheduler with the specified actor system and pool size.- Parameters:
actorSystem- the actor system containing target actorspoolSize- the number of threads in the scheduler's thread pool
-
-
Method Details
-
scheduleAtFixedRate
public String scheduleAtFixedRate(String taskId, String targetActorName, String actionName, String actionArgs, long initialDelay, long period, TimeUnit unit) Schedules a task to execute periodically at a fixed rate.Fixed Rate vs Fixed Delay:
With
scheduleAtFixedRate, executions are scheduled to start at regular intervals (0, period, 2*period, 3*period, ...) regardless of how long each execution takes. If an execution takes longer than the period, the next execution starts immediately after the previous one completes (no delay accumulation).Use this method when you need consistent timing intervals, such as metrics collection or heartbeat checks at precise intervals.
In contrast,
scheduleWithFixedDelay(java.lang.String, java.lang.String, java.lang.String, java.lang.String, long, long, java.util.concurrent.TimeUnit)waits for a fixed delay after each execution completes before starting the next one.scheduleAtFixedRate (period=100ms, task takes 30ms): |task| |task| |task| 0 30 100 130 200 230 300ms └─period─┘ └─period─┘- Parameters:
taskId- unique identifier for this scheduled tasktargetActorName- name of the target actor in the systemactionName- action to invoke on the actoractionArgs- arguments to pass to the actioninitialDelay- delay before first executionperiod- interval between successive execution startsunit- time unit for the delays- Returns:
- the taskId for reference
- See Also:
-
scheduleWithFixedDelay
public String scheduleWithFixedDelay(String taskId, String targetActorName, String actionName, String actionArgs, long initialDelay, long delay, TimeUnit unit) Schedules a task to execute periodically with a fixed delay between executions.Fixed Delay vs Fixed Rate:
With
scheduleWithFixedDelay, the delay between the termination of one execution and the start of the next is alwaysdelaytime units. This ensures that executions never overlap and there is always a guaranteed rest period between executions.Use this method when you need to ensure a minimum gap between executions, such as polling operations where you want to avoid overwhelming a resource, or when each execution depends on external state that needs time to stabilize.
In contrast,
scheduleAtFixedRate(java.lang.String, java.lang.String, java.lang.String, java.lang.String, long, long, java.util.concurrent.TimeUnit)schedules executions at fixed intervals regardless of execution duration.scheduleWithFixedDelay (delay=100ms, task takes 30ms): |task| |task| |task| 0 30 130 160 260 290ms └──delay──┘ └──delay──┘- Parameters:
taskId- unique identifier for this scheduled tasktargetActorName- name of the target actor in the systemactionName- action to invoke on the actoractionArgs- arguments to pass to the actioninitialDelay- delay before first executiondelay- delay between termination of one execution and start of nextunit- time unit for the delays- Returns:
- the taskId for reference
- See Also:
-
scheduleOnce
public String scheduleOnce(String taskId, String targetActorName, String actionName, String actionArgs, long delay, TimeUnit unit) Schedules a task to execute once after a specified delay.- Parameters:
taskId- unique identifier for this scheduled tasktargetActorName- name of the target actor in the systemactionName- action to invoke on the actoractionArgs- arguments to pass to the actiondelay- delay before executionunit- time unit for the delay- Returns:
- the taskId for reference
-
cancelTask
Cancels a scheduled task.- Parameters:
taskId- identifier of the task to cancel- Returns:
- true if the task was found and cancelled, false otherwise
-
getScheduledTaskCount
public int getScheduledTaskCount()Returns the number of currently scheduled tasks.- Returns:
- the number of active scheduled tasks
-
isScheduled
Checks if a task with the given ID is currently scheduled.- Parameters:
taskId- the task identifier to check- Returns:
- true if the task exists and is not cancelled, false otherwise
-
close
public void close()Shuts down the scheduler and cancels all scheduled tasks.- Specified by:
closein interfaceAutoCloseable
-
callByActionName
Executes an action by name with the given arguments.Supported actions:
scheduleAtFixedRate- Args: taskId,targetActor,action,args,initialDelay,period,unitscheduleWithFixedDelay- Args: taskId,targetActor,action,args,initialDelay,delay,unitscheduleOnce- Args: taskId,targetActor,action,args,delay,unitcancel- Args: taskIdgetTaskCount- Args: (none)isScheduled- Args: taskId
- Specified by:
callByActionNamein interfaceCallableByActionName- Parameters:
actionName- the action to executearg- comma-separated arguments- Returns:
- ActionResult indicating success or failure
-