why quartz scheduleJob() execute many times? - java

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

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!!!");
}

Related

Java Spring - manage threads for sync requests

I have Java web application with REST calls using SPRING.
I want to control the number of threads the application is opening for the requests.
So I added Thread config:
package myPackage;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
#Configuration
public class ThreadConfig {
#Bean
public TaskExecutor threadPoolTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(1);
executor.setMaxPoolSize(1);
executor.initialize();
return executor;
}
}
I'm using Sync service not Async, I tested it and it doesn't limit the threads handling the requests, it handles them all at the same time.
What I was expecting is when I send 2 requests at a time - either the 2nd request will be thrown or it will wait until the 1st request will finish.
I'm not implementing Thread in my application at all.
This is the relevant code from my controller:
#RestController
public class Module1Controller {
#RequestMapping(method = RequestMethod.GET, path = "/module1")
InterruptedException {
public Module1 Module1() throws InterruptedException {
Date startDate = new Date();
System.out.println("Thread #: " + Thread.currentThread().getId() + " Request received at: " + startDate);
Thread.sleep(10000);
Date endDate = new Date();
long diff = endDate.getTime() - startDate.getTime();
long seconds = TimeUnit.MILLISECONDS.toSeconds(diff);
System.out.println("Thread #: " + Thread.currentThread().getId() + " thread released at: " + endDate + ", total seconds: " + seconds);
return new Module1(new Clock());
}
This is the console result:
Thread #: 34 Request received at: Sun Dec 17 10:16:20 IST 2017
Thread #: 35 Request received at: Sun Dec 17 10:16:21 IST 2017
Thread #: 34 thread released at: Sun Dec 17 10:16:30 IST 2017, total seconds: 10
Thread #: 35 thread released at: Sun Dec 17 10:16:31 IST 2017, total seconds: 10
What am I missing here?
The problem is that the creation of a TaskExecutor in a configuration bean has no effect on your RestController.
The easiest way to make your RestController process only 1 request at a time is to make the handling method synchronized, e.g. like this:
#RequestMapping(method = RequestMethod.GET, path = "/module1")
public synchronized Module1 getModule1() throws InterruptedException {
If you want a certain maximum number of requests to be processed simultaneously you can use a FixedThreadPool, e.g. like this:
// allow only 2 requests at a time, more requests are automatically placed in a queue
private final ExecutorService es = Executors.newFixedThreadPool(2);
#RequestMapping(method = RequestMethod.GET, path = "/module1")
public Module1 getModule1() throws ExecutionException, InterruptedException {
Future<Module1> result = es.submit(new Callable<Module1>() {
#Override
public String call() throws Exception {
try {
//.... do your work here....
return Module1()
} catch (InterruptedException e) {
return null;
}
}
});
return result.get();
}
I'm not sure why you would want to do this. Limiting the number of requests will result in bad performance and users are not going to like this.
You can not control the threads of request in application instead in container. Maybe you want to run some tasks in limited threads in application. You can do like this:
#RestController
public class ThreadController {
#Autowired
private TaskExecutor taskExecutor;
#RequestMapping(method = RequestMethod.GET, path = "/thread")
public void Module1() {
taskExecutor.execute(new Runnable() {
#Override
public void run() {
Date startDate = new Date();
System.out.println("Thread #: " + Thread.currentThread().getId() +
" Request received at: " + startDate);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Date endDate = new Date();
long diff = endDate.getTime() - startDate.getTime();
long seconds = TimeUnit.MILLISECONDS.toSeconds(diff);
System.out.println("Thread #: " + Thread.currentThread().getId() +
" thread released at: " + endDate + ", total seconds: " + seconds);
}
});
}
}
The result:
Thread #: 55 Request received at: Sun Dec 17 22:40:57 CST 2017
Thread #: 55 thread released at: Sun Dec 17 22:41:07 CST 2017, total seconds: 10
Thread #: 55 Request received at: Sun Dec 17 22:41:16 CST 2017
Thread #: 55 thread released at: Sun Dec 17 22:41:26 CST 2017, total seconds: 10
Thread #: 55 Request received at: Sun Dec 17 22:41:32 CST 2017
Thread #: 55 thread released at: Sun Dec 17 22:41:42 CST 2017, total seconds: 10

How to restrict cron trigger executed a job only once when fired in quartz scheduler

In my quartz scheduler my Main class is executed 5 or 6 times when a cron trigger firedin every 20 seconds.I want to restrict to execute Main class only once when cron trigger fired in every 20 seconds
This is my CronTrigger
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler scheduler = sf.getScheduler();
JobDetail job = JobBuilder.newJob(Main.class).build();
Date startTime = DateBuilder.nextGivenSecondDate(null, 10);
// run every 20 seconds infinite loop
CronTrigger crontrigger = TriggerBuilder.newTrigger().startAt(startTime).withSchedule(CronScheduleBuilder.cronSchedule("0/20 * * * * ?")).build();
scheduler.start();
scheduler.scheduleJob(job, crontrigger);
This is Main Class method
Main.java
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("Trigger Starts..");
}
My actual output is
Trigger 1 - When cron fires first time it will execute 3 or 4 times
Trigger Starts..
Trigger Starts..
Trigger Starts..
Trigger 2 - When cron fires second time it will execute 6 or 7 times
Trigger Starts..
Trigger Starts..
Trigger Starts..
Trigger Starts..
Trigger Starts..
Trigger Starts..
Trigger Starts..
Trigger Starts..
6 or 7 times this Main class is called. I want to restrict this situation
My expected output is
Trigger 1 (first 20 sec)
Trigger Starts..
Trigger 2 (next 20 sec)
Trigger Starts..
It should be executed once
On my system, I tested it and it works perfectly fine. Can you please use the following template and please share it's output and some info about logger if used.
Job:
public void execute(JobExecutionContext context) throws JobExecutionException
{
int count;
if(context.get("count") == null)
context.put("count", count = 1);
else
context.put("count", count = (Integer) context.get("count") + 1);
System.out.println("Hello Quartz!, Count: " + count);
}
This is my CronTrigger:
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler scheduler = sf.getScheduler();
JobDetail job = JobBuilder.newJob(HelloJob.class).build();
// run every 20 seconds infinite loop
CronTrigger crontrigger = TriggerBuilder.newTrigger().withSchedule(
CronScheduleBuilder.cronSchedule("0/20 * * * * ?")).build();
scheduler.start();
scheduler.scheduleJob(job, crontrigger);

schedule a task after a delay in executor service

I want a task to be executed after a delay of 3 seconds and my one task takes 2 seconds to finish.
The output i am getting is showing interval of 5 seconds
Note:The Student class implements Callable interface
I have the following queries
Why there is delay of 5 seconds coming.How can make a delay of 3
seconds Why are thread 1 is showed in second execution ,it should be
thread two
The output i am getting is
The time is : Sat Nov 26 15:08:02 IST 2016
Doing a task during : prerna - Time - Sat Nov 26 15:08:06 IST 2016
pool-1-thread-1 Helloprerna
Doing a task during : abc - Time - Sat Nov 26 15:08:11 IST 2016
pool-1-thread-1 Helloabc
Doing a task during : def - Time - Sat Nov 26 15:08:16 IST 2016
pool-1-thread-2 Hellodef
Doing a task during : xyz - Time - Sat Nov 26 15:08:21 IST 2016
pool-1-thread-1 Helloxyz
Doing a task during : ritu - Time - Sat Nov 26 15:08:26 IST 2016
pool-1-thread-3 Helloritu
Doing a task during : babita - Time - Sat Nov 26 15:08:31 IST 2016
pool-1-thread-2 Hellobabita
The code:
private String display(String name2) {
try {
// System.out.println(Thread.currentThread().getName());
name2=Thread.currentThread().getName()+" Hello"+ name;
System.out.println("Doing a task during : " + name + " - Time - " + new Date());
Thread.sleep(000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return name2;
}
#Override
public String call() throws Exception {
// TODO Auto-generated method stub
if (name == "archana") {
throw new Exception();
}
/*} catch (Exception e) {
// TODO Auto-generated catch block
// e.printStackTrace();
}finally{
return "error";
}*/
return display(name);
}
public class ExecutorScheduleDemo {
public static void main(String args[]) throws InterruptedException{
ScheduledExecutorService executor= Executors.newScheduledThreadPool(5);
ArrayList<Student> list = new ArrayList<Student>();
list.add(new Student("prerna"));
list.add(new Student("abc"));
//list.add(new Student("archana"));
list.add(new Student("def"));
list.add(new Student("xyz"));
list.add(new Student("ritu"));
list.add(new Student("babita"));
System.out.println("The time is : " + new Date());
List<Future<String>> resultList= new ArrayList<Future<String>>();
for(Student s:list){
Future<String> f=executor.schedule(s, 3, TimeUnit.SECONDS);
try {
System.out.println(f.get());
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Use scheduleAtFixedRate(Runnable, long initialDelay, long period, TimeUnit timeunit) instead of schedule(Runnable task, long delay, TimeUnit timeunit).
scheduleAtFixedRate (Runnable, long initialDelay, long period,
TimeUnit timeunit)
Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given period; that is executions will commence after initialDelay then initialDelay+period, then initialDelay + 2 * period, and so on. If any execution of the task encounters an exception, subsequent executions are suppressed. Otherwise, the task will only terminate via cancellation or termination of the executor. If any execution of this task takes longer than its period, then subsequent executions may start late, but will not concurrently execute.
next execution.
To complete eeedev's answer, since your object seems to be a Callable:
You can simply create a new FutureTask by passing your Callable to the constructor, as described in the oracle docs
Note that the FutureTask's type parameter must be the same as the Callable's.
Example:
class Main {
public static void main(String[] args) {
Foo foo = new Foo();
FutureTask<String> fooFutureTask = new FutureTask<>(foo);
}
}
class Foo implements Callable<String> {
#Override
public String call() throws Exception {
return "Calling";
}
}
You can then schedule your newly created FutureTask for execution as described by eeedev.

Add scripts dynamically as jobs using property/xml file in Quartz

Scenario : I want to create a scheduler application which should run shell scripts as per the defined schedule. To keep it simple, I want the user to add script name and execution timings in some external file (properties/xml) which will be used by my application. For now, I am planning to run this application as a background process on Linux server. In future may be we'll make it as a web-app.
What I've tried till now:
I came across xmlschedulingdataprocessorplugin for this purpose but it requires user to write jobs as Java code and then add it in XML file.
I found some examples for scheduling which presently isn't working.
Please suggest some helpful quartz API which can help me in fulfilling this purpose.
UPDATE:
public class CronTriggerExample {
public static void main(String[] args) throws Exception {
String[] a = {"script1.sh:0/10 * * * * ?", "script2.sh:0/35 * * * * ?"};
for (String config : a) {
String[] attr = config.split(":");
System.out.println("Iterating for : "+attr[0]);
JobKey jobKey = new JobKey(attr[0], attr[0]);
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity(attr[0], attr[0])
.withSchedule(CronScheduleBuilder.cronSchedule(attr[1]))
.build();
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.getContext().put("val", config);
JobDetail job = JobBuilder.newJob(HelloJob.class).withIdentity(jobKey).build();
scheduler.start();
scheduler.scheduleJob(job, trigger);
System.out.println("=======================");
}
}
}
My HelloJob class:
public class HelloJob implements Job {
public void execute(JobExecutionContext context) throws JobExecutionException {
String objectFromContext = null;
Date date = new Date();
try {
SchedulerContext schedulerContext = context.getScheduler().getContext();
objectFromContext = (String) schedulerContext.get("val");
} catch (SchedulerException ex) {
ex.printStackTrace();
}
System.out.println("Triggered "+objectFromContext+" at: "+date);
}
}
OUTPUT:
Iterating for : script1.sh
log4j:WARN No appenders could be found for logger (org.quartz.impl.StdSchedulerFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
=======================
Iterating for : script2.sh
=======================
Triggered script2.sh:0/35 * * * * ? at: Mon Apr 18 12:21:50 IST 2016
Triggered script2.sh:0/35 * * * * ? at: Mon Apr 18 12:22:00 IST 2016
Triggered script2.sh:0/35 * * * * ? at: Mon Apr 18 12:22:00 IST 2016
Triggered script2.sh:0/35 * * * * ? at: Mon Apr 18 12:22:10 IST 2016
Triggered script2.sh:0/35 * * * * ? at: Mon Apr 18 12:22:20 IST 2016
Triggered script2.sh:0/35 * * * * ? at: Mon Apr 18 12:22:30 IST 2016
Triggered script2.sh:0/35 * * * * ? at: Mon Apr 18 12:22:35 IST 2016
Triggered script2.sh:0/35 * * * * ? at: Mon Apr 18 12:22:40 IST 2016
What am I missing? I tried to create new Job for each iteration and assign script names as JobExecutionContext
The below tutorial help you to schedule shell script.
http://www.mkyong.com/java/how-to-run-a-task-periodically-in-java/
By using
Runtime.getRuntime().exec("sh shellscript.sh");
You can run shell script.
I would take the following approach :
You create a class JobShellRunner which will implement Job interface from quartz :
public class JobShellRunner implements Job {
#Override
public void execute(JobExecutionContext context)
throws JobExecutionException {
// here you take need information about the shell script you need to run, from the context and run the shell script
}
}
read properties files (i suppose here it will be available information about which shell script to run and it's schedule information). For each information needed you will create it's context and it's trigger:
JobKey jobKey = new JobKey("jobShellRunner", "group1");
// put in the job key need information about shell script (path, etc)
JobDetail jobA = JobBuilder.newJob(JobShellRunner.class)
.withIdentity(jobKey).build();
Then the trigger (note that on the cron expression you should complete with the one that you read from properties file):
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("dummyTriggerName1", "group1")
.withSchedule(
CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
.build();
Then schedule the job
scheduler.scheduleJob(jobA, trigger);
you can do something like as following way,
First of need to go likewise,
create your java application which has one scheduled-job which will read at
some time interval one propery/xml file which will provide an information
for which shell_file needs to execute and at what time.
While your program's scheduled-job read that property/xml file and getting
information as following,
2.1. Shell-Script-File-Name
2.2. Timing at what time that script needs to be execute.
This information which is read by above(step-2), with help of it, this job
will create newer independent-job which is fully responsible for execute
shell script at particular time.(that time will be your job-time which is
read from your propery/xml file). also take care of it to it should be one time only(as per your requirement).
this above step repeatedly does untill whole information read by this job and every time will generate one newer job.
in case after some time user edit/updated/added new line into property/xml file this java program's scheduled job will read only
that newer changes and accordingly does as like above explained.
you can see below image for better understanding purpose,
For scheduling purpose you can set-up spring-quartz API for schedule job.
here, I am going to give you little bit pseudo code,
public class JobA implements Job {
#Override
public void execute(JobExecutionContext context)
throws JobExecutionException {
// continues read property/xml file untill while file not read
// based upon above read info. generate new job(one time only) at runtime which has capability to execute shell script
// shell script can be execute by java program by this ,
// Runtime.getRuntime().exec("sh /full-path/shell_script_name.sh");
}
}
............
public class CronTriggerExample {
public static void main( String[] args ) throws Exception
{
JobKey jobKeyA = new JobKey("jobA", "group1");
JobDetail jobA = JobBuilder.newJob(JobA.class)
.withIdentity(jobKeyA).build();
Trigger trigger1 = TriggerBuilder
.newTrigger()
.withIdentity("dummyTriggerName1", "group1")
.withSchedule(
CronScheduleBuilder.cronSchedule("0/5 * * * * ?")) // you can set here your comfortable job time...
.build();
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(jobA, trigger1);
}
}
So, this is an idea what I believe and represent over here, which is top-most suitable as per your requirement.

Quartz scheduleJob Exceptions

Ok, so here I've got a Quartz(version 2.2.1) job that should instantiate two other jobs, create and add two triggers for each job, and add both jobs to the scheduler, deleting them after a set interval that's passed in through the JobDataMap.
When this job is run by the scheduler, it throws the following exception:
org.quartz.JobPersistenceException: The job (DEFAULT.countdown15, countdownGroup) referenced by the trigger does not exist.
The exception is thrown at line 71:
sched.scheduleJob(countdown15Trigger2);
I couldn't figure out why the job doesn't exist at that point, since it should have been created by the earlier newJob call and added to the scheduler by line 70:
sched.scheduleJob(countdown15, countdown15Trigger1);
So, I temporarily commented out that line and attempted to run the program again, whereupon I got this exception at line 72:
line 72:
sched.scheduleJob(countdown60, countdown60Trigger1);
exception:
org.quartz.SchedulerException: Trigger does not reference given job!
Ok, so now I've got two scheduleJob calls throwing two different exceptions, even though as far as I can tell I'm following the correct syntax. But the first scheduleJob call must be working correctly, because the exceptions are occurring later in the code, so for now I'll comment out all the other scheduleJob calls and see what happens.
And then I get this error at line 70, the first and now only scheduleJob call:
org.quartz.ObjectAlreadyExistsException: Unable to store Job : 'countdownGroup.countdown15', because one already exists with this identification.
I don't really know where to go from here, so here's the complete code for the top level job. Any ideas?
public class ShowClockJob implements Job {
public ShowClockJob() {}
#Override
public void execute(JobExecutionContext context)
throws JobExecutionException {
JobDataMap dm = context.getJobDetail().getJobDataMap();
int min = dm.getInt("min"); //show's runtime in minutes
int ms = min * 60000; //convert the runtime in minutes to millisec
Scheduler sched = context.getScheduler();
JobDetail countdown15 = newJob(CountdownJob15.class)
.withIdentity(jobKey("countdown15", "countdownGroup"))
.build();
JobDetail countdown60 = newJob(CountdownJob60.class)
.withIdentity("countdown60", "countdownGroup")
.build();
Trigger countdown15Trigger1 = newTrigger()
.withIdentity("countdown15Trigger1", "countdownGroup")
.withSchedule(cronSchedule("45 5 * * * ?")
.withMisfireHandlingInstructionDoNothing())
.forJob(jobKey("countdown15", "countdownGroup"))
.build();
Trigger countdown15Trigger2 = newTrigger()
.withIdentity("countdown15Trigger2", "countdownGroup")
.withSchedule(cronSchedule("55 19,32,46 * * * ?")
.withMisfireHandlingInstructionDoNothing())
.forJob(jobKey("countdown15, countdownGroup"))
.build();
Trigger countdown60Trigger1 = newTrigger()
.withIdentity("countdown60Trigger1", "countdownGroup")
.withSchedule(cronSchedule("0 17,29,44 * * * ?")
.withMisfireHandlingInstructionDoNothing())
.forJob(jobKey("countdown60, countdownGroup"))
.build();
Trigger countdown60Trigger2 = newTrigger()
.withIdentity("countdown60Trigger2", "countdownGroup")
.withSchedule(cronSchedule("50 57 * * * ?")
.withMisfireHandlingInstructionDoNothing())
.forJob("countdown60, countdownGroup")
.build();
try {
sched.scheduleJob(countdown15, countdown15Trigger1);
sched.scheduleJob(countdown15Trigger2);
sched.scheduleJob(countdown60, countdown60Trigger1);
sched.scheduleJob(countdown60Trigger2);
Thread.sleep(ms); //sleep for the length of the showtime,
//...then shut down the countdown jobs
sched.deleteJob(jobKey("countdown15","countdownGroup"));
sched.deleteJob(jobKey("countdown60","countdownGroup"));
} catch (SchedulerException | InterruptedException ex) {
Logger.getLogger(ShowClockJob.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Turns out it was a simple syntax error. In all but the first trigger creation blocks, at the forJob method, I left out the middle two parenthesis that seperated the jobKey and jobGroup into two strings.
So this:
forJob("countdown60, countdownGroup");
Should have been this:
forJob("countdown60", "countdownGroup");

Categories

Resources