基於Quartz.net的遠程任務管理系統 一

来源:https://www.cnblogs.com/COOLK/archive/2018/11/04/9905006.html
-Advertisement-
Play Games

在上一篇緒中,已經介紹了整個項目的情況下了,接下來就是開始一步步做起來了。 首先:先整個我們的Job任務表,以及Job執行日誌表。SQL如下: 1 drop table if exists job_info; 2 create table job_info 3 ( 4 id int not null ...


在上一篇緒中,已經介紹了整個項目的情況下了,接下來就是開始一步步做起來了。

首先:先整個我們的Job任務表,以及Job執行日誌表。SQL如下:

 1 drop table if exists job_info;
 2 create table job_info
 3 (
 4     id int not null AUTO_INCREMENT comment '主鍵',
 5     job_name varchar(150) null default null comment '任務名稱',
 6     job_assembly varchar(200) null default null comment '執行的方式的dll名稱',
 7     job_class varchar(300) null default null comment '執行的方法類',
 8     job_corn varchar(100) null default null comment '執行任務的corn表達式',
 9     job_type int not null default 1 comment '任務類型,預設為1 簡單,2 複雜',
10     job_execount int null default 0 comment '任務的執行總次數,0表示無限次',
11     job_starttime datetime null default null comment '任務開始時間',
12     job_state int null default 0 comment '任務的狀態 0 準備中,1 執行中,2 暫定,3 停止,4 結束',
13     addtime TIMESTAMP null default CURRENT_TIMESTAMP comment '任務的創建時間',
14     PRIMARY KEY (`id`)
15 );
16 drop table if exists job_log;
17 create table job_log
18 (
19     id int not null AUTO_INCREMENT comment '主鍵',
20     job_name varchar(150) null default null comment '任務名稱',
21     job_result varchar(200) null default null comment '執行的結果',
22     job_exception text null default null comment '執行任務的異常信息',
23     job_exetime int not null default 0 comment '執行耗時,單位ms',
24     job_exedate datetime null default 0 comment '任務的執行的日期時間',
25     job_exestate int null default 0 comment '執行結果 0 正常,1 異常',
26     addtime TIMESTAMP null default CURRENT_TIMESTAMP comment '任務的執行時間',
27     PRIMARY KEY (`id`)
28 );
View Code

相關實體類與操作方法,如下:

 1 /// <summary>
 2     /// Job信息表
 3     /// </summary>
 4     public class Job_Info : BaseEntity
 5     {
 6         private int _id = 0;
 7         private string _job_name;
 8         private string _job_assembly;
 9         private string _job_class;
10         private string _job_corn;
11         private int _job_type = 1;
12         private int _job_execount = 0;
13         private DateTime _job_starttime;
14         private int _job_state = 0;
15         private DateTime _addtime;
16 
17         /// <summary>
18         /// 主鍵
19         /// </summary>
20         public int Id { get => _id; set => _id = value; }
21         /// <summary>
22         /// 任務名稱
23         /// </summary>
24         public string Job_name { get => _job_name; set => _job_name = value; }
25         /// <summary>
26         /// 執行的方式的dll名稱
27         /// </summary>
28         public string Job_assembly { get => _job_assembly; set => _job_assembly = value; }
29         /// <summary>
30         /// 執行的方法類
31         /// </summary>
32         public string Job_class { get => _job_class; set => _job_class = value; }
33         /// <summary>
34         /// 執行任務的corn表達式
35         /// </summary>
36         public string Job_corn { get => _job_corn; set => _job_corn = value; }
37         /// <summary>
38         /// 任務類型,預設為1 簡單,2 複雜
39         /// </summary>
40         public int Job_type { get => _job_type; set => _job_type = value; }
41         /// <summary>
42         /// 任務的執行總次數,0表示無限次
43         /// </summary>
44         public int Job_execount { get => _job_execount; set => _job_execount = value; }
45         /// <summary>
46         /// 任務開始時間
47         /// </summary>
48         public DateTime Job_starttime { get => _job_starttime; set => _job_starttime = value; }
49         /// <summary>
50         /// 任務的狀態 0 準備中,1 執行中,2 暫定,3 停止,4 結束
51         /// </summary>
52         public int Job_state { get => _job_state; set => _job_state = value; }
53         /// <summary>
54         /// 任務的創建時間
55         /// </summary>
56         public DateTime Addtime { get => _addtime; set => _addtime = value; }
57         
58     }
View Code
/// <summary>
    /// Job運行日誌表
    /// </summary>
    public class Job_Log : BaseEntity
    {
        private int _id = 0;
        private string _job_name;
        private string _job_result;
        private string _job_exception;
        private int _job_exetime;
        private DateTime _job_exedate;
        private int _job_exestate;
        private DateTime _addtime;

        /// <summary>
        /// 主鍵
        /// </summary>
        public int Id { get => _id; set => _id = value; }
        /// <summary>
        /// 任務名稱
        /// </summary>
        public string Job_name { get => _job_name; set => _job_name = value; }
        /// <summary>
        /// 執行的結果
        /// </summary>
        public string Job_result { get => _job_result; set => _job_result = value; }
        /// <summary>
        /// 執行任務的異常信息
        /// </summary>
        public string Job_exception { get => _job_exception; set => _job_exception = value; }
        /// <summary>
        /// 執行耗時,單位ms
        /// </summary>
        public int Job_exetime { get => _job_exetime; set => _job_exetime = value; }
        /// <summary>
        /// 任務的執行的日期時間
        /// </summary>
        public DateTime Job_exedate { get => _job_exedate; set => _job_exedate = value; }
        /// <summary>
        /// 執行結果 0 正常,1 異常
        /// </summary>
        public int Job_exestate { get => _job_exestate; set => _job_exestate = value; }
        /// <summary>
        /// 任務的執行時間
        /// </summary>
        public DateTime Addtime { get => _addtime; set => _addtime = value; }
    }
View Code
  1  /// <summary>
  2     /// Job信息邏輯類
  3     /// </summary>
  4     public class JobInfoBLL
  5     {
  6         /// <summary>
  7         /// 添加Job信息
  8         /// </summary>
  9         /// <param name="jobInfo"></param>
 10         /// <returns></returns>
 11         public int AddInfo(Job_Info jobInfo)
 12         {
 13             int result = 0;
 14 
 15             if (jobInfo == null)
 16                 return result;
 17             try
 18             {
 19                 string SqlStr = string.Format("insert into job_info(job_name,job_assembly,job_class,job_corn,job_type,job_execount,job_starttime,job_state) values('{0}','{1}','{7}','{2}',{3},{4},'{5}',{6})",
 20                     jobInfo.Job_name, jobInfo.Job_assembly, jobInfo.Job_corn, jobInfo.Job_type, jobInfo.Job_execount, jobInfo.Job_starttime.ToString("yyyy-MM-dd HH:mm:ss"), 0, jobInfo.Job_class);
 21                 result = MySqlHelper.ExceuteSql(SqlStr);
 22             }
 23             finally
 24             {
 25 
 26             }
 27 
 28             return result;
 29         }
 30 
 31         /// <summary>
 32         /// 更新Job的狀態
 33         /// </summary>
 34         /// <param name="jobInfo"></param>
 35         /// <returns></returns>
 36         public int UpdateJobState(Job_Info jobInfo)
 37         {
 38             int result = 0;
 39 
 40             if (jobInfo == null)
 41                 return result;
 42             try
 43             {
 44                 string SqlStr = string.Format("update job_info set job_state={0} where ", jobInfo.Job_state);
 45                 if(jobInfo.Id > 0)
 46                 {
 47                     SqlStr += string.Format(" id={0};", jobInfo.Id);
 48                 }else if (!string.IsNullOrEmpty(jobInfo.Job_name))
 49                 {
 50                     SqlStr += string.Format(" job_name='{0}'", jobInfo.Job_name);
 51                 }
 52                 result = MySqlHelper.ExceuteSql(SqlStr);
 53             }
 54             finally
 55             {
 56 
 57             }
 58 
 59             return result;
 60         }
 61 
 62         /// <summary>
 63         /// job列表
 64         /// </summary>
 65         /// <returns></returns>
 66         public List<Job_Info> GetJobList()
 67         {
 68             List<Job_Info> list = null;
 69 
 70             try
 71             {
 72                 string SqlStr = "select * from job_info";
 73                 var ds = MySqlHelper.GetDataSet(SqlStr);
 74                 if(ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
 75                 {
 76                     list = new List<Job_Info>();
 77                     Job_Info info = new Job_Info();
 78                     foreach (System.Data.DataRow row in ds.Tables[0].Rows)
 79                     {
 80                         info = Job_Info.ToEntity<Job_Info>(row, info);
 81                         if (info != null)
 82                             list.Add(info);
 83                     }
 84                 }
 85             }
 86             finally
 87             {
 88 
 89             }
 90 
 91             return list;
 92         }
 93 
 94         /// <summary>
 95         /// job列表
 96         /// </summary>
 97         /// <returns></returns>
 98         public List<Job_Info> GetJobList(int pageIndex,int pageSize)
 99         {
100             List<Job_Info> list = null;
101 
102             try
103             {
104                 string SqlStr = "select * from job_info";
105                 SqlStr += string.Format(" limit {0},{1};", (pageIndex - 1) * pageSize, pageSize);
106                 var ds = MySqlHelper.GetDataSet(SqlStr);
107                 if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
108                 {
109                     list = new List<Job_Info>();
110                     Job_Info info = new Job_Info();
111                     foreach (System.Data.DataRow row in ds.Tables[0].Rows)
112                     {
113                         info = Job_Info.ToEntity<Job_Info>(row, info);
114                         if (info != null)
115                             list.Add(info);
116                     }
117                 }
118             }
119             finally
120             {
121 
122             }
123 
124             return list;
125         }
126 
127         public Job_Info GetOne(int id)
128         {
129             Job_Info job = null;
130 
131             try
132             {
133                 string SqlStr = "select * from job_info where id="+id;
134                 var ds = MySqlHelper.GetDataSet(SqlStr);
135                 if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
136                 {
137                     job = new Job_Info();
138                     foreach (System.Data.DataRow row in ds.Tables[0].Rows)
139                     {
140                         job = Job_Info.ToEntity<Job_Info>(row, job);
141                     }
142                 }
143             }
144             catch (Exception ex)
145             {
146 
147             }
148 
149             return job;
150         }
151 
152         /// <summary>
153         /// job數量
154         /// </summary>
155         /// <returns></returns>
156         public int GetJobCount()
157         {
158            int list = 0;
159 
160             try
161             {
162                 string SqlStr = "select count(0) from job_log";
163                 var ds = MySqlHelper.GetReader(SqlStr);
164                 if (ds != null)
165                 {
166                     int.TryParse(ds.ToString(), out list);
167                 }
168             }
169             finally
170             {
171 
172             }
173 
174             return list;
175         }
176 
177     }
View Code
/// <summary>
    /// Job 日誌邏輯類
    /// </summary>
    public class JobLogBLL
    {

        /// <summary>
        /// 添加Job日誌信息
        /// </summary>
        /// <param name="jobLog"></param>
        /// <returns></returns>
        public int AddLog(Job_Log jobLog)
        {
            int result = 0;

            if (jobLog == null)
                return result;
            try
            {
                string SqlStr = string.Format("insert into job_log(job_name,job_result,job_exception,job_exetime,job_exedate,job_exestate) values('{0}','{1}','{2}',{3},'{4}',{5})",
                    jobLog.Job_name, jobLog.Job_result, jobLog.Job_exception, jobLog.Job_exetime, jobLog.Job_exedate.ToString("yyyy-MM-dd HH:mm:ss"), jobLog.Job_exestate);
                result = MySqlHelper.ExceuteSql(SqlStr);
            }
            finally
            {

            }

            return result;
        }
        
        /// <summary>
        /// job日誌列表
        /// </summary>
        /// <returns></returns>
        public List<Job_Log> GetJobLogList()
        {
            List<Job_Log> list = null;

            try
            {
                string SqlStr = "select * from job_log";
                var ds = MySqlHelper.GetDataSet(SqlStr);
                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    list = new List<Job_Log>();
                    Job_Log info = new Job_Log();
                    foreach (System.Data.DataRow row in ds.Tables[0].Rows)
                    {
                        info = Job_Log.ToEntity<Job_Log>(row, info);
                        if (info != null)
                            list.Add(info);
                    }
                }
            }
            finally
            {

            }

            return list;
        }

        /// <summary>
        /// job日誌列表
        /// </summary>
        /// <returns></returns>
        public List<Job_Log> GetJobLogList(int pageIndex, int pageSize)
        {
            List<Job_Log> list = null;

            try
            {
                string SqlStr = "select * from job_log";
                SqlStr += string.Format(" limit {0},{1};", (pageIndex - 1) * pageSize, pageSize);
                var ds = MySqlHelper.GetDataSet(SqlStr);
                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    list = new List<Job_Log>();
                    Job_Log info = new Job_Log();
                    foreach (System.Data.DataRow row in ds.Tables[0].Rows)
                    {
                        info = Job_Log.ToEntity<Job_Log>(row, info);
                        if (info != null)
                            list.Add(info);
                    }
                }
            }
            finally
            {

            }

            return list;
        }

        public int GetJobLogCount(int state = -1)
        {
            int count = 0;

            try
            {
                string SqlStr = "select count(0) from job_log";
                if (state > -1)
                {
                    SqlStr += " where job_exestate=" + state;
                }
                var ds = MySqlHelper.GetReader(SqlStr);
                if (ds != null)
                {
                    int.TryParse(ds.ToString(), out count);
                }
            }
            finally
            {

            }

            return count;
        }

    }
