why quartz scheduleJob() execute many times?

Multi tool use
public class QuartzStudy implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) {
System.out.println(jobExecutionContext.getJobDetail().getKey().getName() + "-" + Thread.currentThread().getName() + "-" + Thread.currentThread().getId() + "-" + new Date());
}
private static SchedulerFactory schedulerFactory = new StdSchedulerFactory();
private static Scheduler scheduler;
static {
try {
scheduler = schedulerFactory.getScheduler();
scheduler.start();
} catch (SchedulerException e) {
e.printStackTrace();
}
}
public static void main(String args) throws SchedulerException, InterruptedException {
JobDetail jobDetail = JobBuilder.newJob(QuartzStudy.class).withIdentity("job1").build();
CronTrigger trigger = TriggerBuilder.newTrigger()
.withSchedule(CronScheduleBuilder.cronSchedule("*/5 * * * * ?").withMisfireHandlingInstructionIgnoreMisfires())
.build();
scheduler.scheduleJob(jobDetail, trigger);
Thread.sleep(10000);
scheduler.pauseJob(jobDetail.getKey());
}
}
This code should console twice and then pause?
But sometimes console three times,why?
job1-DefaultQuartzScheduler_Worker-1-14-Thu Jan 03 09:34:45 CST 2019
job1-DefaultQuartzScheduler_Worker-2-15-Thu Jan 03 09:34:50 CST 2019
job1-DefaultQuartzScheduler_Worker-3-16-Thu Jan 03 09:34:55 CST 2019
java quartz-scheduler
add a comment |
public class QuartzStudy implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) {
System.out.println(jobExecutionContext.getJobDetail().getKey().getName() + "-" + Thread.currentThread().getName() + "-" + Thread.currentThread().getId() + "-" + new Date());
}
private static SchedulerFactory schedulerFactory = new StdSchedulerFactory();
private static Scheduler scheduler;
static {
try {
scheduler = schedulerFactory.getScheduler();
scheduler.start();
} catch (SchedulerException e) {
e.printStackTrace();
}
}
public static void main(String args) throws SchedulerException, InterruptedException {
JobDetail jobDetail = JobBuilder.newJob(QuartzStudy.class).withIdentity("job1").build();
CronTrigger trigger = TriggerBuilder.newTrigger()
.withSchedule(CronScheduleBuilder.cronSchedule("*/5 * * * * ?").withMisfireHandlingInstructionIgnoreMisfires())
.build();
scheduler.scheduleJob(jobDetail, trigger);
Thread.sleep(10000);
scheduler.pauseJob(jobDetail.getKey());
}
}
This code should console twice and then pause?
But sometimes console three times,why?
job1-DefaultQuartzScheduler_Worker-1-14-Thu Jan 03 09:34:45 CST 2019
job1-DefaultQuartzScheduler_Worker-2-15-Thu Jan 03 09:34:50 CST 2019
job1-DefaultQuartzScheduler_Worker-3-16-Thu Jan 03 09:34:55 CST 2019
java quartz-scheduler
Can you try puttingscheduler.start();
afterscheduler.scheduleJob(jobDetail, trigger);
? Please share your response.
– Mukesh Prajapati
Jan 3 at 6:36
add a comment |
public class QuartzStudy implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) {
System.out.println(jobExecutionContext.getJobDetail().getKey().getName() + "-" + Thread.currentThread().getName() + "-" + Thread.currentThread().getId() + "-" + new Date());
}
private static SchedulerFactory schedulerFactory = new StdSchedulerFactory();
private static Scheduler scheduler;
static {
try {
scheduler = schedulerFactory.getScheduler();
scheduler.start();
} catch (SchedulerException e) {
e.printStackTrace();
}
}
public static void main(String args) throws SchedulerException, InterruptedException {
JobDetail jobDetail = JobBuilder.newJob(QuartzStudy.class).withIdentity("job1").build();
CronTrigger trigger = TriggerBuilder.newTrigger()
.withSchedule(CronScheduleBuilder.cronSchedule("*/5 * * * * ?").withMisfireHandlingInstructionIgnoreMisfires())
.build();
scheduler.scheduleJob(jobDetail, trigger);
Thread.sleep(10000);
scheduler.pauseJob(jobDetail.getKey());
}
}
This code should console twice and then pause?
But sometimes console three times,why?
job1-DefaultQuartzScheduler_Worker-1-14-Thu Jan 03 09:34:45 CST 2019
job1-DefaultQuartzScheduler_Worker-2-15-Thu Jan 03 09:34:50 CST 2019
job1-DefaultQuartzScheduler_Worker-3-16-Thu Jan 03 09:34:55 CST 2019
java quartz-scheduler
public class QuartzStudy implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) {
System.out.println(jobExecutionContext.getJobDetail().getKey().getName() + "-" + Thread.currentThread().getName() + "-" + Thread.currentThread().getId() + "-" + new Date());
}
private static SchedulerFactory schedulerFactory = new StdSchedulerFactory();
private static Scheduler scheduler;
static {
try {
scheduler = schedulerFactory.getScheduler();
scheduler.start();
} catch (SchedulerException e) {
e.printStackTrace();
}
}
public static void main(String args) throws SchedulerException, InterruptedException {
JobDetail jobDetail = JobBuilder.newJob(QuartzStudy.class).withIdentity("job1").build();
CronTrigger trigger = TriggerBuilder.newTrigger()
.withSchedule(CronScheduleBuilder.cronSchedule("*/5 * * * * ?").withMisfireHandlingInstructionIgnoreMisfires())
.build();
scheduler.scheduleJob(jobDetail, trigger);
Thread.sleep(10000);
scheduler.pauseJob(jobDetail.getKey());
}
}
This code should console twice and then pause?
But sometimes console three times,why?
job1-DefaultQuartzScheduler_Worker-1-14-Thu Jan 03 09:34:45 CST 2019
job1-DefaultQuartzScheduler_Worker-2-15-Thu Jan 03 09:34:50 CST 2019
job1-DefaultQuartzScheduler_Worker-3-16-Thu Jan 03 09:34:55 CST 2019
java quartz-scheduler
java quartz-scheduler
asked Jan 3 at 1:44


