spring的任務調度和事件監聽

1:spring的任務調度配置

@Service 
public class timeTasks1{ 
    public void execute() { 
          Thread t = new Thread(new Runnable(){  
              public void run(){  
              System.out.println(“執行任務”);  
            }});  
        t.start(); 
    } 
} 
 <context:component-scan base-package="com.test"/>
<task:scheduled-tasks>
  <task:scheduled ref="timeTasks1" method="execute" initial-delay="5000" fixed-delay="3600000"/>
  <task:scheduled ref="timeTasks2" method="exchange" cron="0 59 23 * * ?" />
</task:scheduled-tasks>

第一個任務表示程序啟動5s后調用timeTasks1類中的execute方法,然后每隔一個小時再調用execute一次
第三個任務表示每天的23點59分調用timeTasks2類中的exchange方法
更加全面的任務調度框架可以選擇quartz

2.spring 的事件監聽:

事件類 extends org.springframework.context.ApplicationEvent;
監聽類 implements org.springframework.context.ApplicationListener<ContextRefreshedEvent>
@EventListener事件監聽,定義事件監聽的Bean,

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,991評論 19 139
  • Spring Boot 參考指南 介紹 轉載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 46,970評論 6 342
  • Idea SpringMVC+Spring+MyBatis+Maven整合 創建項目 File-New Proje...
    mingli_jianshu1閱讀 1,677評論 2 15
  • spring官方文檔:http://docs.spring.io/spring/docs/current/spri...
    牛馬風情閱讀 1,731評論 0 3
  • 馬上春節又要到來了,今年已經是我人生的第二十二個春節,每年的春節都會有不一樣的地方但也有千篇一律的地方。 關于春節...
    魚小坤閱讀 344評論 0 1