View Source

The DSB provides a simple way to create recurrent tasks known has cronjobs by adding annotations on methods to call regularly. This comes from the fact that in a system, you generally have to periodically do things. The DSB ease this task creation from the developer point of view by introducing a simple Job annotation defined like
{code:language=java}package org.petalslink.dsb.annotations.cron;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.concurrent.TimeUnit;

/**
* @author chamerling
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD })
public @interface Job {

String id() default "";

long period() default 60;

long delay() default 60;

TimeUnit timeUnit() default TimeUnit.SECONDS;

}
{code}
By putting this annotation on a method, it will be called periodically. The period is configured with the annotation parameters. For example:
{code:language=java}@FractalComponent
public class FooJob {

@Monolog(name = "logger")
private Logger logger;

private LoggingUtil log;

@LifeCycle(on = LifeCycleType.START)
protected void start() {
this.log = new LoggingUtil(this.logger);
}

@LifeCycle(on = LifeCycleType.STOP)
protected void stop() {
}

@Job(delay = 20L, period = 20L, timeUnit = TimeUnit.SECONDS)
public void iAmACronJob() {
Date d = new Date(System.currentTimeMillis());
System.out.println("I am a FOO job, invoked at " + d.toString());
}

}{code}
The method _iAmACronJob_ will be fired 20 seconds after the DSB startup, and will occurs every 20 seconds.
{note}The parameter must not have any parameter and must be public. Return values are not handled.{note}
To use it, just put the annotation, as many other annotation based stuff, the DSB will introspect the kernel components and will create the jobs for you.