What happens if I do not shutdown() quartz scheduler - java

What would happen if I do not call the shutdown() method on my Quartz scheduler?
I have a job that needs to be run each day at different times the day:
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
JobDetail job = newJob(NotificationCronJob.class).withIdentity("notificationJob1", "notificationGroup1").build();
CronTrigger cronTriggerSunday = newTrigger().withIdentity("notificationTrigger1", "notificationGroup1")
.withSchedule(cronSchedule(Config.SUNDAY_NOTIFY))
.forJob(job)
.build();
CronTrigger cronTriggerMonday = newTrigger().withIdentity("notificationTrigger2", "notificationGroup1")
.withSchedule(cronSchedule(Config.MONDAY_NOTIFY))
.forJob(job)
.build();
CronTrigger cronTriggerTuesday = newTrigger().withIdentity("notificationTrigger3", "notificationGroup1")
.withSchedule(cronSchedule(Config.TUESDAY_NOTIFY))
.forJob(job)
.build();
CronTrigger cronTriggerWednesday = newTrigger().withIdentity("notificationTrigger4", "notificationGroup1")
.withSchedule(cronSchedule(Config.WEDENSDAY_NOTIFY))
.forJob(job)
.build();
CronTrigger cronTriggerThursday = newTrigger().withIdentity("notificationTrigger5", "notificationGroup1")
.withSchedule(cronSchedule(Config.THURSDAY_NOTIFY))
.forJob(job)
.build();
CronTrigger cronTriggerFriday = newTrigger().withIdentity("notificationTrigger6", "notificationGroup1")
.withSchedule(cronSchedule(Config.FRIDAY_NOTIFY))
.forJob(job)
.build();
CronTrigger cronTriggerSaturday = newTrigger().withIdentity("notificationTrigger7", "notificationGroup1")
.withSchedule(cronSchedule(Config.SATURDAY_NOTIFY))
.forJob(job)
.build();
scheduler.scheduleJob(job, cronTriggerSunday);
scheduler.scheduleJob(cronTriggerMonday);
scheduler.rescheduleJob(cronTriggerMonday.getKey(), cronTriggerMonday);
scheduler.scheduleJob(cronTriggerTuesday);
scheduler.scheduleJob(cronTriggerWednesday);
scheduler.scheduleJob(cronTriggerThursday);
scheduler.scheduleJob(cronTriggerFriday);
scheduler.scheduleJob(cronTriggerSaturday);
scheduler.start();
Each Config.DAY is a cron expression fore example 0 0 9 ? * 1 run each sunday at 9am.
Now the problem is if I shutdown the scheduler the job would never be run, so therefore I just start it and just let it run. But I am concerned with if that would result in some memory leak og threading problem of some kind, I cannot figure out if this is a well enough solution. My quartz properties are as follows:
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.MSSQLDelegate
org.quartz.jobStore.dataSource = myDS
org.quartz.dataSource.myDS.driver = com.microsoft.sqlserver.jdbc.SQLServerDriver
org.quartz.dataSource.myDS.URL = jdbc:sqlserver://localhost;databaseName=myDB
org.quartz.dataSource.myDS.user = myUser
org.quartz.dataSource.myDS.password = myPassword
org.quartz.dataSource.myDS.maxConnections = 5
org.quartz.jobStore.tablePrefix = QRTZ_
What I need to achieve in the end is an application that runs a job at the defined times and the scheduling should be mutable without restarting the application.

Quartz scheduler shouldn't call 'shutdown' method while your application is running. If you found any problem such as memory leak, you can issue the problem to Quartz community.
If 'shutdown' is called, Quartz scheduler is never restarted even if you call 'start' method again.
Please refer to the below URL which is Quartz documentation.
http://www.quartz-scheduler.org/documentation/quartz-2.x/cookbook/ShutdownScheduler.html

Related

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);

how to reschedule quartz job dynamically

