This library supports for modify Triggering in many variations.
You have to implement your class implementing Job class as "TestJob.java".
TestJob.java
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class TestJob implements Job {
@Override
public void execute(JobExecutionContext jec) throws JobExecutionException {
System.out.println("Hello Quartz!");
}
}
This "Schedule.java" will be triggered by 5 seconds.
Schedule.java
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
public class Schedule {
public static void main(String[] args) throws Exception {
JobDetail job = JobBuilder.newJob(TestJob.class).withIdentity("testJob", "group1").build();
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("dummyTriggerName", "group1")
.withSchedule(
SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(5).repeatForever())
.build();
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);
}
}
And you can use Quartz as a cron job.
Cron.java
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
public class Cron
{
public static void main( String[] args ) throws Exception
{
JobDetail job = JobBuilder.newJob(TestJob.class)
.withIdentity("testJobName", "group1").build();
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("dummyTriggerName", "group1")
.withSchedule(
CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
.build();
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);
}
}
Following libraries must be added to the projects.
- quartz-2.1.6.jar
- log4j-1.2.16.jar
- c3p0-0.9.1.1.jar
- slf4j-log4j12-1.6.1.jar
- slf4j-api-1.6.1.jar