View Code

這些都是資料庫的基本操作,我就直接貼出來了。為了規範,我們就定了一個介面,供所有Job任務來實際業務,介面很簡單,具體如下:

 /// <summary>
    /// 基Job
    /// </summary>
    public interface IJob
    {        
        /// <summary>
        /// 執行任務
        /// </summary>
        bool Exceute();
    }
View Code

好了,下麵我們就開始我們的Job托管器-Window Server。為了方便,我就使用Topshelf,因為創建JobManage.Service這個項目時,一定要選擇Console項目(我一開始就直接使用了Service項目,然後一直運行不起來)。

那現在來創建一個JobServer類,實現ServiceControl, ServiceSuspend 兩大介面的方法,在服務啟動的時候,實例化Schedule,然後加入檢查Job的Job任務,定期增加或刪除Schedule里的Job。

實現如下: 

public bool Start(HostControl hostControl)
        {
            if (JobConfig.factory == null)
            {
                JobConfig.factory = new StdSchedulerFactory();
                JobConfig.scheduler = JobConfig.factory.GetScheduler();
                JobConfig.scheduler.Start();

                //創建迴圈Job
                IJobDetail job = JobBuilder.Create<LoopJob>().WithIdentity("LoopJob", "group1").Build();
                ITrigger trigger = TriggerBuilder.Create()
                .WithIdentity("LoopJobtrigger	   

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 配置PHP開發環境的時候,當進行到在Apache的httpd.conf文件中配置載入PHP模塊時發生如下錯誤 httpd: Syntax error on line 185 of D:/wamp/Apache24/conf/httpd.conf: Cannot load D:/wamp/php 5. ...
  • Like music and movies, video games are rapidly becoming an integral part of our lives. Over the years, youve yearned for every new gaming console, mas ...
  • 1.arrary_merge 示例代碼: 運行上面的代碼,輸出結果如下圖所示: 普通數組合併時,會把第二個數組放到第一個數組後面,拼接後返回。 但是對於鍵值對的數組來說,如果有相同的鍵,那麼第二個數組會覆蓋第一個數組相同的鍵所對應的值。 2.通過 合併 示例代碼: 運行上面的代碼,輸出結果如下圖所示 ...
  • datetimepicker造成的問題,年、月和日參數描述無法表示的 DateTime ...
  • 原則的誕生:面向對象:封裝、繼承、多態三大支柱蘊含了用抽象來封裝變化,降低耦合,實現復用的精髓; 封裝:隱藏內部的實現,保護內部信息; 繼承:實現復用,歸納共性; 多態:改寫對象行為,實現更高級別的繼承 要實現這些目的,就必須遵守一些原則:封裝變化、對介面編程、少繼承多聚合 實現系統的可擴展、可復用 ...
  • FormsAuthentication.SetAuthCookie(UserFlag, createPersistentCookie); createPersistentCookie是否永久保存cookie https://www.cnblogs.com/joeylee/p/3521131.html ...
  • 一、線程開銷 操作系統創建線程是有代價的,其主要開銷在下麵列舉出來了。 記憶體開銷 1. 線程內核對象 擁有線程描述屬性與線程上下文,線程上下文占用的記憶體空間為 x86 架構 占用 700 位元組、x64 架構 1240 位元組 、ARM 架構 350 位元組。 2. 線程環境塊(TEB) TEB 消耗一個 ...
  • 一、什麼是運行時序列化 序列化的作用就是將對象圖(特定時間點的對象連接圖)轉換為位元組流,這樣這些對象圖就可以在文件系統/網路進行傳輸。 二、序列化/反序列化快速入門 一般來說我們通過 FCL 提供的 對象就可以將一個對象序列化為位元組流進行存儲,或者通過該 Formatter 將一個位元組流反序列化為一 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...