I am trying to update the quartz job scheduling dynamically:
Initially it is after every 3 minute, but dynamically i want to make it 1 min.
here's my quartz.xml and java file
quartz.xml
<schedule>
<job>
<name>JOBNAME</name>
<group>GroupDummy</group>
<description>This is Job B</description>
<job-class>in.xyz.MyClass.java</job-class>
</job>
<trigger>
<cron>
<name>dummyTriggerNameB</name>
<group>MYTRIGGER_GROUP</group>
<job-name>JOBNAME</job-name>
<job-group>GroupDummy</job-group>
<cron-expression>0 0/3 * * * ?</cron-expression>
</cron>
</trigger>
</schedule>
MyClass.java
public void execute(JobExecutionContext arg0) throws JobExecutionException {
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger("dummyTriggerNameB", "MYTRIGGER_GROUP");
CronExpression cronExpression = new CronExpression("0 0/1 * * * ?");
cronTrigger.setCronExpression(cronExpression);
scheduler.rescheduleJob("dummyTriggerNameB", "MYTRIGGER_GROUP", cronTrigger);
}
I am trying the above java code to change the time from 3 minute to 1 minute dynamically but it is not happening "cronTrigger" is returning null.
Help to solve this problem so that i can reschedule dynamically.

quartz job run only once

I have created Job. I want to run it every minute
but it run only once.
following is my java class
String exp = "0 0/1 * 1/1 * ? *";
SchedulerFactory factory = new StdSchedulerFactory();
Scheduler scheduler = factory.getScheduler();
scheduler.start();
JobDetail job = JobBuilder.newJob(schedulartest.class).build();
Trigger trigger = TriggerBuilder.newTrigger()
.startNow()
.withSchedule(
CronScheduleBuilder.cronSchedule(exp))
.build();
scheduler.scheduleJob(job, trigger);
when i execute it it run only first time.
can someone help to figure out these
This is how you should do it :
CronScheduleBuilder cronSchedule = CronScheduleBuilder.cronSchedule( "* * * * * ?" );
Trigger trigger = TriggerBuilder.newTrigger().withIdentity( "trigger-" + id, group ).withSchedule( cronSchedule ).build();
scheduler.scheduleJob( job, trigger );

Two instances not doing automatic load balancing in Quartz scheduler on the same machine

Two instances in a clustered JobStore are not working on the same machine inspite of having the correct quartz properties (As given in the docs).
The problem seems that the load balancing is not happening and everytime it's the same instance that picks the job (The one that starts later).
============================================================================
Configure Main Scheduler Properties
org.quartz.scheduler.instanceName = MyClusteredScheduler
org.quartz.scheduler.instanceId = instance1
============================================================================
Configure ThreadPool
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 25
org.quartz.threadPool.threadPriority = 5
============================================================================
Configure JobStore
org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.useProperties = false
org.quartz.jobStore.dataSource = mySQLDS
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.isClustered = true
org.quartz.jobStore.clusterCheckinInterval = 1000
============================================================================
Configure Datasources
org.quartz.dataSource.mySQLDS.driver = com.mysql.jdbc.Driver
org.quartz.dataSource.mySQLDS.URL = jdbc:mysql://localhost:3306/quartz2
org.quartz.dataSource.mySQLDS.user = quartz2
org.quartz.dataSource.mySQLDS.password = quartz2123
org.quartz.dataSource.mySQLDS.maxConnections = 5
org.quartz.dataSource.mySQLDS.validationQuery=select 0 from dual
The other instance had instance id as instance2.

JDBCJobStore Confusion in Quartz Schdular

I am using Quartz JDBCJobStore and have following job definition
JobDetail job=newJob(HelloJob.class).withIdentity("demo11", "group11").
usingJobData("jobSays", "Hello Vikas")
.usingJobData("myFloatValue", 3.141f).storeDurably(true).
build();
and trigger as
Trigger trigger=newTrigger().withIdentity("Trigger11","group11")
.startNow().withSchedule(CronScheduleBuilder.
cronSchedule("0 0/1 * * * ?")).build();
I was of impression that Quartz will store jobSays and myFloatValue in the database but I am unable see any such property in the database.
Is there a way to store these JobData in the database?
Here is the quartz.property file
org.quartz.scheduler.instanceName = MyScheduler
org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 3
org.quartz.jobStore.dataSource = myDS
org.quartz.dataSource.myDS.driver=com.mysql.jdbc.Driver
org.quartz.dataSource.myDS.URL=jdbc:mysql://localhost:3306/quartz
org.quartz.dataSource.myDS.user=root
org.quartz.dataSource.myDS.password=root
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.MSSQLDelegate
org.quartz.jobStore.tablePrefix = QRTZ_
Quartz stores job data as BLOB datatype in QRTZ_JOB_DETAILS table.
Please check there.

Categories

Resources