There are many examples of Spring+quartz integration, where integration is not mentioned;
If you configure a fixed job, often use Methodinvokingjobdetailfactorybean, is also good, the problem is that this class does not implement serializable interface, resulting in the job information into the database, it does not work,
This is one of the triggers. The following is the main content of the article.
Premises and Objectives
1.job information stored in database
2. You can add a fixed job (written in a configuration file) to your project, or you can add a dynamic job (such as sending a message regularly, adding a job from Java code)
3. To be able to use in the actual project value
configuration steps begin (In other words, writing a program is writing a configuration file)
1. Configure DataSource, Schedulerfactorybean
Using MySQL
<!--datasource-->
<bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource" destroy-method= "Close" >
<property name= "Driverclassname" value= "${jdbc.driverclassname}"/>
< Property name= "url" value= "${jdbc.url}"/>
<property name= "username" value= "${jdbc.username}"/>
<property name= "Password" value= "${jdbc.password}"/>
</bean>
<!--schedulerfactory-->
<bean id= "Schedulerfactory" lazy-init= "true" class= " Org.springframework.scheduling.quartz.SchedulerFactoryBean ">
<property name=" Autostartup "value=" ${ Scheduler.auto.startup} "/>
<property name=" DataSource "ref=" DataSource "/>" <property name= "
Triggers ">
<list>
<ref bean=" Testfixedtriggerbean "/>
</list>
</property >
</bean>
${scheduler.auto.startup} is a Boolean variable that specifies whether to automatically enable
MySQL database SQL files See the project others directory, you can also go to Quartz's website to download http://www.quartz-scheduler.org/
2. Configure a fixed job
Because serialization (Serializable) is required to store job information in the database, the Methodinvokingjobdetailfactorybean object cannot be used.
It is recommended that you use Crontriggerfactorybean, configured as follows
<bean id= "<span style=" font-family:arial, Helvetica, Sans-serif; >testfixedtriggerbean</span><span style= "font-family:arial, Helvetica, Sans-serif;" > "</span> class=" Org.springframework.scheduling.quartz.CronTriggerFactoryBean ">
<property Name= "Jobdetail" ref= "Testfixedjobdetailbean"/> <property name= "cronexpression" value= "${"
Test.fixed.cron.expression} "/>
</bean>
${test.fixed.cron.expression} is the corresponding cron expression, such as 0/5 * * * *?
Configure the corresponding Jobdetailfactorybean
<bean id= "Testfixedjobdetailbean"
class= "Org.springframework.scheduling.quartz.JobDetailFactoryBean" >
<property name= "Jobclass" value= "Com.andaily.service.scheduler.TestFixedJobDetailBean"/>
< Property name= "Durability" value= "true"/>
</bean>
Note: Be sure to put the durability=true; Jobclass is to perform the specific class of tasks, need to be a subclass of the job, the general inheritance Quartzjobbean can
public class Testfixedjobdetailbean extends Quartzjobbean {
@Override
protected void executeinternal ( Jobexecutioncontext context) throws Jobexecutionexception {
System.out.println ("I am working on" + New Date ());
}
}
Overlay Executeinternal method, inside write business implementation
Note: Because the code here does not have a transaction, if the business method requires a transaction, take care of it yourself.
3. Configure a dynamic job
The so-called dynamic, is in the program running, by the use of people manually to join the job, commonly used such as mail regularly sent, task reminders, etc.
The essence is to tell the job to Schedulerfactory, let it deal with
For this we add the following objects
Dynamicschedulerfactory--Managing dynamic job registration, deletion, etc., needs to be configured in an XML configuration file; Of course, you definitely need a reference to the Schedulerfactory object
Dynamicjob-Contains all the information required by a dynamic job, such as a cron expression. Created by program when needed
Public final class Dynamicschedulerfactory implements Initializingbean {private static final Logger LOG = loggerfact
Ory.getlogger (Dynamicschedulerfactory.class);
private static Scheduler Scheduler; Public dynamicschedulerfactory () {}/** * Register a job * @param job Dynamicjob * @retur N True is register successful * @throws Schedulerexception */public static Boolean registerjob (Dynamicjob J
OB) throws Schedulerexception {final Triggerkey Triggerkey = Job.triggerkey ();
if (scheduler.checkexists (Triggerkey)) {final Trigger Trigger = Scheduler.gettrigger (Triggerkey);
throw new Schedulerexception ("already exist trigger [" + Trigger + "] by key [" + Triggerkey +] in Scheduler ");
Final Cronschedulebuilder Cronschedulebuilder = Cronschedulebuilder.cronschedule (Job.cronexpression ()); Final Crontrigger Crontrigger = Triggerbuilder.newtrigger (). Withidentity (TriggeRkey). Withschedule (Cronschedulebuilder). build ();
Final Jobdetail jobdetail = Job.jobdetail ();
Final date = Scheduler.schedulejob (Jobdetail, Crontrigger);
Log.debug ("Register dynamicjob {} on [{}]", job, date);
return true; /** * Remove exists job * * @param existjob A dynamicjob which exists in Scheduler * @return True is remove successful * @throws schedulerexception */public static Boolean removejob (Dynamicjob existjo
b) throws Schedulerexception {final Triggerkey Triggerkey = Existjob.triggerkey ();
Boolean result = false;
if (scheduler.checkexists (Triggerkey)) {result = Scheduler.unschedulejob (Triggerkey);
} log.debug ("Remove dynamicjob {} result [{}]", existjob, result);
return result;
public void Setscheduler (Scheduler Scheduler) {dynamicschedulerfactory.scheduler = Scheduler; @Override public void Afterpropertiesset () throws Exception {Assert.notnull (Scheduler, "Scheduler is
Null ");
Log.info ("Initial dynamicschedulerfactory successful, scheduler instance: {}", scheduler);
}
public class Dynamicjob {//job class private class<? extends job> target;
Cron expression private String cronexpression;
Private String jobgroup = Scheduler.default_group;
Must unique private String jobName;
private transient triggerkey triggerkey;
private transient jobdetail jobdetail;
Default public Dynamicjob () {} public dynamicjob (String jobName) {this.jobname = JobName; Public class<?
Extends job> target () {return target;
Public dynamicjob Target (class<. Extends job> target) {this.target = target;
return this;
Public Dynamicjob cronexpression (String cronexpression) {this.cronexpression = cronexpression;
return this;
Public String Jobgroup () {return jobgroup;
Public Dynamicjob Jobgroup (String jobgroup) {this.jobgroup = Jobgroup;
return this; } public StriNg JobName () {return jobName;
Public Dynamicjob jobName (String jobName) {this.jobname = JobName;
return this; Public Triggerkey Triggerkey () {if (Triggerkey = null) {Triggerkey = Triggerkey.triggerkey (t
His.jobname, This.jobgroup);
return triggerkey;
Public Jobdetail Jobdetail () {if (Jobdetail = null) {Jobdetail = Jobbuilder.newjob (target)
. withidentity (This.jobname, This.jobgroup). Build ();
return jobdetail; } * * Transfer data to Job * job * CONTEXT.GETMERGEDJOBDATAMAP (). Get (Key) */public Dyn
Amicjob Addjobdata (String key, Object value) {final Jobdetail detail = Jobdetail ();
Final Jobdatamap Jobdatamap = Detail.getjobdatamap ();
Jobdatamap.put (key, value);
return this; Public String cronexpression () {return This.cronExpression; }
}
With these two basic classes, the implementation of the dynamic job is as follows
1. Create Dynamicjob instance (Jobname,cronexpression and target are required; target is an implementation of the job class)
2). Call Dynamicschedulerfactory registerjob Register a job or call Removejob to delete a job that has been added
An example is as follows:
Dynamicjob dynamicjob = Createdynamicjob ();
Dynamicjob.addjobdata ("Mailguid", Mail.guid ());//transfer parameter
try {
Dynamicschedulerfactory.registerjob (dynamicjob);
} catch (Schedulerexception e) {
throw new illegalstateexception (e);
}
Note: About target in Dynamicjob.
This field is a class type that requires implementation to be a job implementation class. It's not specific.
To this, to complete;
I've put the code of the sample on Git, address: http://git.oschina.net/mkk/spring-dynamic-job everyone to discuss and study together.