How to schedule Jobs in a Java CDI Environment

Often we need to schedule Jobs. Standard Java CDI does not have a build in Job Scheduler feature. But this functionality can be easily implemented by using the CDI extension mechanism.

I wrote a simple CDI extension for Job scheduling backed by the Quartz Framework. It is hosted at Github.

To schedule a Job, we basically need to do the following things:

a) add the following Maven dependency(available from Central Repository):


b) implement a Bean with the Job implementation:

import de.mirkosertic.cdicron.api.Cron;

public class DummyJob {

    public static final AtomicLong COUNTER = new AtomicLong(0);

    @Cron(cronExpression = "0/2 * * * * ?")
    public void scheduledMethod() {

Nothing else to do :-) The Job scheduler is booted by the CDI extension and will schedule the annotated method as defined by the supplied cron expression.The cdicron project comes with a built in scheduler abstraction. So it is quite easy to support other scheduling frameworks without changing existing code. Take a look at the GitHub project for more information.

I really love CDI!

Loading comments...