當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Springboot 简单的定时器
生活随笔
收集整理的這篇文章主要介紹了
Springboot 简单的定时器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
springboot 相對于其他而言,就是化繁為簡,能用注解完成的,絕不用xml。
定時器 也不例外!
首先,在啟動類上打開 定時器的總開關
@SpringBootApplication //開啟緩存功能 @EnableCaching //定時器總開關 @EnableScheduling public class ShiroApplication {public static void main(String[] args) {SpringApplication.run(ShiroApplication.class, args);}}然后在 定時任務 類加上?
@Component 和 @Scheduled(cron = "0 0/5 * * * ?") //表示每5分鐘執行一次 @Component public class Timer {@AutowiredJedisPool jedisPool;@Scheduled(cron = "0 0/5 * * * ?")public void deleteFromCache() {Jedis jedis =null;jedis =jedisPool.getResource();jedis.del("users::userCache");System.err.println("users::userCache.從緩存中刪除.");System.out.println("執行了Timer,時間為:" + new Date(System.currentTimeMillis()));} } 2018-08-09 09:14:36,865 DEBUG OrderedRequestContextFilter:104 - Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@4b251260 users::userCache.從緩存中刪除. 執行了Timer,時間為:Thu Aug 09 09:15:00 CST 2018 users::userCache.從緩存中刪除. 執行了Timer,時間為:Thu Aug 09 09:20:00 CST 2018 執行了Timer,時間為:Thu Aug 09 09:25:00 CST 2018 users::userCache.從緩存中刪除. users::userCache.從緩存中刪除. 執行了Timer,時間為:Thu Aug 09 09:30:00 CST 2018 執行了Timer,時間為:Thu Aug 09 09:35:00 CST 2018 users::userCache.從緩存中刪除. 執行了Timer,時間為:Thu Aug 09 09:40:00 CST 2018 users::userCache.從緩存中刪除.?
轉載于:https://www.cnblogs.com/wang-qiang/p/9470529.html
總結
以上是生活随笔為你收集整理的Springboot 简单的定时器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: BZOJ4004: [JLOI2015]
- 下一篇: 基础知识:什么是ASP.NET Razo