開源一個windows下的定時任務架構,簡單粗暴好用。

來源:互聯網
上載者:User

標籤:

這裡是你想要的功能:
  1. 支援外掛程式,將你要執行的任務編譯成程式集放到架構的根目錄下,再進行簡單的配置就行了。
  2. 支援Corn運算式。想讓任務在什麼時候執行就在什麼時候執行。
  3. 支援安裝成windows service (通過topshelf實現)
  4. 保證任務在執行階段,就算到了下一個觸發時間,不重複執行(如果本次還沒執行完的話)。
  5. 還有…等我想到了再實現^o^,您也可以關注項目的地址:https://github.com/leleroyn/Windows-TaskSchedule,提出你想要的功能。
簡單介紹下項目的使用:編寫外掛程式支援需要實現架構中的Ijob介面,架構中內建了一個簡單的不能再簡單的樣本任務。直接看代碼:  IJob
   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:   
   6:  namespace Windows.TaskSchedule
   7:  {
   8:      public interface IJob
   9:      {
  10:          void Init();
  11:          void Excute();
  12:          void OnError(Exception ex);        
  13:      }
  14:  }

 DemoJob

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:   
   6:  namespace Windows.TaskSchedule
   7:  {
   8:      public class DemoJob:IJob
   9:      {
  10:          static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(DemoJob));
  11:          DateTime date = new DateTime();
  12:          public void Init()
  13:          {
  14:              date = DateTime.Now;
  15:          }
  16:   
  17:          public void Excute()
  18:          {
  19:              logger.Debug(date);           
  20:          }
  21:   
  22:          public void OnError(Exception ex)
  23:          {
  24:              logger.Debug(ex.ToString());
  25:          }       
  26:      }
  27:  }
配置外掛程式到架構中執行通過修改configs目錄下的Jobs.config檔案,讓架構識別你的任務。

Jobs.config

   1:  <?xml version="1.0" encoding="utf-8" ?>
   2:  <Jobs serverName="demo-server" displayName="測試服務" description="測試服務的描述">
   3:    <Job name="demoJob1" type="Windows.TaskSchedule.DemoJob,Windows.TaskSchedule" cornExpress="0/3 * * * * ?"  />
   4:  </Jobs>
參數說明:

serverName:發布成服務時的服務名稱(最好不要有空格)

displayName:發布成服務時的服務顯示名稱

description:發布成服務時的服務描述

Job節點就是各個任務的具體配置了,name:任務的名稱(最好唯一),type:外掛程式的具體執行個體與程式集,cornExpress:任務執行的時間,只支援corn運算式

 

執行任務

如果是開發調試階段,可以直接啟動Windows.TaskSchedule.exe 這樣可以直接在控制台輸出相關資訊。

如果要部署成windows服務,需要在命令列下 先執行Windows.TaskSchedule.exe install  進行安裝,然後啟動:Windows.TaskSchedule.exe start

卸載windows服務也很簡單,直接執行 Windows.TaskSchedule.exe uninstall   具體可參考topshelf組件的使用方法。http://topshelf-project.com/

 

想要源碼,如果你細心的話在文章的開頭就應該有啦,我還是貼一下地址吧:https://github.com/leleroyn/Windows-TaskSchedule

申明一下,此項目主要是藉助一些第三方的組件,有疑問或者建議可以在這或者github裡給我留言,項目實際過程中出現任何問題概不負責^o^.

開源一個windows下的定時任務架構,簡單粗暴好用。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.