quartz定时任务实现只执行一次,以后不再执行

时间:2021-03-29 08:02:12

按说只在tomcat启动时执行一次,我们可以用监听器来实现,但是有些开发框架中因特殊场景不适合用监听器。那么定时任务也可以实现只执行一次的操作。配置如下

好使的话大家给个赞

<?xml version="1.0" encoding="UTF-8"?> <beans  xmlns="http://www.springframework.org/schema/beans"  xmlns:mvc="http://www.springframework.org/schema/mvc"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:context="http://www.springframework.org/schema/context"  xmlns:tx="http://www.springframework.org/schema/tx"  xmlns:util="http://www.springframework.org/schema/util"  xsi:schemaLocation="  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">       <bean id="publicWebServiceJob" class="cn.org.site.business.quartz.PublicWebServiceScheduleJob"/>  <bean id="publicWebServiceJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  <property name="targetObject" ref="publicWebServiceJob" />  <property name="targetMethod" value="doJob"></property>  <!--默认允许并发执行,设置为false 防止并发执行 发生死锁问题 -->  <property name="concurrent" value="false"/>   </bean>    <!-- quartz一分钟执行一次的实现方式 -->  <!-- <bean id="publicWebServiceTriggerBean" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">  <property name="jobDetail" ref="publicWebServiceJobDetail"></property>  <property name="cronExpression" value="0 */1 * * * ?"></property>  <property name="description" value="定时"></property>  </bean>-->    <!-- quartz实例化5秒后执行一次job的方式 -->  <bean id="publicWebServiceTriggerBean" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">  <property name="jobDetail" ref="publicWebServiceJobDetail" />  <property name="startDelay" value="5000" /> <!-- 启动延迟 单位/毫秒-->   <property name="repeatCount" value="0" /> <!-- 重复次数 -->  </bean>   <!-- 触发器-->  <bean id="schedulerFactoryBean-em" lazy-init="false" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  <property name="triggers">  <list>    <ref bean="publicWebServiceTriggerBean"/>  </list>  </property>  </bean> </beans>