Hi all,
I was working with Cron scheduling and with some help from the internet blogs finally i made a example out of it. Please refer the following website for full explanation http://www.mkyong.com/java/quartz-scheduler-example/
/**
* Scheduler Task --- This class is the one which contains the task that needs to be executed...
* */
public class RunMeTask
{
public void printMe() {
System.out.println("Run Me ~");
}
}
public class RunMeJob implements Job
{
public RunMeJob() {
}
public void execute(JobExecutionContext context)
throws JobExecutionException {
Map dataMap = context.getJobDetail().getJobDataMap();
RunMeTask task = (RunMeTask)dataMap.get("runMeTask");
task.printMe();
}
}
public class CronLearning {
public static void main( String[] args ) throws Exception
{
simpleTrig();
unixCronTrig();
}
private static void simpleTrig() throws SchedulerException{
RunMeTask task = new RunMeTask();
//specify your sceduler task details
JobDetail job = new JobDetail();
job.setName("runMeJob");
job.setJobClass(RunMeJob.class);
Map dataMap = job.getJobDataMap();
dataMap.put("runMeTask", task);
//configure the scheduler time
SimpleTrigger trigger = new SimpleTrigger();
trigger.setName("runMeJobTesting");
trigger.setStartTime(new Date(System.currentTimeMillis() + 1000));
//trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
trigger.setRepeatCount(2);
trigger.setRepeatInterval(30000);
//trigger.setEndTime(new Date(System.currentTimeMillis() + 60000));
//schedule it
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);
}
private static void unixCronTrig() throws SchedulerException, ParseException{
RunMeTask task = new RunMeTask();
//specify your sceduler task details
JobDetail job = new JobDetail();
job.setName("CronJob");
job.setJobClass(CronJob.class);
CronTrigger trigger = new CronTrigger();
trigger.setName("CronJobTesting");
//configure the scheduler time to fire every 30 seconds
//trigger.setCronExpression("0/30 * * * * ?");
//configure the scheduler time to fire once
trigger.setCronExpression("10 05 07 25 6 ? 2012");
//schedule it
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);
System.out.println("cron expression --->"+trigger.getCronExpression());
}
}
I was working with Cron scheduling and with some help from the internet blogs finally i made a example out of it. Please refer the following website for full explanation http://www.mkyong.com/java/quartz-scheduler-example/
/**
* Scheduler Task --- This class is the one which contains the task that needs to be executed...
* */
public class RunMeTask
{
public void printMe() {
System.out.println("Run Me ~");
}
}
/**
* Scheduler Job --- This class is the one which implements the Job interface
*/
* Scheduler Job --- This class is the one which implements the Job interface
*/
import java.util.Map;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class RunMeJob implements Job
{
public RunMeJob() {
}
public void execute(JobExecutionContext context)
throws JobExecutionException {
Map dataMap = context.getJobDetail().getJobDataMap();
RunMeTask task = (RunMeTask)dataMap.get("runMeTask");
task.printMe();
}
}
/**
*
* Scheduler Job --- This class is the one which implements the Job interface to work with the Cron trigger
*/
*
* Scheduler Job --- This class is the one which implements the Job interface to work with the Cron trigger
*/
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class CronJob implements Job
{
public CronJob() {
}
public void execute(JobExecutionContext context)
throws JobExecutionException {
System.out.println("inside the cron job methoid------>>");
}
}
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class CronJob implements Job
{
public CronJob() {
}
public void execute(JobExecutionContext context)
throws JobExecutionException {
System.out.println("inside the cron job methoid------>>");
}
}
/**
*
* This class is the one which will initiate the trigger and schedule the job
*
* This class is the one which will initiate the trigger and schedule the job
*/
import java.text.ParseException;
import java.util.Date;
import java.util.Map;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;
import java.util.Date;
import java.util.Map;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;
public class CronLearning {
public static void main( String[] args ) throws Exception
{
simpleTrig();
unixCronTrig();
}
private static void simpleTrig() throws SchedulerException{
RunMeTask task = new RunMeTask();
//specify your sceduler task details
JobDetail job = new JobDetail();
job.setName("runMeJob");
job.setJobClass(RunMeJob.class);
Map dataMap = job.getJobDataMap();
dataMap.put("runMeTask", task);
//configure the scheduler time
SimpleTrigger trigger = new SimpleTrigger();
trigger.setName("runMeJobTesting");
trigger.setStartTime(new Date(System.currentTimeMillis() + 1000));
//trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
trigger.setRepeatCount(2);
trigger.setRepeatInterval(30000);
//trigger.setEndTime(new Date(System.currentTimeMillis() + 60000));
//schedule it
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);
}
private static void unixCronTrig() throws SchedulerException, ParseException{
RunMeTask task = new RunMeTask();
//specify your sceduler task details
JobDetail job = new JobDetail();
job.setName("CronJob");
job.setJobClass(CronJob.class);
CronTrigger trigger = new CronTrigger();
trigger.setName("CronJobTesting");
//configure the scheduler time to fire every 30 seconds
//trigger.setCronExpression("0/30 * * * * ?");
//configure the scheduler time to fire once
trigger.setCronExpression("10 05 07 25 6 ? 2012");
//schedule it
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);
System.out.println("cron expression --->"+trigger.getCronExpression());
}
}
No comments:
Post a Comment