difference between pauseJob and pauseTrigger in quartz scheduler? - java

What is the difference between pauseJob() and pauseTrigger() in quartz scheduler?
How can select one among them for use? now i want to pause/interept a specific job how can i do
my scheduler code is given bellow
JobDetail job = new JobDetail();
job.setName("pollerjob"+pollerId);
job.setJobClass(Pollersheduller.class);
job.getJobDataMap().put("socialMediaObj", socialMediaObj);
job.getJobDataMap().put("queue", queue);
//configure the scheduler time
SimpleTrigger trigger = new SimpleTrigger();
trigger.setName("pollerSocial"+pollerId);
trigger.setStartTime(new Date(System.currentTimeMillis() + 1000));
trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
trigger.setRepeatInterval(Long.parseLong(intervel));
//schedule it
Scheduler scheduler = null;
try {
scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);
} catch (SchedulerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

As you have probably noticed, in Quartz a single job can be associated with multiple triggers. And if you look into Quartz sources, you will see that the pauseJob method simply retrieves all triggers associated with the specified job and pauses them, whereas the pauseTrigger method pauses only a particular trigger. So that is the main difference.
Please note that pausing a job in Quartz does not pause a currently running running job, it merely prevents the job from being run in the future!
If you want to interrupt a running job, then you can use the interruptJob method defined in the org.quartz.Interruptable interface the job must implement. If your job implements this interface, then it is entirely up to you to implement the interrupting logic. For example, you can set some sort of a flag when the interruptJob method is called and then you need to check the value of this flag in the job's execute method.

Related

quartz job to wait till start and standby after completion

I'm using quartz job my requirement is like I get some data to be persist in DB but before that I need to perform some modification on given data so I started processing the data in background using quartz. But now what is happening some of the time job is getting standby even before starting and due to that some of the data payload didn't get processed.
How can I maintain the job to be wait until the job complete its work.
scheduler.start();
scheduler.scheduleJob(job, trigger);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
scheduler.standby();
In my code if job starts before 3 second the standby will wait for job to complete the task but some of the time job taking more time to stat.
Instead of making your job wait for data, you can
Start your job every x minutes
Don't spawn new jobs if there's already a working one so they
won't overlap

How can I build a cron-like job executor in Java?

I'd like to develop a Java program that executes tasks registered in a database. The tasks have their own cron-like schedule, which is an object of CronExpression of Quartz Scheduler, and saved in the database after being serialized.
Tasks should be executed anytime according to its schedule, so I think the program should be daemonized, and may be able to be restarted or stopped outside the program (like an usual service beneath /etc/init.d/)
I'm studying the examples of Quartz
and saw the program running continuously even if there's no sleep and shutdown method. This seems nice to achieve my purpose, but I'm not sure if this way can generate a daemon process.
// TODO: Retrieve cron format from the database
Trigger trigger = org.quartz.TriggerBuilder.newTrigger()
.withIdentity("trigger1", "group1")
.withSchedule(cronSchedule("* * * ? * MON-FRI"))
.startNow()
.build();
try {
sched.scheduleJob(job, trigger);
sched.start();
// Thread.sleep(90L * 1000L);
// sched.shutdown(true);
} catch (SchedulerException e) {
...
My question is
What is the best way to build a cron job scheduler which runs continuously on a server?
Thank you in advance, and any opinions or questions would be appreciated.

Quartz Scheduler pausing running job internally upon shutdown?

I am noticing that sometimes quartz scheduler is automatically pausing some of the jobs. Is this a bug or any configuration issue?
All scheduled jobs are using CronTrigger.
I am suspecting that whenever server is stopped, it maybe automatically pausing running job? I have following code for ServletContextListener for shutdown of application.
public void shutdownScheduler(Scheduler scheduler) {
try {
if (null != scheduler) {
scheduler.shutdown();
}
} catch (Exception e) {
log.error(e);
}
}
Some of the quartz properties are listed below...
org.quartz.scheduler.wrapJobExecutionInUserTransaction = false
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 20
org.quartz.threadPool.threadPriority = 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true
org.quartz.jobStore.misfireThreshold = 60000
Yes, it does go to standBy mode by default. If you check the code you will see that one of the first thing the code does after calling shutdown is invoking the standby() function.
If you want to avoid this behaviur, you have the option to call shutdown with a parameter:
shutdown(true)
which will force the Scheduler to wait for running jobs to complete first.
Confirmation is in the doc, but not too much about the details unfortunately.

Java Scheduler? (Add tasks etc.)

Good Day,
Is there any API for Java where I can "add" tasks like an OS? I have a ExecutorService that runs every 1 minute, and during this tick, I need it to send about 10 TCP messages to multiple sockets.
I currently have a function that goes sendMessage(string data,string ipAdd,int port)
I was wondering if there is an EASY API for me to simply go taskScheduler.addTask(sendMessage(..)) in a loop say 10 times for 10 different data, and I am guranteed for them to executed all simultaneously?
Thanks
yes there is. have a look at quartz scheduler.
its really not difficult to set-up:
// Grab the Scheduler instance from the Factory
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
// and start it off
scheduler.start();
// define the job and tie it to our HelloJob class
JobDetail job = newJob(HelloJob.class)
.withIdentity("job1", "group1")
.build();
// Trigger the job to run now, and then repeat every 40 seconds
Trigger trigger = newTrigger()
.withIdentity("trigger1", "group1")
.startNow()
.withSchedule(simpleSchedule()
.withIntervalInSeconds(40)
.repeatForever())
.build();
// Tell quartz to schedule the job using our trigger
scheduler.scheduleJob(job, trigger);

Quartz: Based on configured schedule, the given trigger will never fire

I am getting the "Based on configured schedule, the given trigger will never fire" error when scheduling my job. I have tried adding ".startNow()" to the trigger, but that didn't solve it. I don't understand what I have done wrong.
JobDetail jobDetail = newJob(DeploymentJob.class)
.withIdentity(scheduleName)
.usingJobData("uploadLocation", deployment.getUploadDir())
.build();
// Add the job to the Scheduler
scheduler.addJob(jobDetail, true);
// Create the trigger with cron expression
Trigger trigger = newTrigger()
.withIdentity(scheduleName)
.withSchedule(cronSchedule(cron))
.forJob(jobDetail)
.build();
// Tell quartz to schedule the job using our trigger
scheduler.scheduleJob(jobDetail, trigger); <--- Problem line.
scheduler.start();
Thanks in advance for the answers! Much appreciated!
Your setting time is beyond reach.Spring Quartz never execute the task which time is beyond reach.So change your setting time.

Categories

Resources