Skip to content

Task scheduler

elandau edited this page Nov 30, 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.

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

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

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

Reading task history

Clone this wiki locally