A quartz scheduler is also a solution, and firstly, you run the Quartz Job class.
Quartz task is defined that you want to run
package com.blogspot.geekonjava.quartz; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.quartz.JobKey; public class QuartzJob implements Job { public void execute(JobExecutionContext context) throws JobExecutionException { JobKey jobKey = context.getJobDetail().getKey(); System.out.println("Quartz" + "Job Key " + jobKey); } }
Now you need to do Quartz Trigger
There are two types of triggers in Quartz
SimpleTrigger . Allows you to set the start time, end time, repetition interval.
Trigger trigger = newTrigger().withIdentity("TriggerName", "Group1") .withSchedule(SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(10).repeatForever()).build();
CronTrigger . Allows an unix cron expression to specify dates and to do its job.
Trigger trigger = newTrigger() .withIdentity("TriggerName", "Group2") .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?")).build();
The scheduler class links both the Work and the Trigger and executes it.
Scheduler scheduler = new StdSchedulerFactory().getScheduler(); scheduler.start(); scheduler.scheduleJob(job, trigger);
The full example you can see here
GeekOnJava May 26 '15 at 6:34 am 2015-05-26 06:34
source share