quartz scheduler | quartz scheduler example | quartz java
OneTime a scheduler is initialized, it can be started, placed in stand-by mode, and shut-down. Note that once a scheduler is shut-down, it cannot be re-started without being re-initialized. Triggers do not execute jobs until the scheduler has been started, nor while it is in the paused-state.
- Scheduler :- this api interacting with the scheduler.
- Job :- this interface to be implemented by component that you want to be executed by the scheduler.
- JobDetail :- it is define instances of Jobs.
- JobBuilder :- this is define/build JobDetail instances, which define instances of Jobs.
- Trigger :- this is defines the scheduled on which Job will be executed.
- TriggerBuilder :- this is define/build Trigger instances.
Jobs and Triggers are given identifying keys as they are registered with the Quartz scheduler.
The keys of JobKey and TriggerKey allow them to be placed into group. which can be useful for organizing your jobs and triggers into categories like a reporting jobs and maintenance jobs. The name portion of the key of a job or trigger must be unique with-in the group - or in other words, the complete identifier of a job or trigger is the compound of the name and group.
cron scheduler Expressions :-
cron scheduler Expressions are use to configure instances of cron-trigger. cron scheduler Expressions are strings that are actually made up of seven subexpressions, that describe individual details of the scheduler. These sub-expression are separated with white space.
1) second
2) minute
3) hour
4) day-of-month
5) month
6) day-of-week
7) year (it is optional)
individual sub-expressions can contain ranges or lists.
For example, the day of week in the MON-WED, MON,WED or MON-WED,FRI.
'' can be used to say each possible value of this field. Therefore the '' character in the Month field of the previous example simply means every-month.* in the Day-Of-Week field would therefore obviously mean every day of the week.
All of the fields have a set of valid values that can be specified.
For example, the day of week in the MON-WED, MON,WED or MON-WED,FRI.
'' can be used to say each possible value of this field. Therefore the '' character in the Month field of the previous example simply means every-month.* in the Day-Of-Week field would therefore obviously mean every day of the week.
All of the fields have a set of valid values that can be specified.
1) These values should be - such as the numbers 0 to 59 for seconds
2) These values should be - such as the numbers 0 to 59 for minutes
3) These values should be - such as the numbers 0 to 23 for hours.
4) These values should be - such as the numbers 1 to 31 Day-of-Month, but take care you need to be how many days are in a given this month.
5) These values should be - such as the numbers 0 to 11, or by using the strings like JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV and DEC.
6) These values should be - such as the numbers 1 to 7 Days-of-Week (1 is allocated by Sunday), or by using the strings like SUN, MON, TUE, WED, THU, FRI and SAT.
the wild-card character "/" can be used to specify increments to values.
like, if you put 1/10 in the Minutes place,
it means every 10 minute of the hour. starting at 1st minute
If you used 5/20 in the Minutes field, it would mean every 20 minute of the hour. starting at 5th minute
or you can also write same as "1,11,21,31,41,51" OR "5,25,45" in the Minutes field.
Note the subtlety that /10 does * not mean every 10 minutes
it is like every 10th minute of the hour, starting at minute first
or in other words the same as specifying 0,10.
the wild-card character "?" is allowed for the day-of-month and day-of-week place.
It is used to specify not specific value.
This is useful when you need to specify something in one of the two fields,but not the other.
the alphabet "L" character is allowed for the day-of-month.
this character is short-hand-letter for "last", but it has different meaning in each of the two fields.
like that,the value "L" in the day-of-month means "the last day of the month".
for that means day 31 for in month January,
day 28 for in month February,
day 31 for in month March visa-versa.
Same as,the alphabet "L" character is allowed for the day-of-week.
it simply means "7" OR "SAT".
But if used in the day-of-week field use "L" value,
like "6L" or "FRIL" both mean "the last friday of this month".
You can also specify an offset from the last day of the month,
such as "L-2" which would mean the second-to-last day of the calendar month.
When using the "L" option, you can't use specify lists, or ranges of values,
it unexpected results.
the alphabet "W" is used to specify the weekday (Monday to Friday) nearest the given day.
like, if you were to specify "10W" as the value for the day-of-month field,
effect is: "the nearest weekday to the 10th of the month".
the wild-card character "#" is used to specify the value weekday of the month.
like, the value of "5#3" or "THU#3" in the day-of-week field means the third Thursday of the month.
An example of a complete cron scheduler Expressions is the string
"20 40 5 ? * FRI"
it means - every Friday on 05:40:20 am.
Some Example,
0 40 7 5 * ? Fire at 7:40 morning on the 5th day of every month
0 40 7 L-3 * ? Fire at 7:40 morning on the 3rd-to-last last day of every month
0 40 7 L * ? Fire at 7:40 morning on the last day of every month
0 25 20 ? * 5L Fire at 8:25 evening on the last Thursday of every month
0 15 19 ? * 4L Fire at 7:15 evening on the last Wednesday of every month
0 15 18 ? * 3L 2011-2017 Fire at 6 :15 evening on every last thursday of every month on the years of 2011, 2012, 2013, 2014, 2015, 2016, 2017
0 45 6 ? * 2#3 Fire at 6:45 morning on the third Monday of every month
Example :-
Require Lib :- Quartz 2.2.3 .tar.gz
Scheduler.java
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import static org.quartz.JobBuilder.newJob;
import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
/**
*
* @author vishal.khokhar
*/
public class Scheduler {
public static void main(String[] args) {
new Scheduler().test();
}
public void test() {
try {
// define the job and tie it to our MyJob class
JobDetail job = newJob(JOB.class)
.withIdentity("job1", "group1")
.build();
// Trigger the job to run now, and then repeat every 40 seconds
Trigger trigger = (CronTrigger) TriggerBuilder
.newTrigger()
.withIdentity("Vishal", "Scheduler")
.withSchedule(CronScheduleBuilder.cronSchedule("0 54 15 * * ?"))
.build();
// Schedulers can be immediately used to schedule jobs,
// but, they will not start executing any until the .start() method has been called.
org.quartz.Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
// quartz is schedule the job using trigger in which we set our job
scheduler.scheduleJob(job, trigger);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
import org.quartz.CronTrigger;
import static org.quartz.JobBuilder.newJob;
import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
/**
*
* @author vishal.khokhar
*/
public class Scheduler {
public static void main(String[] args) {
new Scheduler().test();
}
public void test() {
try {
// define the job and tie it to our MyJob class
JobDetail job = newJob(JOB.class)
.withIdentity("job1", "group1")
.build();
// Trigger the job to run now, and then repeat every 40 seconds
Trigger trigger = (CronTrigger) TriggerBuilder
.newTrigger()
.withIdentity("Vishal", "Scheduler")
.withSchedule(CronScheduleBuilder.cronSchedule("0 54 15 * * ?"))
.build();
// Schedulers can be immediately used to schedule jobs,
// but, they will not start executing any until the .start() method has been called.
org.quartz.Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
// quartz is schedule the job using trigger in which we set our job
scheduler.scheduleJob(job, trigger);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
JOB.java
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
/**
*
* @author vishal.khokhar
*/
public class JOB implements Job{
@Override
public void execute(JobExecutionContext jec) throws JobExecutionException {
//Here,you put your logic.
//Temporary print Hello
System.out.println("Hello This is Sample JOB TEST !!!");
}
}
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
/**
*
* @author vishal.khokhar
*/
public class JOB implements Job{
@Override
public void execute(JobExecutionContext jec) throws JobExecutionException {
//Here,you put your logic.
//Temporary print Hello
System.out.println("Hello This is Sample JOB TEST !!!");
}
}
No comments:
Post a Comment