|
Description
The 'jobs' service allows executing a unit of work according to a certain schedule. There are different
types of similar services: "cron" on Unix-systems, "Task Scheduler" on Windows, "Jobs" on an Oracle database, etc.
However most of these solutions depend on their specific platform or database while the 'jobs' service
provides a uniform and functionally rich facility for time-based scheduling. Note: Java provides simple job
scheduling capabilities via the class java.util.Timer but this class is really only suited to
trivial cases and lacks functionality such as a job calendar, custom schedulers, job context, etc.
The 'jobs' service operates with the following concepts:
| Job |
A job is a specific task that can be scheduled for a background execution and consistes of a scheduler,
job body and job calendar.
|
| Body |
A job body represents the user defined code that defines what is being executed when job is
executing.
|
| Scheduler |
A job scheduler defines when a job is to be executed. The user can use the following pre-defined schedulers
(or develop his own):
- Daily scheduler
- Fixed delay scheduler
- Fixed rate scheduler
- Monthly scheduler
- One time scheduler
- Weekly scheduler
|
| Calendar |
A job calendar defines the exceptions to a job's schedule, i.e. the times when the job should not be
executed. For example, weekends can be defined as exceptions for the daily scheduler.
|
| Job Context |
A job context provides the current job with access to other jobs in the system enabling the development
of cooperative jobs.
|
| Jobs Group |
A jobs group provides an ability to perform schedule/cancel/delete operations with multiple jobs at a time.
|
| Jobs Time Scaler |
Time scaler allows invoking scheduled jobs in 'scaled' time, which helps, for example, to
run daily, weekly, or monthly or other long-period jobs within short interval of time to test their
functionality.
|
| Jobs Store |
Job store allows saving active job's state and restoring it on service's startup. Jobs store implementations
can use various means for storing. Service provides pre-defined implementation, which uses
filesystem for storing/restoring operations.
|
Note: the 'jobs' service is not a real-time system. It doesn't guarantee execution of jobs at the
exact defined time.
Top
Configuration
Configuration of the 'jobs' service is defined in the xtier_jobs.xml configuration file.
This file follows the standard xTier service configuration pattern that can be illustrated by the following complete
example:
| 1 |  | <!-- |
| 2 |  | |
| 3 |  | --> |
| 4 |  | <time-scaler> |
| 5 |  | <ioc ref-uid="scaler"/> |
| 6 |  | </time-scaler> |
| 7 |  | |
| 8 |  | <!-- |
| 9 |  | |
| 10 |  | --> |
| 11 |  | <store> |
| 12 |  | <ioc ref-uid="store"/> |
| 13 |  | </store> |
| 14 |  | |
| 15 |  | <!-- |
| 16 |  | |
| 17 |  | --> |
| 18 |  | <group name="all.xml.jobs"> |
| 19 |  | <include-job name="one.time.job"/> |
| 20 |  | <include-job name="fixed.delay.job"/> |
| 21 |  | <include-job name="fixed.rate.job"/> |
| 22 |  | <include-job name="daily.job"/> |
| 23 |  | <include-job name="weekly.job"/> |
| 24 |  | <include-job name="monthly.job"/> |
| 25 |  | </group> |
| 26 |  | |
| 27 |  | <!----> |
| 28 |  | <job name="one.time.job" autostart="false"> |
| 29 |  | <scheduler> |
| 30 |  | <ioc policy="new"> |
| 31 |  | <java class="com.fitechlabs.xtier.services.job. |
| 32 |  | schedulers.JobOneTimeScheduler"> |
| 33 |  | <ctor> |
| 34 |  | <!----> |
| 35 |  | <arg type="int64">3000</arg> |
| 36 |  | </ctor> |
| 37 |  | </java> |
| 38 |  | </ioc> |
| 39 |  | </scheduler> |
| 40 |  | |
| 41 |  | <body> |
| 42 |  | <!----> |
| 43 |  | <ioc policy="new"> |
| 44 |  | <java class="com.fitechlabs.xtier.examples. |
| 45 |  | services.jobs.SimpleJobBody"/> |
| 46 |  | </ioc> |
| 47 |  | </body> |
| 48 |  | </job> |
| 49 |  | |
| 50 |  | <!----> |
| 51 |  | <job name="weekly.job" autostart="true"> |
| 52 |  | <scheduler> |
| 53 |  | <ioc policy="new"> |
| 54 |  | <java class="com.fitechlabs.xtier.services.job. |
| 55 |  | schedulers.JobWeeklyScheduler"> |
| 56 |  | <ctor> |
| 57 |  | <!----> |
| 58 |  | <arg type="string"> |
| 59 |  | Mon 03:43:00 am, |
| 60 |  | Wen 9:40 pm, |
| 61 |  | Thu 7:05 pm |
| 62 |  | </arg> |
| 63 |  | </ctor> |
| 64 |  | </java> |
| 65 |  | </ioc> |
| 66 |  | </scheduler> |
| 67 |  | |
| 68 |  | <calendar> |
| 69 |  | <!----> |
| 70 |  | <ioc policy="new"> |
| 71 |  | <java class="com.fitechlabs.xtier.services.job. |
| 72 |  | calendars.JobUsaWorkCalendar"/> |
| 73 |  | </ioc> |
| 74 |  | </calendar> |
| 75 |  | <body> |
| 76 |  | <ioc policy="new"> |
| 77 |  | <java class="com.fitechlabs.xtier.examples. |
| 78 |  | services.jobs.SimpleJobBody"/> |
| 79 |  | </ioc> |
| 80 |  | </body> |
| 81 |  | </job> |
The 'jobs' service configuration consists of one or more job descriptor definitions specified by the
<job> tag. Note that job bodies, job schedulers and job calendars are defined as IoC objects.
See IocService for more details on using the IoC object. Note also that
the 'jobs' service configuration should define the name of the thread pool that is going to be used by this
service (see below). Formal specification for the <job> tag can be found in the xtier_jobs.dtd DTD file in the
${XTIER_ROOT}/config/dtd folder.
The <region> tag defines the following sub-elements:
| <thread-pool-name> |
This element defines name of the thread pool that should be defined in 'objpool'
service configuration. This thread pool will be used by the 'jobs' service implementation.
|
| <time-scaler> |
This optional element defines IoC descriptor for time scaler to be used by the service. Note that if
time scaler is not specified (on XML or explicitly via API) 'jobs' service uses default
'linear time scaling'implementation initiallly tuned to no scaling (i.e. scaled time equals to system time).
|
| <store> |
This optional element defines IoC descriptor for jobs store. Jobs store performs storing and
restoring operations for job service components, namely active Jobs. Note that only
xml-defined jobs can be stored/restored. Implementation may use various databases or other means
for data storing.
|
| <group> |
This optional element defines jobs group. Jobs groups provide an ability to
perform schedule/cancel/delete operations with multiple jobs at a time. Jobs groups can be either
created at runtime or pre-defined in XML configuration. Each job group consists of the one or more
>include-job< elements specifying job's name to be included into the group.
|
The <job> tag defines the following attributes and sub-elements:
| name |
This attribute defines a job's name. |
| autostart |
This attribute defines the flag indicating whether or not this job should be started after
the 'jobs' service starts. Note that this attribute is optional. The default value is false.
|
| delete-on-finished |
This attribute defines the flag indicating whether or not this job should be deleted from list of jobs after
it finishes. Note that this attribute is optional. The default value is false. Note that while
scheduling the job using schedule(String, boolean) method, the second boolean parameter passed
to this method overwrites delete-on-finished value specified by this attribute for a runtiume
(configuration file is not changed).
|
| <body> |
This sub-element defines the IoC descriptor for the job's body. |
| <scheduler> |
This sub-element defines the IoC descriptor for the job's scheduler. |
| <calendar> |
This sub-element defines the IoC descriptor for a job's calendar. Note that this element is optional.
|
Top
Examples
Usage of the 'jobs' service follows the standard pattern of using an xTier service: you need to obtain
an instance of the xTier kernel that serves as a service registry. Once you have the xTier kernel you can get
an instance of any service, in our case the 'jobs' service. Assuming the above configuration,
start scheduling and using jobs as in the following code example:
| 1 |  | |
| 2 |  | XtierKernel xtier = XtierKernel.getInstance(); |
| 3 |  | |
| 4 |  | |
| 5 |  | JobsService jobs = xtier.jobs(); |
| 6 |  | |
| 7 |  | |
| 8 |  | Job weeklyJob = jobs.getJob("weekly.job"); |
| 9 |  | |
| 10 |  | Job oneTimeJob = jobs.getJob("one.time.job"); |
| 11 |  | |
| 12 |  | |
| 13 |  | jobs.schedule(oneTimeJob.getName(), true); |
Download xTier for full examples and documentation.
Following diagram depicts the state transition of a job:
Top
|