Quartz.2.1.x Job Scheduling

Source: Internet
Author: User

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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.