SpringMVC 定时任务的配置

xiaoxiao2021-02-28  105

SpringMVC 定时任务的配置有两种

第一种:

   使用 @Scheduled(cron="0 0 1 * * ?") 注解

  同时在配置文件中引入

        xmlns:task="http://www.springframework.org/schema/task" http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">  <!-- 计划任务配置,用 @Service @Lazy(false)标注类,用@Scheduled(cron = "0 0 1 * * ?")标注方法 -->        <task:executor id="executor" pool-size="10"/> <task:scheduler id="scheduler" pool-size="10"/>       <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>

    cron="0 0 1 * * ?"  表示定时执行的时间(每天凌晨1点)

第二种:

 使用配置文件的方式

<!-- 定义每天凌晨执行调度任务 -->

首先配置定时任务  <bean id="doTime2" class="org.springframework.scheduling.quartz.CronTriggerBean">  <property name="jobDetail">  <ref bean="dosettaskTime" />    </property>    cron表达式 <property name="cronExpression">  <value>0 0 1 * * ?</value>  </property> </bean>   <bean id="dosettaskTime"  class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">    调用的类 <property name="targetObject">    <bean class="com.sxs.controller.TasktimerController" />  </property>    调用类中的方法 <property name="targetMethod">    <value>taskSetEveryDay</value>    </property>   <property name="concurrent" value="false" />  </bean>  

然后调用,配置的定时任务都放在list里循环。

<!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 --> <bean id="startQuertz" lazy-init="true" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="doTime2"/>                         </list> </property> </bean>

转载请注明原文地址: https://www.6miu.com/read-22112.html

最新回复(0)