quartz包含4个核心模块:
调度器scheduled---触发器trigger---jobDetail---job。
调度关系为:(偷懒盗用的图片,简洁明了。而且quartz非常简单)
1、springmvc下配置:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd"> <!-- Spring scheduled定时器 --> <task:annotation-driven/> <context:component-scan base-package="com.mst.quartz" /> <!-- 启动触发器的配置开始 --> <!-- quartz-2.x的配置 --> <!-- 设置调度 --> <bean lazy-init="false" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="myJobTrigger" /> <ref bean="scanRecommend" /> <!-- <ref bean="securitiesData" /> --> </list> </property> </bean> <!-- quartz-2.x的配置 --> <bean id="myJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail"> <ref bean="myJobDetail" /> </property> <property name="cronExpression"> <value>0 0 0 * * ?</value> </property> </bean> <!-- job的配置开始 --> <bean id="myJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="ProjectQuartz" /> <property name="targetMethod" value="updateProjectStaute" /> </bean> <!-- 扫描推荐收益数据 --> <!-- scanRecommend的时间配置,定义在每个月的1号凌晨3点开始 --> <bean id="scanRecommend" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail"> <ref bean="scanRecommendDetail" /> </property> <property name="cronExpression"> <value>0 0 3 1 * ?</value> </property> </bean> <!-- scanRecommend的配置开始job --> <bean id="scanRecommendDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="ProjectQuartz" /> <property name="targetMethod" value="scanRecommendIncome" /> </bean> </beans>
说明:1、启动调度器并启用自动扫描对应的job包路径;2、触发器中包含"myJobTrigger、"scanReconmmend"两个jobDetail任务描述(描述中指定对应的job并设置cron表达式);3、设置job指定具体任务类和方法;
2、springboot下的配置:
说明:引入对应的包:spring-boot-starter-web包(此包包含spring-context,而schedule在context包下)。1、创建对应的task定时任务类和方法,方法中调用对应的job如下。2、若是不执行类请加注解@Configurable和@EnableScheduling;3、就这没有了。所以boot是真的简单(注意类中的注解)
@Component public class ServiceTask { private Logger logger = Logger.getLogger(getClass()); @Resource CreatePrice createPrice; /** * 每日上午9点半生成开盘价格 */ @Scheduled(cron="0 30 9 * * ?") public void createFristPriceTask() { logger.info("########################生成开盘价start#######################"); createPrice.createFristPrice(); logger.info("########################生成开盘价数据end#######################"); }
//cron.expression.math.first 在对应的application.properties中加入对应的指定即可。如: //cron.expression.math.first=0/1 30-59 09 * * MON-FRI @Scheduled(cron="${cron.expression.math.first}") public void entrustListTask1() { logger.info("########################委托队列任务start#######################"); bondEntrustService.entrustListTask(); bondDealService.tradeBondDeal(); logger.info("########################委托队列任务end#######################"); }
}