public class DedicatedScheduledExecutorService extends BoundedScheduledExecutorService implements ReportingExecutorService
ScheduledExecutorService that is associated with one specific task for which it
provides automatic logging capabilitiesScheduledExecutorService that do not throw Exceptions when we try to start the same task
multiple times. This is handy for implementing the "start" and "shutdown" methods of the background workers of IRI
that would otherwise have to take care of not starting the same task more than once (when trying to be robust against
coding errors or tests that start the same thread multiple times).| Constructor and Description |
|---|
DedicatedScheduledExecutorService()
Does the same as
DedicatedScheduledExecutorService(String, Logger, boolean) but defaults to null
for the thread name (which causes only error messages to be printed), the DEFAULT_LOGGER for the log
messages and false for the debug flag.Note: This is for example used by the com.iota.iri.utils.log.interval.IntervalLogger which does not want
to inform the user when scheduling a log output, but which still needs the "only run one task" logic. |
DedicatedScheduledExecutorService(boolean debug)
Does the same as
DedicatedScheduledExecutorService(String, Logger, boolean) but defaults to null
for the thread name (which causes only error messages to be printed - unless debug is true) and the
DEFAULT_LOGGER for the log messages.Note: This is for example used by the com.iota.iri.utils.log.interval.IntervalLogger which does not want
to inform the user when scheduling a log output, but which still needs the "only run one task" logic. |
DedicatedScheduledExecutorService(org.slf4j.Logger logger)
Does the same as
DedicatedScheduledExecutorService(String, Logger, boolean) but defaults to null
for the thread name (which causes only error messages to be printed - unless debug is true) and and false for the
debug flag.Note: This is for example used by the com.iota.iri.utils.log.interval.IntervalLogger which does not want
to inform the user when scheduling a log output, but which still needs the "only run one task" logic. |
DedicatedScheduledExecutorService(org.slf4j.Logger logger,
boolean debug)
Does the same as
DedicatedScheduledExecutorService(String, Logger, boolean) but defaults to null
for the thread name (which causes only error messages to be printed - unless debug is true).Note: This is for example used by the com.iota.iri.utils.log.interval.IntervalLogger which does not want
to inform the user when scheduling a log output, but which still needs the "only run one task" logic. |
DedicatedScheduledExecutorService(String threadName)
Does the same as
DedicatedScheduledExecutorService(String, Logger, boolean) but defaults to the
DEFAULT_LOGGER for the log messages and false for the debug flag. |
DedicatedScheduledExecutorService(String threadName,
boolean debug)
Does the same as
DedicatedScheduledExecutorService(String, Logger, boolean) but defaults to the
DEFAULT_LOGGER for the log messages. |
DedicatedScheduledExecutorService(String threadName,
org.slf4j.Logger logger)
Does the same as
DedicatedScheduledExecutorService(String, Logger, boolean) but defaults to false
for the debug flag. |
DedicatedScheduledExecutorService(String threadName,
org.slf4j.Logger logger,
boolean debug)
Creates a
ScheduledExecutorService that is associated with one specific task for which it provides
automatic logging capabilities (using the provided thread name).It informs the user about its lifecycle using the logback loggers used by IRI. |
| Modifier and Type | Method and Description |
|---|---|
String |
getThreadName()
This method is the getter for the name of the thread that gets created by this service.
|
void |
onCancelTask(TaskDetails taskDetails)
This method gets called whenever a task is cancelled through its
Future or through
the shutdown methods of the ExecutorService.It only gets called once for every task. |
void |
onCompleteTask(TaskDetails taskDetails,
Throwable error)
This method gets called whenever a task completes.
This can be through either raising an exception, cancelling from the outside or by simply terminating in a normal manner. |
void |
onFinishTask(TaskDetails taskDetails,
Throwable error)
This method gets called whenever a task is finished.
For recurring tasks it is called multiple times. |
void |
onScheduleTask(TaskDetails taskDetails)
This method gets called whenever a new task is scheduled to run.
In contrast to ReportingExecutorService.onStartTask(TaskDetails) this method gets called only once for "recurring" tasks that are
scheduled to run in pre-defined intervals. |
void |
onStartTask(TaskDetails taskDetails)
This method gets called whenever a task is started.
For recurring tasks it is called multiple times. |
awaitTermination, execute, invokeAll, invokeAll, invokeAny, invokeAny, isShutdown, isTerminated, schedule, schedule, scheduleAtFixedRate, scheduleWithFixedDelay, shutdown, shutdownNow, silentExecute, silentInvokeAll, silentInvokeAll, silentInvokeAny, silentInvokeAny, silentSchedule, silentSchedule, silentScheduleAtFixedRate, silentScheduleWithFixedDelay, silentSubmit, silentSubmit, silentSubmit, submit, submit, submitpublic DedicatedScheduledExecutorService(String threadName, org.slf4j.Logger logger, boolean debug)
ScheduledExecutorService that is associated with one specific task for which it provides
automatic logging capabilities (using the provided thread name).ScheduledExecutorService that do not throw Exceptions when we try to start the
same task multiple times. This is handy for implementing the "start" and "shutdown" methods of the background
workers of IRI that would otherwise have to take care of not starting the same task more than once (when trying
to be robust against coding errors or tests that start the same thread multiple times).
Example:
private final static Logger logger = LoggerFactor.getLogger(MilestoneSolidifier.class);
private DedicatedScheduledExecutorService milestoneSolidifier = new DedicatedScheduledExecutorService(
"Solidification Thread", logger, false);
// calling this multiple times will only start exactly one background job (ignore additional requests)
public void start() {
milestoneSolidifier.silentScheduleAtFixedRate(this::solidificationThread, 10, 50, MILLISECONDS);
}
// calling this multiple times will only stop the one instance that is running (if it is running)
public void shutdown() {
milestoneSolidifier.shutdownNow();
}
public void solidificationThread() {
System.out.println("I get executed every 50 milliseconds");
}
Resulting Log Output:
[main] INFO MilestoneSolidifier - Starting [Milestone Solidifier] (starts in 10ms / runs every 50ms) ...
[main] INFO MilestoneSolidifier - [Milestone Solidifier] Started (execution #1) ...
[Milestone Solidifier] INFO c.i.i.s.m.MilestoneSolidifier - I get executed every 50 milliseconds
[Milestone Solidifier] INFO c.i.i.s.m.MilestoneSolidifier - I get executed every 50 milliseconds
[Milestone Solidifier] INFO c.i.i.s.m.MilestoneSolidifier - I get executed every 50 milliseconds
[main] INFO MilestoneSolidifier - Stopping [Milestone Solidifier] ...
[main] INFO MilestoneSolidifier - [Milestone Solidifier] Stopped (after #4 executions) ...
threadName - name of the thread (or null if we want to disable the automatic logging - exceptions will
always be logged)logger - logback logger that shall be used for the origin of the log messagesdebug - debug flag that indicates if every "run" should be accompanied with a log messagepublic DedicatedScheduledExecutorService(String threadName, boolean debug)
DedicatedScheduledExecutorService(String, Logger, boolean) but defaults to the
DEFAULT_LOGGER for the log messages.threadName - name of the thread (or null if we want to disable the automatic logging - exceptions will
always be logged)debug - debug flag that indicates if every "run" should be accompanied with a log messagepublic DedicatedScheduledExecutorService(String threadName, org.slf4j.Logger logger)
DedicatedScheduledExecutorService(String, Logger, boolean) but defaults to false
for the debug flag.threadName - name of the thread (or null if we want to disable the automatic logging - exceptions will
always be logged)logger - logback logger that shall be used for the origin of the log messagespublic DedicatedScheduledExecutorService(org.slf4j.Logger logger,
boolean debug)
DedicatedScheduledExecutorService(String, Logger, boolean) but defaults to null
for the thread name (which causes only error messages to be printed - unless debug is true).com.iota.iri.utils.log.interval.IntervalLogger which does not want
to inform the user when scheduling a log output, but which still needs the "only run one task" logic.logger - logback logger that shall be used for the origin of the log messagesdebug - debug flag that indicates if every "run" should be accompanied with a log messagepublic DedicatedScheduledExecutorService(String threadName)
DedicatedScheduledExecutorService(String, Logger, boolean) but defaults to the
DEFAULT_LOGGER for the log messages and false for the debug flag.threadName - name of the thread (or null if we want to disable the automatic logging - exceptions will
always be logged)public DedicatedScheduledExecutorService(org.slf4j.Logger logger)
DedicatedScheduledExecutorService(String, Logger, boolean) but defaults to null
for the thread name (which causes only error messages to be printed - unless debug is true) and and false for the
debug flag.com.iota.iri.utils.log.interval.IntervalLogger which does not want
to inform the user when scheduling a log output, but which still needs the "only run one task" logic.logger - logback logger that shall be used for the origin of the log messagespublic DedicatedScheduledExecutorService(boolean debug)
DedicatedScheduledExecutorService(String, Logger, boolean) but defaults to null
for the thread name (which causes only error messages to be printed - unless debug is true) and the
DEFAULT_LOGGER for the log messages.com.iota.iri.utils.log.interval.IntervalLogger which does not want
to inform the user when scheduling a log output, but which still needs the "only run one task" logic.debug - debug flag that indicates if every "run" should be accompanied with a log messagepublic DedicatedScheduledExecutorService()
DedicatedScheduledExecutorService(String, Logger, boolean) but defaults to null
for the thread name (which causes only error messages to be printed), the DEFAULT_LOGGER for the log
messages and false for the debug flag.com.iota.iri.utils.log.interval.IntervalLogger which does not want
to inform the user when scheduling a log output, but which still needs the "only run one task" logic.public String getThreadName()
threadName.public void onScheduleTask(TaskDetails taskDetails)
ReportingExecutorService.onStartTask(TaskDetails) this method gets called only once for "recurring" tasks that are
scheduled to run in pre-defined intervals.DedicatedScheduledExecutorService (to not pollute the CLI with meaningless messages).onScheduleTask in interface ReportingExecutorServiceonScheduleTask in class BoundedScheduledExecutorServicetaskDetails - metadata holding the relevant information of the taskpublic void onStartTask(TaskDetails taskDetails)
DedicatedScheduledExecutorService (display it like it would be a Thread with one start message
and one stop message - to not pollute the CLI with meaningless messages).Thread that will consequently be used for log
messages from the task itself.onStartTask in interface ReportingExecutorServiceonStartTask in class BoundedScheduledExecutorServicetaskDetails - metadata holding the relevant information of the taskpublic void onFinishTask(TaskDetails taskDetails, Throwable error)
onCompleteTask(TaskDetails, Throwable) callback will give enough information about the crash).onFinishTask in interface ReportingExecutorServiceonFinishTask in class BoundedScheduledExecutorServicetaskDetails - metadata holding the relevant information of the taskerror - the exception that caused this task to terminate or null if it terminated normallypublic void onCancelTask(TaskDetails taskDetails)
Future or through
the shutdown methods of the ExecutorService.DedicatedScheduledExecutorService (to not pollute the CLI with meaningless messages).onCancelTask in interface ReportingExecutorServiceonCancelTask in class BoundedScheduledExecutorServicetaskDetails - metadata holding the relevant information of the taskpublic void onCompleteTask(TaskDetails taskDetails, Throwable error)
BoundedScheduledExecutorService.scheduledTasksCounter and removing the task from the
BoundedScheduledExecutorService.scheduledTasks set.DedicatedScheduledExecutorService (to not pollute the CLI with meaningless
messages).onCompleteTask in interface ReportingExecutorServiceonCompleteTask in class BoundedScheduledExecutorServicetaskDetails - metadata holding the relevant information of the taskerror - the exception that caused this task to terminate or null if it terminated normallyCopyright © 2019. All rights reserved.