The recent project to use timed tasks, the previous practice is to use the Spring Quartz annotated form of the timing task to deal with, but after the implementation of the distributed environment, scheduled tasks need to avoid repeated execution, so the introduction of quartz distributed, roughly configured as follows:
Use the time note version, spring3.x above needs to use quartz2.x above,
Download quartz.2.1.7.tar.gz to the official website to download it
After extracting, locate the Tables_mysql.sql file under the Quartz-2.1.7\docs\dbtables path to import SQL into the database
Then import the Quartz.properties file
#============================================================================ # Configure Main Scheduler Properties #============================================================================ Org.quartz.scheduler.instanceName = Testscheduler org.quartz.scheduler.instanceId = AUTO Org.quartz.scheduler.skipUpdateCheck = True #=================== ========================================================= # Configure ThreadPool #================================
============================================ Org.quartz.threadPool.class = Org.quartz.simpl.SimpleThreadPool Org.quartz.threadPool.threadCount = org.quartz.threadPool.threadPriority = 5 #=================================== ========================================= # Configure Jobstore #================================================== ========================== Org.quartz.jobStore.misfireThreshold = 60000 #org. Quartz.jobStore.class = Org.quartz.simpl.RAMJobStore Org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStOretx Org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate Org.quartz.jobStore.dataSource = MyDS Org.quartz.jobStore.useProperties = False Org.quartz.jobStore.tablePrefix = Qrtz _ Org.quartz.jobStore.isClustered = True #========================================================================
= = = = Configure datasources #============================================================================
Org.quartz.dataSource.myDS.driver = com.mysql.jdbc.Driver Org.quartz.dataSource.myDS.URL = xxx
Org.quartz.dataSource.myDS.user = xxxx Org.quartz.dataSource.myDS.password =xxx
Org.quartz.dataSource.myDS.maxConnections = 15
To create a new scheduled task, you need to inherit Quartzjobbean,
Package Com.remair.hx.schedule.quarz;
Import Com.remair.hx.cache.RedisCache;
Import com.remair.hx.constants.RedisCacheRootKeyConstants;
Import Com.remair.hx.mapper.ConnectLogMapper;
Import Com.remair.hx.mapper.LiveInfoMapper;
Import Com.remair.hx.mapper.LiveUserMapper;
Import Com.remair.hx.mapper.UserMapper;
Import Com.remair.hx.model.LiveUserSchedule;
Import Com.remair.hx.thread.TimeOutUserThread;
Import Com.remair.hx.utils.DateUtil;
Import org.quartz.*;
Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.scheduling.quartz.QuartzJobBean;
Import org.springframework.stereotype.Component;
Import java.util.ArrayList;
Import Java.util.Date;
Import java.util.List;
/** * Created by Caige on 2016/6/10. */@Component @PersistJobDataAfterExecution @DisallowConcurrentExecution public class Heartschedulejob extends
Quartzjobbean {Logger Logger = Loggerfactory.getlogger (This.getclass ()); @Autowired Liveinfomapper Liveinfomapper;
@Override protected void executeinternal (Jobexecutioncontext jobexecutioncontext) throws Jobexecutionexception { Specific method logic}} Because quartz will maintain a set of containers on its own, spring's objects fail, such as those injected in the top class.
Liveinfomapper fails, so join a class to resolve this issue:
Package Com.remair.hx.schedule.quarz;
Import Org.quartz.spi.TriggerFiredBundle;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
Import org.springframework.scheduling.quartz.SpringBeanJobFactory;
/**
* Created by Caige on 2016/6/14.
*/Public
class Myjobfactory extends Springbeanjobfactory {
@Autowired
private Autowirecapablebeanfactory beanfactory;
@Override
Protected Object Createjobinstance (Triggerfiredbundle bundle) throws Exception {
object Jobinstance = Super.createjobinstance (bundle);
Beanfactory.autowirebean (jobinstance);
return jobinstance;
}
}
This class, when configured in Quartz.xml, introduces
<?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE beans Public "-//spring//dtd BEAN 2.0//en" "Http://www.springframework.org/dtd/spring-beans-2.0.dtd" > < beans> <bean name= "Startquertz" class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean" Lazy-init = "false" autowire= "no" > <property name= "configlocation" value= "Classpath:quartz.properties"/> & Lt;property name= "Jobdetails" > <list> <ref bean= "Heartdetail"/> < ;/list> </property> <property name= "triggers" > <list> < ; ref bean= "Hearttime"/> </list> </property> <property name= "Applicationcont Extschedulercontextkey "value=" contextconfiglocation "/>//This is used to avoid the injection of spring-managed objects <property Name=" Jobfactory "> <bean class=" com.remair.hx.schedule.quarz.MyJobFactory "/> ≪/property> </bean> <bean id= "Heartdetail" class= "Org.springframework.scheduling.quartz.JobDetailFac Torybean "> <property name=" jobclass "value=" Com.remair.hx.schedule.quarz.HeartScheduleJob "/> < Property name= "Durability" value= "true"/> <property name= "Requestsrecovery" value= "true"/> </bea n> <bean id= "Hearttime" class= "Org.springframework.scheduling.quartz.CronTriggerFactoryBean" > <PR Operty name= "Jobdetail" ref= "Heartdetail"/> <property name= "cronexpression" value= "0/30 * * * * *?" /> </bean> </beans>
Adding an quartz.xml to the Web. xml
<!--read Spring configuration file--
<context-param>
<param-name>contextconfiglocation</param-name >
<param-value>classpath:applicationContext.xml,classpath:quartz.xml</param-value>
< /context-param>
Because the writing is not good, so many points did not write, if have the same question welcome comment, I see will reply in time.