See: Description
Interface | Description |
---|---|
BackgroundRunnable |
This is the interface that a user periodically executed Task class must implement.
|
Class | Description |
---|---|
BackgroundRunnableFactory |
This class is part of the framework and probably will not be accessed externally
|
BackgroundThreadsRunner |
This class is part of the framework and probably will not be accessed externally.
|
BaseBackgroundRunnable |
This is the base class for any user defined periodic Task classes See source code example in package
com.mgnt.lifecycle.management.backgroundrunner.example that demonstrates how it is done |
BaseBackgroundRunnable
class and override 4 methods:
BaseBackgroundRunnable.initParamsForSpecificImplementation()
BackgroundRunnable.getTaskExecutionInterval()
BackgroundRunnable.setParamValue(com.mgnt.utils.entities.TimeInterval,
java.lang.String)
Runnable.run()
@Scheduled
that can be used for any method and that would work just fine. Well The annotation
@Scheduled
takes time interval property i.e. the time between 2 sequential executions in milliseconds or as
a cron expression. Neither of those formats are user friendly. Imagine that you need to run a task each 9 hours.
So you parameter that you will have to provide to @Scheduled
annotation
would look like @Scheduled(fixedRate = 32400000)
. (32400000 is number of milliseconds in 9 hours) This is
hardly intuitive... What if you could write @Scheduled(fixedRate = 9h)
? Well, that would be great, but you
can not (at least with currently available latest versions). This is what this infrastructure provides. Especially
if you annotate one of your properties with annotation @Value("${task.execution.interval}")
and then you will
have a properties file that will have a property task.execution.interval=9h
instead of
task.execution.interval=32400000
. So there is a trade-off here. This infrastructure definitely requires more effort
from a programmer then mere method annotation with @Scheduled
, but provides very intuitive and humanly readable
way to define time interval properties. Internally this framework uses utility provided by this library to parse those
time intervals. (For details see TextUtils.parseStringToTimeInterval(java.lang.String)
) Also
this infrastructure allows you to parse other time interval properties (if you class has any) with the same format.
package com.mgnt.lifecycle.management.backgroundrunner.example
contains working source code that demonstrates
how this infrastructure is used. Assume that you have implemented your tasks
TypeOneTask
and
TypeTwoTask
(The same classes that are used in package
com.mgnt.lifecycle.management.backgroundrunner.example
). If you work in Spring environment you will need
to annotate those classes with annotation Component
(or define them as beans in your xml configuration files)
and you will need to add one more declaration in your xml configuration file:
<bean id="backgroundThreadsRunner" name="backgroundThreadsRunner"
class="com.mgnt.lifecycle.management.backgroundrunner.BackgroundThreadsRunner" depends-on="typeOneTask,typeTwoTask"/>
The reason is that your tasks must be instantiated before instance of
BackgroundThreadsRunner
is created by Spring
com.mgnt.lifecycle.management.backgroundrunner.example
the code there is concise and well commented with
detailed explanation