I have quartz scheduler and I have created a trigger for specified time(Every day). And I also created another trigger for another specified time (Every three months once)
Below is my code to schedule two triggers to single scheduler.
Scheduler sch = new StdSchedulerFactory().getScheduler();
JobDetail jobDetail = JobBuilder.newJob(MFRScheduler.class)
.withIdentity("firstJob", "group1").build();
JobDetail jobDetail1 = JobBuilder.newJob(MFRScheduler.class)
.withIdentity("cleanTrigger", "group2").build();
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("firstJob", "group1")
.withSchedule(
CronScheduleBuilder.cronSchedule(schduledTime))
.build();
Trigger houseKeepingTrigger = TriggerBuilder
.newTrigger()
.withIdentity("mfrJobHouseKeeping","group2")
.withSchedule(
CronScheduleBuilder.cronSchedule(cleanTrigerTime)
)
.build();
sch.start();
sch.scheduleJob(jobDetail, trigger);
sch.scheduleJob(jobDetail1, cleanTrigger);
I have overided the method execute()
of org.quartz.Job
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
// TODO Auto-generated method stub
_log.info("--- This is in Execute method");
try{
Thread thread = new Thread(new FIRSTProcess("FIRST_THREAD"),"thread2");
thread.start();
thread.setName("FIRST_THREAD");
thread = null;
Thread.sleep(3000);
}catch(Exception e)
{
e.printStackTrace();
}
What my question is here One trigger triggers every day and content in execute()
method will execute, since I have only one method execute()
will get executed for two triggers.
What I need is the content in execute
method should get executed only when my firstTrigger
triggers when my second trigger triggers I would like to do some other things instead of making duplicate work of firstTrigger
Can please explain how to segergate the two triggers work.
Thanks in advance
As you want to do different work, I would suggest to create different implementations of the jobs (e.g. MFRSchedulerHouseKeeping.class). Then you can assign the triggers you want to the specific jobs.
An alternative approach would be to query the Trigger from the
JobExecutionContext
viagetTrigger()
and do the appropriate work on behalf of the result.