Scheduler JOB in java
package com..scheduler;
import static org.quartz.CronScheduleBuilder.dailyAtHourAndMinute;
import java.util.Calendar;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
public class SchedulerTest implements Job {
public static void main(String[] args) {
int HOUR_OF_DAY = Integer.parseInt("20");
int MINUTE_OF_HOUR = Integer.parseInt("10");
Trigger trigger1 = TriggerBuilder.newTrigger().withIdentity("test trigger1", "group1").withSchedule(dailyAtHourAndMinute(08, 00)).build();
Trigger trigger2 = TriggerBuilder.newTrigger().withIdentity("test trigger2", "group1").withSchedule(dailyAtHourAndMinute(20, 01)).build();
Trigger trigger3 = TriggerBuilder.newTrigger().withIdentity("test trigger3", "group1").withSchedule(dailyAtHourAndMinute(23, 10)).build();
try {
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
JobDetail job1 = JobBuilder.newJob(NewsLetterSchedulerTest.class).withIdentity("test job1", "group1").build();
scheduler.scheduleJob(job1, trigger1);
JobDetail job2 = JobBuilder.newJob(NewsLetterSchedulerTest.class).withIdentity("test job2", "group1").build();
scheduler.scheduleJob(job2, trigger2);
JobDetail job3 = JobBuilder.newJob(NewsLetterSchedulerTest.class).withIdentity("test job3", "group1").build();
scheduler.scheduleJob(job3, trigger3);
} catch (SchedulerException e) {
e.printStackTrace();
} catch (Throwable e) {
e.printStackTrace();
}
}
public boolean getValue(){
System.out.println("Simple Method invoked from Scheduler");
return true;
}
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
SchedulerTest scheduler=new SchedulerTest ();
scheduler.getValue();
}
}
PS: Make sure that required jars your have to add to run above code
import static org.quartz.CronScheduleBuilder.dailyAtHourAndMinute;
import java.util.Calendar;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
public class SchedulerTest implements Job {
public static void main(String[] args) {
int HOUR_OF_DAY = Integer.parseInt("20");
int MINUTE_OF_HOUR = Integer.parseInt("10");
Trigger trigger1 = TriggerBuilder.newTrigger().withIdentity("test trigger1", "group1").withSchedule(dailyAtHourAndMinute(08, 00)).build();
Trigger trigger2 = TriggerBuilder.newTrigger().withIdentity("test trigger2", "group1").withSchedule(dailyAtHourAndMinute(20, 01)).build();
Trigger trigger3 = TriggerBuilder.newTrigger().withIdentity("test trigger3", "group1").withSchedule(dailyAtHourAndMinute(23, 10)).build();
try {
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
JobDetail job1 = JobBuilder.newJob(NewsLetterSchedulerTest.class).withIdentity("test job1", "group1").build();
scheduler.scheduleJob(job1, trigger1);
JobDetail job2 = JobBuilder.newJob(NewsLetterSchedulerTest.class).withIdentity("test job2", "group1").build();
scheduler.scheduleJob(job2, trigger2);
JobDetail job3 = JobBuilder.newJob(NewsLetterSchedulerTest.class).withIdentity("test job3", "group1").build();
scheduler.scheduleJob(job3, trigger3);
} catch (SchedulerException e) {
e.printStackTrace();
} catch (Throwable e) {
e.printStackTrace();
}
}
public boolean getValue(){
System.out.println("Simple Method invoked from Scheduler");
return true;
}
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
SchedulerTest scheduler=new SchedulerTest ();
scheduler.getValue();
}
}
PS: Make sure that required jars your have to add to run above code
Comments
Post a Comment