Spring+mybatis+quartz Cluster

Source: Internet
Author: User

1. Defining a data source


<bean id= "DataSource" class= "Com.alibaba.druid.pool.DruidDataSource" init-method= "Init" destroy-method= "Close" >

<!--basic properties URL, user, password-->

<property name= "url" value= "${jdbc.url}"/>

<property name= "username" value= "${jdbc.user}"/>

<property name= "Password" value= "${jdbc.pwd}"/>

<!--configuration initialization size, minimum, maximum-->

<property name= "InitialSize" value= "${jdbc.initialpoolsize}"/>

<property name= "Minidle" value= "${jdbc.minpoolsize}"/>

<property name= "maxactive" value= "${jdbc.maxpoolsize}"/>

<!--Configure the time to get the connection wait timeout-->

<property name= "maxwait" value= "10000"/>

<!--How long does the configuration interval take to detect and detect idle connections that need to be closed, in milliseconds-->

<property name= "Timebetweenevictionrunsmillis" value= "60000"/>

<!--Configure a connection to the minimum surviving time in the pool, in milliseconds-->

<property name= "Minevictableidletimemillis" value= "300000"/>

<property name= "Validationquery" value= "select ' 1 '"/>

<property name= "Testwhileidle" value= "true"/>

<property name= "Testonborrow" value= "false"/>

<property name= "Testonreturn" value= "false"/>

<!--open Pscache and specify the size of Pscache on each connection-->

<property name= "Poolpreparedstatements" value= "true"/>

<property name= "maxpoolpreparedstatementperconnectionsize" value= "/>"

</bean>

2. Define Schedulerfactorybean

<bean name= "Schedulerfactorybean" class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean" >

<property name= "DataSource" >

<ref bean= "DataSource"/>

</property>

<!--spring-managed service injection-->

<propertyname= "Schedulercontextasmap" >

<map>

<entrykey= "MyService" value-ref= "MyService"/>

</map>

</property>

<property name= "Applicationcontextschedulercontextkey" value= "ApplicationContext"/>

<property name= "Quartzproperties" >

<props>

<prop key= "Org.quartz.scheduler.instanceName" >myJob</prop>

<prop key= "Org.quartz.scheduler.instanceId" >AUTO</prop>

<prop key= "Org.quartz.threadPool.class" >org.quartz.simpl.SimpleThreadPool</prop>

<prop key= "Org.quartz.threadPool.threadCount" >20</prop>

<prop key= "Org.quartz.threadPool.threadPriority" >5</prop>

<prop key= "Org.quartz.jobStore.misfireThreshold" >120000</prop>

<prop key= "Org.quartz.jobStore.class" >org.quartz.impl.jdbcjobstore.JobStoreTX</prop>

<prop key= "org.quartz.jobStore.isClustered" >true</prop>

<prop key= "Org.quartz.jobStore.clusterCheckinInterval" >15000</prop>

<prop key= "Org.quartz.jobStore.maxMisfiresToHandleAtATime" >1</prop>

<prop key= "Org.quartz.jobStore.tablePrefix" >QRTZ_</prop>

</props>

</property>

</bean>

3. Initialize Job task

public class Qrtzjobinit {


Private final Logger Logger = Loggerfactory.getlogger (This.getclass ());

@Autowired
Schedulerfactorybean Schedulerfactorybean;

@Autowired
Qrtzjoblogic qrtzjoblogic;

public void init () {
Scheduler Scheduler = Schedulerfactorybean.getscheduler ();
QRTZJOBPO cond = new QRTZJOBPO ();
Cond.setjobstatus (EnumJobState.NORMAL.getKey ());
try {
resultpolistbean<qrtzjobpo> Resultpolistbean = Qrtzjoblogic.queryqrtzjoblistbycond (cond);
if (resultpolistbean.getvalue () = null | | resultpolistbean.getvalue (). Size () = 0)
Return
For (Qrtzjobpo Job:resultPOListBean.getValue ()) {
AddJob (Job,scheduler);
}
if (Scheduler.isshutdown ())
Scheduler.startdelayed (20);
Logger.error ("Task load complete, size:{}", Resultpolistbean.getvalue (). Size ());
catch (Exception e) {
Logger.error ("anomaly:" +e);
}
}

private void AddJob (Qrtzjobpo job,scheduler Scheduler) {
try {
if (job = = null) {
Return
}
if (! Cronexpression.isvalidexpression (Job.getcronexpression ()) {
Logger.error ("Time expression error, jobname={},jobgroup={},cronexpression={}", Job.getjobname (), Job.getjobgroup (),
Job.getcronexpression ());
Return
}
Triggerkey Triggerkey = Triggerutils.gettriggerkey (Job);
Crontrigger trigger = (Crontrigger) scheduler.gettrigger (Triggerkey);
if (null = = Trigger) {
Jobdetail Jobdetail = jobutils.getjob (Job);
Trigger = Triggerutils.newtrigger (Job);
Scheduler.schedulejob (Jobdetail, Trigger);
} else {
Trigger =triggerutils.gettrigger (trigger, job);
Scheduler.reschedulejob (Triggerkey, Trigger);
}
catch (Schedulerexception e) {
Logger.error ("anomaly:" +e);
}
Return
}

4. Job Task execution

public class Qrtzjobfactory extends Basejob implements Job {


@Override
public void execute (jobexecutioncontext context) throws Jobexecutionexception {
try {
Schedulercontext Schedulercontext = Context.getscheduler (). GetContext ();
Jobdatamap map = Context.getmergedjobdatamap ();
set<string> keys = Map.keyset ();
for (String Key:keys) {
QRTZJOBPO schedulejob = (QRTZJOBPO) context.getmergedjobdatamap (). get (key);
Logger.info ("Perform task: {}, start.", Schedulejob.getjobname ());
Executor (Schedulejob,schedulercontext);
Logger.info ("Perform task: {}, complete.", Schedulejob.getjobname ());
}
Thread.Sleep (60000);
catch (Interruptedexception | Schedulerexception e) {
Logger.error ("anomaly:" +e);
}
}


}


Executor method to implement dynamic tasks using Spring's reflection tool


Reference articles

1. http://itindex.net/detail/53315-spring-quartz-%E7%AE%A1%E7%90%86

2. http://itindex.net/detail/52939-quartz-%E9%9B%86%E7%BE%A4-spring

3. http://www.cnblogs.com/nick-huang/p/4862732.html

4. https://nkcoder.github.io/blog/20140627/quartz-tutorial-job-jobdetail/


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.