Quartz2.1.x multi-task execution case
1. Add a jar package
[Html]
<Dependency>
<GroupId> org. quartz-scheduler </groupId>
<ArtifactId> quartz </artifactId>
<Version> 2.1.0 </version>
</Dependency>
2. Create a task
[Java]
/**
* Add a task
* @ Param scheduler
* @ Param jobName
* @ Param jobGroup
* @ Param cron
*/
Private void addJob (Scheduler schedname, String jobName, String jobGroup, String cron ){
Try {
JobDetail jobOne = JobBuilder. newJob (GbSimpleJob. class). withIdentity (jobName, jobGroup). build ();
CronTrigger trgOne = TriggerBuilder. newTrigger (). withIdentity (jobName, jobGroup)
. WithSchedule (CronScheduleBuilder. cronSchedule (cron) // [0 0/1 ***?] Run Once every minute
. ForJob (jobName, jobGroup). build ();
Scheduler. scheduleJob (jobOne, trgOne );
} Catch (ParseException e ){
E. printStackTrace ();
} Catch (SchedulerException e ){
E. printStackTrace ();
}
}
[Java]
GbSimpleJob code
[Java]
<Pre name = "code" class = "java"> package com. gbsoft. quartz. gbsoft;
Import java. text. SimpleDateFormat;
Import java. util. Date;
Import org. quartz. Job;
Import org. quartz. JobExecutionContext;
Import org. quartz. JobExecutionException;
Import org. quartz. JobKey;
/**
* @ Description GbSimpleJob
* @ Class GbSimpleJob
* @ Author is gone
* @ Date 2012-11-9
*/
Public class GbSimpleJob implements Job {
Private SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss ");
/**
* Execute the task
*/
@ Override
Public void execute (JobExecutionContext jobExecutionContext) throws JobExecutionException {
System. out. println ("start job task start, time:" + sdf. format (new Date ()));
JobKey jobKey = jobExecutionContext. getJobDetail (). getKey ();
System. out. println ("task --- name:" + jobKey. getName () + ", group:" + jobKey. getGroup ());
System. out. println ("NO content! ");
System. out. println ("end job ");
}
}
</Pre> <br>
<Br>
<Pre> </pre>
<Br>
3. Add multiple tasks
<P> </p> <pre name = "code" class = "java"> private void one (){
Try {
Scheduler schedfactory = StdSchedulerFactory. getdefaschscheduler ();
AddJob (scheduler, "job1", "jobGroup1", "0 0/1 ***? ");
AddJob (scheduler, "job2", "jobGroup2", "0 0/2 ***? ");
Scheduler. start ();
Scheduler. pauseJob (new JobKey ("job1", "jobGroup1 "));
} Catch (SchedulerException e ){
E. printStackTrace ();
} </Pre> <br>
4. Test the main method <p> </p>
<P> </p> <pre name = "code" class = "java"> /**
* @ Param args
*/
Public static void main (String [] args ){
System. out. println ("test started ");
SchedulerHelper test = new SchedulerHelper ();
Test. one (); www.2cto.com
System. out. println ("test ended ");
} </Pre> <br>
<Br>
<P> </p>