1. 程式人生 > 實用技巧 >springmvc使用註解實現非阻塞定時器

springmvc使用註解實現非阻塞定時器

1、在Spring配置檔案中新增

xmlns:task="http://www.springframework.org/schema/task"


http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.2.xsd
<context:component-scan base-package="lct.conference.timer" />
<!-- 開啟註解排程支援 @Async非同步-->
<task:executor id="executor" pool-size="5" />
<task:annotation-driven executor="executor"/>

2、

package lct.conference.timer;

import lct.conference.util.PCMSLog;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; /** * @Author * @Date 2020/10/26 14:18 * @Version 1.0 * @Description 描述 */ @EnableAsync @Lazy(false)//避免spring懶載入導致無效 @Component public class FlightTrainTask { PCMSLog pcmsLog = PCMSLog.getlog(getClass()); @Async//開啟非同步,避免阻塞 @Scheduled(cron = "0/5 * * * * ? ") //
間隔5秒執行 public void taskCycle() throws InterruptedException { Thread.sleep(1000*10); pcmsLog.info("=======================使用SpringMVC框架配置定時任務"); } }