C# windows服務+Quartz.net 3.0.7 實現任務調度系統思路分析

一:首先構建Web端具體實現在這裏就不寫了,簡單的截個圖給觀看的朋友看一下,此處截圖也是從他人的博客中截取出來的哈哈,已經懶的像一條鹹魚!!

Web端主要是用於配置具體的節點信息。可以把信息儲存到相關的關係型數據庫中。主要需要注意的就是上傳的附件爲生成的引用包DLL文件。

主要使用框架反射方式

1:當我們寫job時同一實現這個框架接口,類庫形式。

2:寫完後編譯成DLL,上傳到我們的作業執行節點。

3:在執行節點中,通過反射拿到DLL的job信息。

4:然後構建quartz的job,添加到scheduler。

這種方式缺點: 耦合性太高,開發量較大。 優點:集中式管理。

還有好多實現方式在此處就不一一敘述了。

然後我們創建一個windowservice服務,作爲基點。

以下是簡單的案例沒有實際意義:

    /// <summary>
        /// 任務運行池
        /// </summary>
        private static Dictionary<string, string> TaskRuntimePool = new Dictionary<string, string>();
        private static IScheduler scheduler = null;
        static void Main(string[] args)
        {
            //創建調度器工廠
            ISchedulerFactory factory = new StdSchedulerFactory();
            //使用工廠生成一個調度器
            scheduler = factory.GetScheduler().Result;
            scheduler.Start();
            ThreadingTimer();
            Console.ReadLine();
        }
        private static void ThreadingTimer()
        {
            using (var t1 = new Timer(Run, null, TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(60)))
            {
                Task.Delay(150000).Wait();
            }
        }
        private static void Run(object state)
        {
            var dt = DbService.Instance.Ado.GetDataTable("SELECT * FROM dbo.tb_version");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string tempFile = AppDomain.CurrentDomain.BaseDirectory + "App_Temp\\" + dt.Rows[i][1] + "\\" + dt.Rows[i][5];
                string filePath = AppDomain.CurrentDomain.BaseDirectory + "App_Data\\" + dt.Rows[i][1];
                if (!Directory.Exists(filePath)) Directory.CreateDirectory(filePath);
                if (!File.Exists(tempFile)) File.Create(tempFile);
                //取得數據庫的二進制
                byte[] buffer = (byte[])dt.Rows[i]["zipfile"];
                File.WriteAllBytes(tempFile, buffer);
                CompressHelper.UnCompress(tempFile, filePath);
                //加載程序及路徑
                Assembly ass = Assembly.LoadFrom(filePath + "\\TestOneJob.dll");
                if (ass != null)
                {
                    Type[] types = ass.GetExportedTypes();
                    foreach (Type t in types)
                    {
                        if (typeof(IJob).IsAssignableFrom(t))
                        {
                            IJobDetail jobDetail = JobBuilder.Create(t).WithIdentity("CaseHandingJob", "CaseHandingJobGroup").Build();
                            //新建一個觸發器
                            ITrigger trigger = TriggerBuilder.Create()
                                .StartNow()
                                .WithCronSchedule("0/7 * * * * ?").Build();
                            //任務和觸發器關聯放入調度器
                            scheduler.ScheduleJob(jobDetail, trigger);
                            break;
                        }
                    }
                }
            }
        }

通過解析程序集,實時監控web端配置的節點信息,以上只是簡寫,許多地方都需要完善,僅供參考。

具體的安裝服務步驟,再次就不敘說了,網上一大把,將本人使用安裝方式與大家分享一下:

string startPath = ConfigurationManager.AppSettings["installToolPath"] + ConfigurationManager.AppSettings["InstallPath"];
                    Process p = new Process();
                    p.StartInfo.FileName = "cmd.exe";
                    p.StartInfo.UseShellExecute = false;
                    p.StartInfo.RedirectStandardInput = true;
                    p.StartInfo.RedirectStandardOutput = true;
                    p.StartInfo.RedirectStandardError = true;
                    p.StartInfo.CreateNoWindow = true;
                    p.Start();
                    p.StandardInput.AutoFlush = true;
                    p.StandardInput.WriteLine(startPath);
                    p.StandardInput.WriteLine("exit");
                    string strRst = p.StandardOutput.ReadToEnd();
                    p.WaitForExit();
                    p.Close();

然後就是windows服務定時跑,去查詢數據庫或緩存服務器中的數據進行觸發。

程序集的案例如下:

namespace TestOneJob
{
    public class RunningCommon
    {
        public void Run(IScheduler scheduler)
        {
            //創建任務
            IJobDetail job = JobBuilder.Create<JobOne>()
                .WithIdentity("CaseHandingJob", "CaseHandingJobGroup")
                .Build();
            //新建一個觸發器
            ITrigger trigger = TriggerBuilder.Create()
                .StartNow()
                .WithCronSchedule("0/7 * * * * ?").Build();
            //任務和觸發器關聯放入調度器
            scheduler.ScheduleJob(job, trigger);
        }
    }

namespace TestOneJob
{
    public class JobOne : IJob
    {
        public async Task Execute(IJobExecutionContext context)
        {
            await Console.Out.WriteLineAsync("這是我的第一個程序集作業");
        }
    }
}

將這些類文件生成DLL引用放到,windows服務觸發的指定目錄下就可以觸發了。描述的可能十分亂,就這樣吧!,語言表達能力有限

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章