-
Notifications
You must be signed in to change notification settings - Fork 353
Task scheduler
Experimental and not yet released !!!
This is a Quartz like task scheduler where you can register a task class, task metadata and scheduling details. The recipe builds on top of the Durable Message Queue recipe for scheduling the next execution time and persists task details in a separate column family.
TODO
The first step to using the scheduler is to create a DistributedTaskScheduler instance.
TaskScheduler scheduler = new DistributedTaskScheduler.Builder()
.withBatchSize(5)
.withKeyspace(keyspace)
.withName("TestScheduler")
.build();
First, you must define a task processing class.
public class HelloWorldTask implements Task {
@Override
public void execute(TaskInfo task) {
System.out.println("Hello World!");
}
}
Next, schedule a task and give it a trigger. This example shows how to create a task that will execute every hour starting from now and will repeat 5 times. The scheduler will also keep a status history of every task execution.
scheduler.scheduleTask(
new TaskInfo.Builder()
.withKey("MyHelloWorld")
.withClass(HelloWorldTask.class)
.withHistory(true)
.build()
,
new RepeatingTrigger.Builder()
.withInterval(1, TimeUnit.HOURS)
.withRepeatCount(5)
.build()
);
You can define this CronTrigger which uses the Quartz cron parser to determine the next execution time.
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import org.quartz.CronExpression;
import com.netflix.astyanax.recipes.scheduler.Trigger;
public class CronTrigger extends AbstractTrigger {
public static class Builder {
private CronTrigger trigger = new CronTrigger();
public Builder withExpression(String expression) {
trigger.expression = expression;
return this;
}
public CronTrigger build() {
trigger.setTriggerTime(trigger.getNextCronFromNow());
return trigger;
}
}
private String expression;
@Override
public Trigger nextTrigger() {
CronTrigger nextTrigger = new CronTrigger();
nextTrigger.expression = expression;
nextTrigger.setTriggerTime(getNextCronFromNow());
nextTrigger.setExecutionCount(getExecutionCount() + 1);
return nextTrigger;
}
private long getNextCronFromNow() {
try {
Calendar cal = Calendar.getInstance();
Date currTime = cal.getTime();
CronExpression cronExpression = new CronExpression(expression);
return cronExpression.getNextValidTimeAfter(currTime).getTime();
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
public String getCrontab() {
return expression;
}
public void setCrontab(String crontab) {
this.expression = crontab;
}
}
A Netflix Original Production
Tech Blog | Twitter @NetflixOSS | Jobs
- Getting-Started
- Configuration
- Features
- Monitoring
- Thread Safety
- Timeouts
- Recipes
- Examples
- Javadoc
- Utilities
- Cassandra-Compatibility
- FAQ
- End-to-End Examples
- Astyanax Integration with Java Driver