日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring定时器,定时器一次执行两次的问题

發布時間:2023/12/10 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring定时器,定时器一次执行两次的问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Spring 定時器

方法一:注解形式

配置文件頭加上如下:

xmlns:task="http://www.springframework.org/schema/task"http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd 需要 quartz 包 <dependency><groupId>org.quartz-scheduler</groupId><artifactId>quartz</artifactId><version>2.2.2</version> </dependency>

?

<!-- 掃描包 --> <context:component-scan base-package="com.hehe.content.job" /><!-- 啟動定時任務 -->
<task:annotation-driven/>

?

@Component public class MyTask {@Scheduled(cron="0 0 2 * * ?") // 每天凌晨2點執行,該方法不能有返回值public void taskCycle(){ System.out.println("======================"); } }

?

方法二: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:p="http://www.springframework.org/schema/p"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-4.1.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.1.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"><!-- 啟動定時任務 --><!-- <task:annotation-driven/> <context:component-scan base-package="com.hehe.content.job" /> --><!-- 要調用的工作類 --> <bean id="quartzJob" class="com.hehe.content.job.MyTask"></bean> <!-- 定義調用對象和調用對象的方法 --> <bean id="jobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- 調用的類 --> <property name="targetObject"> <ref bean="quartzJob" /> </property> <!-- 調用類中的方法 --> <property name="targetMethod"> <value>taskCycle</value> </property> </bean> <!-- 定義觸發時間 --> <bean id="doTime" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail"> <ref bean="jobtask" /> </property> <!-- cron表達式 --> <property name="cronExpression"> <value>0 0 2 * * ?</value> </property> </bean> <!-- 總管理類 如果將lazy-init='false'那么容器啟動就會執行調度程序 --> <bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="doTime" /> </list> </property> </bean> </beans>

問題: 每次任務到點都執行兩次!!!!!!

網上查了好多資料 ,都不是我的情況,后來發現是我的項目在啟動的時候每次都會加載兩次,原來是eclipse 中tomcat配置的問題

圖中若選擇的是第二個,項目會啟動兩次,這就導致了后面的定時器執行了兩次。最后改為了第一選項就好了。

?

轉載于:https://www.cnblogs.com/c9999/p/6171062.html

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的spring定时器,定时器一次执行两次的问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。