定时任务 - quartz
1)maven 依赖
<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.1.5.RELEASE</version> </dependency>
2)spring 配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <!-- 任务调度工厂类 --> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="quartzProperties"> <props> <prop key="org.quartz.threadPool.class">org.quartz.simpl.SimpleThreadPool</prop> <prop key="org.quartz.threadPool.threadCount">10</prop> <prop key="org.quartz.threadPool.threadPriority">5</prop> <prop key="org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread">true</prop> </props> </property> <property name="triggers"> <list> <ref bean="scaQuartzSchedulerTaskTrigger"/> </list> </property> <property name="applicationContextSchedulerContextKey" value="applicationContext"/> </bean> <!-- 统计面积的触发器 --> <bean id="scaQuartzSchedulerTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="scaQuartzSchedulerTask"/> <property name="cronExpression"> <!-- 配置参考 http://www.cnblogs.com/sunjie9606/archive/2012/03/15/2397626.html --> <value>0 0 23 * * ?</value> <!-- 每天夜间23点执行一次 --> </property> </bean> <bean id="scaQuartzSchedulerTask" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> <property name="jobClass" value="info.shagri.vpst.web.quartz.StatisComAreasTask"/> </bean> </beans>
3)jobClass
package info.shagri.vpst.web.quartz; import info.shagri.vpst.service.jcsj.CompanyService; import org.quartz.JobExecutionContext; import org.springframework.scheduling.quartz.QuartzJobBean; import com.framework.web.utils.ApplicationContextHolder; /** * 统计所有有效公司各项面积 * */ public class StatisComAreasTask extends QuartzJobBean { protected void executeInternal(JobExecutionContext jobExecutionContext) { CompanyService service = ApplicationContextHolder.getBean("companyService"); service.doStatisThenStoreCompanyAreas(); } }