Lce ManLce Man
197
197
Can you try puttingscheduler.start();
afterscheduler.scheduleJob(jobDetail, trigger);
? Please share your response.
– Mukesh Prajapati
Jan 3 at 6:36
add a comment |
Can you try puttingscheduler.start();
afterscheduler.scheduleJob(jobDetail, trigger);
? Please share your response.
– Mukesh Prajapati
Jan 3 at 6:36
Can you try putting
scheduler.start();
after scheduler.scheduleJob(jobDetail, trigger);
? Please share your response.– Mukesh Prajapati
Jan 3 at 6:36
Can you try putting
scheduler.start();
after scheduler.scheduleJob(jobDetail, trigger);
? Please share your response.– Mukesh Prajapati
Jan 3 at 6:36
add a comment |
1 Answer
1
active
oldest
votes
First of all, */5 * * * * ?
means Every 5 seconds starting at :00 second after the minute
.
You are doing Thread.sleep(10000);
. This means main thread
will be paused for 10 seconds. But, since you are getting logs even in those 10 seconds, that means scheduler is independent of main thread
. So, at 10th second, scheduler will create a thread to run execute
and also after 10th second, you are pausing job
.If first one(i.e. creating of thread occurs before pausing
) occurs first, logs will be printed thrice. Whereas if second one(pausing
of job
occurs before spawning
new thread) occurs first, logs will be printed only twice. That's why sometime log is getting printed twice and sometimes thrice.
So, to be sure that logs are printed only twice, decrease sleep time to 9 seconds. I tried below code and it always prints logs twice:
public static void main(String args) throws SchedulerException, InterruptedException {
System.out.println("Main method:" + Thread.currentThread().getName() + "-" + Thread.currentThread().getId());
JobDetail jobDetail = JobBuilder.newJob(QuartzStudy.class).withIdentity("job1").build();
CronTrigger trigger = TriggerBuilder.newTrigger()
.withSchedule(CronScheduleBuilder.cronSchedule("*/5 * * * * ?").withMisfireHandlingInstructionIgnoreMisfires())
.build();
scheduler.scheduleJob(jobDetail, trigger);
scheduler.start(); // it's better to start job once you have scheduled one
Thread.sleep(9000);
scheduler.pauseJob(jobDetail.getKey());
System.out.println("Job is Paused!!!");
}
Please reply if it solves your problem.
– Mukesh Prajapati
Jan 4 at 2:01
i know your means, i solve this problem but i find scheduler.resumeJob() it execute twice how to solve it?
– Lce Man
Jan 4 at 3:04
scheduler.resumeJob()
is no where in question. Can you explain it more clearly... maybe by providing expected input and output. Where exactly are you placingresumeJob
?
– Mukesh Prajapati
Jan 4 at 3:07
stackoverflow.com/questions/54015360/…
– Lce Man
Jan 4 at 3:23
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54015360%2fwhy-quartz-schedulejob-execute-many-times%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
First of all, */5 * * * * ?
means Every 5 seconds starting at :00 second after the minute
.
You are doing Thread.sleep(10000);
. This means main thread
will be paused for 10 seconds. But, since you are getting logs even in those 10 seconds, that means scheduler is independent of main thread
. So, at 10th second, scheduler will create a thread to run execute
and also after 10th second, you are pausing job
.If first one(i.e. creating of thread occurs before pausing
) occurs first, logs will be printed thrice. Whereas if second one(pausing
of job
occurs before spawning
new thread) occurs first, logs will be printed only twice. That's why sometime log is getting printed twice and sometimes thrice.
So, to be sure that logs are printed only twice, decrease sleep time to 9 seconds. I tried below code and it always prints logs twice:
public static void main(String args) throws SchedulerException, InterruptedException {
System.out.println("Main method:" + Thread.currentThread().getName() + "-" + Thread.currentThread().getId());
JobDetail jobDetail = JobBuilder.newJob(QuartzStudy.class).withIdentity("job1").build();
CronTrigger trigger = TriggerBuilder.newTrigger()
.withSchedule(CronScheduleBuilder.cronSchedule("*/5 * * * * ?").withMisfireHandlingInstructionIgnoreMisfires())
.build();
scheduler.scheduleJob(jobDetail, trigger);
scheduler.start(); // it's better to start job once you have scheduled one
Thread.sleep(9000);
scheduler.pauseJob(jobDetail.getKey());
System.out.println("Job is Paused!!!");
}
Please reply if it solves your problem.
– Mukesh Prajapati
Jan 4 at 2:01
i know your means, i solve this problem but i find scheduler.resumeJob() it execute twice how to solve it?
– Lce Man
Jan 4 at 3:04
scheduler.resumeJob()
is no where in question. Can you explain it more clearly... maybe by providing expected input and output. Where exactly are you placingresumeJob
?
– Mukesh Prajapati
Jan 4 at 3:07
stackoverflow.com/questions/54015360/…
– Lce Man
Jan 4 at 3:23
add a comment |
First of all, */5 * * * * ?
means Every 5 seconds starting at :00 second after the minute
.
You are doing Thread.sleep(10000);
. This means main thread
will be paused for 10 seconds. But, since you are getting logs even in those 10 seconds, that means scheduler is independent of main thread
. So, at 10th second, scheduler will create a thread to run execute
and also after 10th second, you are pausing job
.If first one(i.e. creating of thread occurs before pausing
) occurs first, logs will be printed thrice. Whereas if second one(pausing
of job
occurs before spawning
new thread) occurs first, logs will be printed only twice. That's why sometime log is getting printed twice and sometimes thrice.
So, to be sure that logs are printed only twice, decrease sleep time to 9 seconds. I tried below code and it always prints logs twice:
public static void main(String args) throws SchedulerException, InterruptedException {
System.out.println("Main method:" + Thread.currentThread().getName() + "-" + Thread.currentThread().getId());
JobDetail jobDetail = JobBuilder.newJob(QuartzStudy.class).withIdentity("job1").build();
CronTrigger trigger = TriggerBuilder.newTrigger()
.withSchedule(CronScheduleBuilder.cronSchedule("*/5 * * * * ?").withMisfireHandlingInstructionIgnoreMisfires())
.build();
scheduler.scheduleJob(jobDetail, trigger);
scheduler.start(); // it's better to start job once you have scheduled one
Thread.sleep(9000);
scheduler.pauseJob(jobDetail.getKey());
System.out.println("Job is Paused!!!");
}
Please reply if it solves your problem.
– Mukesh Prajapati
Jan 4 at 2:01
i know your means, i solve this problem but i find scheduler.resumeJob() it execute twice how to solve it?
– Lce Man
Jan 4 at 3:04
scheduler.resumeJob()
is no where in question. Can you explain it more clearly... maybe by providing expected input and output. Where exactly are you placingresumeJob
?
– Mukesh Prajapati
Jan 4 at 3:07
stackoverflow.com/questions/54015360/…
– Lce Man
Jan 4 at 3:23
add a comment |
First of all, */5 * * * * ?
means Every 5 seconds starting at :00 second after the minute
.
You are doing Thread.sleep(10000);
. This means main thread
will be paused for 10 seconds. But, since you are getting logs even in those 10 seconds, that means scheduler is independent of main thread
. So, at 10th second, scheduler will create a thread to run execute
and also after 10th second, you are pausing job
.If first one(i.e. creating of thread occurs before pausing
) occurs first, logs will be printed thrice. Whereas if second one(pausing
of job
occurs before spawning
new thread) occurs first, logs will be printed only twice. That's why sometime log is getting printed twice and sometimes thrice.
So, to be sure that logs are printed only twice, decrease sleep time to 9 seconds. I tried below code and it always prints logs twice:
public static void main(String args) throws SchedulerException, InterruptedException {
System.out.println("Main method:" + Thread.currentThread().getName() + "-" + Thread.currentThread().getId());
JobDetail jobDetail = JobBuilder.newJob(QuartzStudy.class).withIdentity("job1").build();
CronTrigger trigger = TriggerBuilder.newTrigger()
.withSchedule(CronScheduleBuilder.cronSchedule("*/5 * * * * ?").withMisfireHandlingInstructionIgnoreMisfires())
.build();
scheduler.scheduleJob(jobDetail, trigger);
scheduler.start(); // it's better to start job once you have scheduled one
Thread.sleep(9000);
scheduler.pauseJob(jobDetail.getKey());
System.out.println("Job is Paused!!!");
}
First of all, */5 * * * * ?
means Every 5 seconds starting at :00 second after the minute
.
You are doing Thread.sleep(10000);
. This means main thread
will be paused for 10 seconds. But, since you are getting logs even in those 10 seconds, that means scheduler is independent of main thread
. So, at 10th second, scheduler will create a thread to run execute
and also after 10th second, you are pausing job
.If first one(i.e. creating of thread occurs before pausing
) occurs first, logs will be printed thrice. Whereas if second one(pausing
of job
occurs before spawning
new thread) occurs first, logs will be printed only twice. That's why sometime log is getting printed twice and sometimes thrice.
So, to be sure that logs are printed only twice, decrease sleep time to 9 seconds. I tried below code and it always prints logs twice:
public static void main(String args) throws SchedulerException, InterruptedException {
System.out.println("Main method:" + Thread.currentThread().getName() + "-" + Thread.currentThread().getId());
JobDetail jobDetail = JobBuilder.newJob(QuartzStudy.class).withIdentity("job1").build();
CronTrigger trigger = TriggerBuilder.newTrigger()
.withSchedule(CronScheduleBuilder.cronSchedule("*/5 * * * * ?").withMisfireHandlingInstructionIgnoreMisfires())
.build();
scheduler.scheduleJob(jobDetail, trigger);
scheduler.start(); // it's better to start job once you have scheduled one
Thread.sleep(9000);
scheduler.pauseJob(jobDetail.getKey());
System.out.println("Job is Paused!!!");
}
answered Jan 3 at 10:34
Mukesh PrajapatiMukesh Prajapati
916519
916519
Please reply if it solves your problem.
– Mukesh Prajapati
Jan 4 at 2:01
i know your means, i solve this problem but i find scheduler.resumeJob() it execute twice how to solve it?
– Lce Man
Jan 4 at 3:04
scheduler.resumeJob()
is no where in question. Can you explain it more clearly... maybe by providing expected input and output. Where exactly are you placingresumeJob
?
– Mukesh Prajapati
Jan 4 at 3:07
stackoverflow.com/questions/54015360/…
– Lce Man
Jan 4 at 3:23
add a comment |
Please reply if it solves your problem.
– Mukesh Prajapati
Jan 4 at 2:01
i know your means, i solve this problem but i find scheduler.resumeJob() it execute twice how to solve it?
– Lce Man
Jan 4 at 3:04
scheduler.resumeJob()
is no where in question. Can you explain it more clearly... maybe by providing expected input and output. Where exactly are you placingresumeJob
?
– Mukesh Prajapati
Jan 4 at 3:07
stackoverflow.com/questions/54015360/…
– Lce Man
Jan 4 at 3:23
Please reply if it solves your problem.
– Mukesh Prajapati
Jan 4 at 2:01
Please reply if it solves your problem.
– Mukesh Prajapati
Jan 4 at 2:01
i know your means, i solve this problem but i find scheduler.resumeJob() it execute twice how to solve it?
– Lce Man
Jan 4 at 3:04
i know your means, i solve this problem but i find scheduler.resumeJob() it execute twice how to solve it?
– Lce Man
Jan 4 at 3:04
scheduler.resumeJob()
is no where in question. Can you explain it more clearly... maybe by providing expected input and output. Where exactly are you placing resumeJob
?– Mukesh Prajapati
Jan 4 at 3:07
scheduler.resumeJob()
is no where in question. Can you explain it more clearly... maybe by providing expected input and output. Where exactly are you placing resumeJob
?– Mukesh Prajapati
Jan 4 at 3:07
stackoverflow.com/questions/54015360/…
– Lce Man
Jan 4 at 3:23
stackoverflow.com/questions/54015360/…
– Lce Man
Jan 4 at 3:23
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54015360%2fwhy-quartz-schedulejob-execute-many-times%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
t aov7ife9JQ8 HQ,LwlaYjqL 3WZYCrpGbIGUU2dk4Eri9sl
Can you try putting
scheduler.start();
afterscheduler.scheduleJob(jobDetail, trigger);
? Please share your response.– Mukesh Prajapati
Jan 3 at 6:36