Class Scheduler
- java.lang.Object
-
- com.gurock.smartinspect.scheduler.Scheduler
-
public class Scheduler extends Object
Responsible for scheduling protocol operations and executing them asynchronously in a different thread of control.This class is used by the asynchronous protocol mode to asynchronously execute protocol operations. New commands can be scheduled for execution with the schedule method. The scheduler can be started and stopped with the start and stop methods. The scheduler uses a size limited queue to buffer scheduler commands. The maximum size of this queue can be set with the setThreshold method. To influence the behavior of the scheduler if new commands are enqueued and the queue is currently considered full, you can specify the throttle mode.
This class is guaranteed to be threadsafe.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Removes all scheduler commands from this scheduler.longgetThreshold()Returns the maximum size of the scheduler command queue.booleangetThrottle()Indicates if the scheduler should automatically throttle threads that enqueue new scheduler commands.booleanschedule(SchedulerCommand command, SchedulerQueue.QueueEnd insertTo)Schedules a new command for asynchronous execution.voidsetThreshold(long threshold)Sets the maximum size of the scheduler command queue.voidsetThrottle(boolean throttle)Specifies if the scheduler should automatically throttle threads that enqueue new scheduler commands.voidstart()Starts this scheduler and the internal scheduler thread.voidstop()Stops this scheduler and the internal scheduler thread.
-
-
-
Field Detail
-
logger
public static final Logger logger
-
-
Constructor Detail
-
Scheduler
public Scheduler(Protocol protocol)
Creates and initializes a new Scheduler instance.- Parameters:
protocol- The protocol on which to execute the actual operations like connect, disconnect, write or dispatch
-
-
Method Detail
-
start
public void start()
Starts this scheduler and the internal scheduler thread.This method must be called before scheduling new commands with the schedule method. Call stop to stop the internal thread when the scheduler is no longer needed. Note that this method starts the internal scheduler thread only once. This means that subsequent calls to this method have no effect.
-
stop
public void stop()
Stops this scheduler and the internal scheduler thread.This is the matching method for start. After calling this method, new commands will no longer be accepted by schedule and are ignored. This method blocks until the internal thread has processed the current content of the queue. Call clear before calling stop to exit the internal thread as soon as possible.
-
getThreshold
public long getThreshold()
Returns the maximum size of the scheduler command queue.To influence the behavior of the scheduler if new commands are enqueued and the queue is currently considered full, you can specify the
throttle mode.- Returns:
- The maximum size of the scheduler command queue
-
setThreshold
public void setThreshold(long threshold)
Sets the maximum size of the scheduler command queue.To influence the behavior of the scheduler if new commands are enqueued and the queue is currently considered full, you can specify the throttle mode.
- Parameters:
threshold- The new maximum size of the scheduler command queue
-
getThrottle
public boolean getThrottle()
Indicates if the scheduler should automatically throttle threads that enqueue new scheduler commands.If this method returns true and the queue is considered full when enqueuing new commands, the enqueuing thread is automatically throttled until there is room in the queue for the new command. In non-throttle mode, the thread is not blocked but older commands are removed from the queue.
- Returns:
- True if the scheduler should automatically throttle threads that enqueue new scheduler commands and false otherwise
-
setThrottle
public void setThrottle(boolean throttle)
Specifies if the scheduler should automatically throttle threads that enqueue new scheduler commands.If this method is passed true and the queue is considered full when enqueuing new commands, the enqueuing thread is automatically throttled until there is room in the queue for the new command. In non-throttle mode, the thread is not blocked but older commands are removed from the queue.
- Parameters:
throttle- Pass true if the scheduler should automatically throttle threads that enqueue new scheduler commands and false otherwise
-
schedule
public boolean schedule(SchedulerCommand command, SchedulerQueue.QueueEnd insertTo)
Schedules a new command for asynchronous execution. This method adds the passed command to the internal queue of scheduler commands. The command is eventually executed by the internal scheduler thread. This method can block the caller if the scheduler operates in throttle mode and the internal queue is currently considered full (seesetThreshold(long)).- Parameters:
command- The command to scheduleinsertTo- End of a queue to insert a command to- Returns:
- True if the command could be scheduled for asynchronous execution and false otherwise
-
clear
public void clear()
Removes all scheduler commands from this scheduler. This method clears the current queue of scheduler commands. If the stop method is called after calling clear and no new commands are stored between these two calls, the internal scheduler thread will exit as soon as possible (after the current command, if any, has been processed).
-
-