Skip to content

Task scheduler

elandau edited this page Dec 4, 2012 · 9 revisions

Experimental and not yet released !!!

Overview

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.

Data Model

TODO

Examples

Setting up the scheduler

The first step to using the scheduler is to create a DistributedTaskScheduler instance. The keyspace client is assumed to already have been created.

TaskScheduler scheduler = new DistributedTaskScheduler.Builder()
    .withBatchSize(5)
    .withKeyspace(keyspace)
    .withName("TestScheduler")
    .build();

Creating the column families

Once the TaskScheduler instance has been set up you can call .create() to create the proper column families (there will be 3) to be used by the scheduler. You only need to call this once when you set up your scheduler for the first time.

scheduler.create();

Scheduling a task

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()
      );

Stopping a task

Resuming a task

Deleting a task

CRON Scheduling

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 getExpression() {
        return expression;
    }

    public void setExpression(String expression) {
        this.expression = expression;
    }
}

Here's an example of how to define a cron trigger that runs every 5 seconds

new CronTrigger.Builder()
    .withExpression("*/5 * * * * ?")
    .build()

Reading task history

Clone this wiki locally