-
Notifications
You must be signed in to change notification settings - Fork 1
Basic Usage
Include the spikeify-taskqueue.jar
lib in your project or add a maven dependency:
<dependency>
<groupId>com.spikeify</groupId>
<artifactId>taskqueue</artifactId>
<version>0.1.9</version>
</dependency>
The DefaultTaskQueueService
takes care of:
- adding new jobs to the queue
- getting the next job to be executed
- removing jobs from queue
- changing job state in its lifecycle
For most cases only the add(Job job, String queueName)
method will be needed.
Creating a new job is easy. Just inherit from the Job
interface and implement TaskResult execute(TaskContext context)
method.
public class SimpleJob implements Job {
private int property;
protected SimpleJob() {
// for Jackson
}
public SimpleJob(int value) {
setProperty(value);
}
public int getProperty() {
return property;
}
public void setProperty(int value) {
property = value;
}
@Override
public TaskResult execute(TaskContext context) {
// nothing to do right now ...
if (context != null && context.interrupted()) {
return TaskResult.interrupted();
}
return TaskResult.ok();
}
}
Following must be considered:
- jobs should be small atomic tasks, not running more than a few minutes (currently 10 minutes by default)
- job execution order is not guaranteed so one job should not rely on some other job started in short sequence
- jobs get serialized and deserialized from to JSON string, only properties that are serialized can be used by the job in the
execute
method - jobs must have a default (empty) constructor
- jobs must return a success or failed state (this is used by the queue service to re-run failed jobs)
- long running jobs should regularly check TaskContext if the interrupted() method returns
true
. If so the job has some time (10 seconds at the most) to end gracefully. Otherwise the job thread will be interrupted.
Once a job has been added to the queue, the queue manager takes over:
- runs a scheduled check every 10 seconds checking is a new task is present in the queue
- once a task has been found, the task is executed
- if more tasks are found the execution continues
- successful or failed tasks are regularly purged from the queue (failed tasks stay longer in the queue to be inspected)
- queue manager also builds up running statistic for all completed or failed tasks
In order to start queue following must be done:
- Register queue
TaskQueueService queues = new DefaultTaskQueueService(spikeify);
TaskQueueManager manager = new DefaultTaskQueueManager(spikeify, queues);
manager.register("myQueue", true);
- Start queue / check
manager.check("myQueue");
The check
call should be triggered regularly via cron job on each instance where the queue service is running. It takes care of stopping/starting the queue service in case the service has been stopped or started on some other instance.
If you are running the queue service on only one instance then the check
is not needed. Just start up the queue manually:
manager.start("myQueue");