有些操作总是需要定时执行,如每5分钟执行一次,或者每晚23:00执行,或者每月的某天某时执行
主要分为配置文件和类
1.类
类中有定时任务操作的方法类
2.配置文件applicationContext-quartz.xml
配置文件主要是用于绑定任务,任务定时的作用
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
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
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"
>
<!-- 线程执行器配置,用于任务注册 -->
<bean id="executor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="10" />
<property name="maxPoolSize" value="100" />
<property name="queueCapacity" value="500" />
</bean>
<!-- 1.获取到相关任务类 -->
<bean id="TimingMegConfirmInductionTask" class="com.yl.oms.job.TimingMegConfirmInductionTask" />
<!-- 2.任务类中方法绑定 业务调度 -->
<bean id="TimingMegConfirmInduction" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="TimingMegConfirmInductionTask" /> <!-- 指向类的bean -->
<property name="targetMethod" value="excute" /> <!-- 定时任务方法 -->
</bean>
<!-- 3.业务调度 定时时间绑定 -->
<bean id="cronTimingMegConfirmInduction" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="TimingMegConfirmInduction" /> <!-- 业务调度bean-->
<property name="cronExpression" value="0 0 9,12 * * ?" /> <!--cron表达式代表任务调度执行时间 -->
</bean>
<!-- 4.设置调度 设置好调度便完成-->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTimingMegConfirmInduction" /> <!--设置定时时间的bean -->
</list>
</property>
<property name="taskExecutor" ref="executor" />
</bean>
<beans>
类准备就绪,配置文件搞定,即完成了一个定时任务