日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

任务调度及远端管理(基于Quartz.net)

發布時間:2023/11/29 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 任务调度及远端管理(基于Quartz.net) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這篇文章我們來了解一些項目中的一個很重要的功能:任務調度

可能有些同學還不了解這個,其實簡單點說任務調度與數據庫中的Job是很相似的東西

只不過是運行的物理位置與管理方式有點不一樣,從功能上來說我覺得還是差不多的,

存儲過程有很大的局限性,耦合性也太高,所以最好把系統的一些Job放在代碼層,

于是就有了Quartz.net,我們本篇就是針對Quartz.net的二次開發

?

一、新建HelloJob

HelloJob.cs,示例Job,每次執行都輸出msg變量中的信息

1 using Common.Logging; 2 using Quartz; 3 4 namespace Job.Items 5 { 6 public class HelloJob : IJob 7 { 8 public const string Message = "msg"; 9 private static readonly ILog log = LogManager.GetLogger(typeof(HelloJob)); 10 11 public virtual void Execute(IJobExecutionContext context) 12 { 13 var jobKey = context.JobDetail.Key; 14 var message = context.JobDetail.JobDataMap.GetString(Message); 15 log.InfoFormat("HelloJob: msg: {0}", message); 16 } 17 } 18 }

HelloJobExample.cs,每5秒執行一次

1 public class HelloJobExample 2 { 3 public virtual void Run() 4 { 5 ISchedulerFactory sf = new StdSchedulerFactory(); 6 IScheduler sched = sf.GetScheduler(); 7 8 IJobDetail job = JobBuilder.Create<HelloJob>() 9 .WithIdentity("job1", "group1") 10 .Build(); 11 12 JobDataMap map = job.JobDataMap; 13 map.Put("msg", "Your remotely added job has executed!"); 14 15 ITrigger trigger = TriggerBuilder.Create() 16 .WithIdentity("trigger1", "group1") 17 .ForJob(job.Key) 18 .WithCronSchedule("/5 * * ? * *") 19 .Build(); 20 21 sched.ScheduleJob(job, trigger); 22 sched.Start(); 23 } 24 }

好了,有效代碼就那么多,我們來試試

1 class Program 2 { 3 static void Main(string[] args) 4 { 5 var example = new HelloJobExample(); 6 example.Run(); 7 8 Console.ReadKey(); 9 } 10 }

貌似沒什么問題,如愿地執行了。

?

但是我們想想,實際運行中執行任務的服務器一般都是獨立出來的,那怎么去管理這些任務的開啟、關閉及暫停呢?

肯定不能每次手動去操作,那太麻煩了。我們的希望是在應用中(系統管理后臺)去管理這些任務。萬幸Quartz.net足夠強大,

他是支持遠程操作的,沒有太深入了解,不過看調用參數應該是通過TCP請求進行操作的,我們試試看

?

二、Job遠程管理

2.1、新建Job.Items項目,把之前新建的HelloJob.cs放在其中

2.2、新建Job.Server項目

新建RemoteServer.cs

1 public class RemoteServer : ILjrJob 2 { 3 public string Name 4 { 5 get { return GetType().Name; } 6 } 7 8 public virtual void Run() 9 { 10 ILog log = LogManager.GetLogger(typeof(RemoteServer)); 11 12 NameValueCollection properties = new NameValueCollection(); 13 properties["quartz.scheduler.instanceName"] = "RemoteServer"; 14 properties["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz"; 15 properties["quartz.threadPool.threadCount"] = "5"; 16 properties["quartz.threadPool.threadPriority"] = "Normal"; 17 properties["quartz.scheduler.exporter.type"] = "Quartz.Simpl.RemotingSchedulerExporter, Quartz"; 18 properties["quartz.scheduler.exporter.port"] = "555"; 19 properties["quartz.scheduler.exporter.bindName"] = "QuartzScheduler"; 20 properties["quartz.scheduler.exporter.channelType"] = "tcp"; 21 properties["quartz.scheduler.exporter.channelName"] = "httpQuartz"; 22 properties["quartz.scheduler.exporter.rejectRemoteRequests"] = "true"; 23 24 } 25 }

2.3、新建控制器HelloJobController

1 public class HelloJobController : Controller 2 { 3 public ActionResult Index() 4 { 5 try 6 { 7 if (HelloJobHelper.Trigger != null) 8 { 9 ViewBag.JobKey = "remotelyAddedJob"; 10 ViewBag.State = HelloJobHelper.Scheduler.GetTriggerState(HelloJobHelper.Trigger.Key); 11 ViewBag.StartTime = HelloJobHelper.Trigger.StartTimeUtc.ToString(); 12 } 13 else 14 { 15 ViewBag.State = "獲取Job執行狀態失敗"; 16 } 17 } 18 catch (Exception ex) 19 { 20 ViewBag.State = "Job服務器連接失敗"; 21 } 22 23 return View(); 24 } 25 public ActionResult Run() 26 { 27 HelloJobHelper.RunJob(); 28 29 return RedirectToAction("Index", "HelloJob"); 30 } 31 public ActionResult Pause() 32 { 33 HelloJobHelper.PauseJob(); 34 35 return RedirectToAction("Index", "HelloJob"); 36 } 37 public ActionResult Resume() 38 { 39 HelloJobHelper.ResumeJob(); 40 return RedirectToAction("Index", "HelloJob"); 41 } 42 }

2.4、新建HelloJobHelper

先配置連接遠端任務服務器的參數,這個要和上面的RemoteServer.cs對應

1 properties["quartz.scheduler.proxy"] = "true"; 2 properties["quartz.scheduler.proxy.address"] = "tcp://127.0.0.1:555/QuartzScheduler";

我們來看看開始操作,運行這個方法,任務服務器將自動開啟這個Job

1 public static void RunJob() 2 { 3 if (!scheduler.CheckExists(jobKey)) 4 { 5 IJobDetail job = JobBuilder.Create<HelloJob>() 6 .WithIdentity(jobKey) 7 .Build(); 8 9 JobDataMap map = job.JobDataMap; 10 map.Put("msg", "Your remotely added job has executed!"); 11 12 ITrigger trigger = TriggerBuilder.Create() 13 .WithIdentity(triggerKey) 14 .ForJob(job.Key) 15 .WithCronSchedule("/5 * * ? * *") 16 .Build(); 17 18 scheduler.ScheduleJob(job, trigger); 19 20 JobDetail = job; 21 Trigger = trigger; 22 } 23 }

暫停比較簡單

1 public static void PauseJob() 2 { 3 scheduler.PauseJob(jobKey); 4 }

2.5、View

1 @{ 2 ViewBag.Title = "Index"; 3 Layout = "~/Views/Shared/_Bootstrap.cshtml"; 4 } 5 6 <!DOCTYPE html> 7 8 <html> 9 <head> 10 <meta name="viewport" content="width=device-width" /> 11 <title>Index</title> 12 <style> 13 .col-sm-offset-2 { 14 margin-left:20px; 15 } 16 </style> 17 </head> 18 <body> 19 <br /> 20 @using (Html.BeginForm("Run", "HelloJob", null, FormMethod.Post, new { @id = "form1", @class = "form-horizontal", role = "form" })) 21 { 22 @Html.AntiForgeryToken() 23 <div class="form-group"> 24 <div class="col-sm-offset-2 col-sm-10"> 25 <input type="hidden" name="Id" id="Id" /> 26 <button type="submit" class="btn btn-default">Run</button> 27 </div> 28 </div> 29 } 30 31 @using (Html.BeginForm("Pause", "HelloJob", null, FormMethod.Post, new { @id = "form2", @class = "form-horizontal", role = "form" })) 32 { 33 @Html.AntiForgeryToken() 34 <div class="form-group"> 35 <div class="col-sm-offset-2 col-sm-10"> 36 <input type="hidden" name="Id" id="Id" /> 37 <button type="submit" class="btn btn-default">Pause</button> 38 </div> 39 </div> 40 } 41 42 @using (Html.BeginForm("Resume", "HelloJob", null, FormMethod.Post, new { @id = "form3", @class = "form-horizontal", role = "form" })) 43 { 44 @Html.AntiForgeryToken() 45 <div class="form-group"> 46 <div class="col-sm-offset-2 col-sm-10"> 47 <input type="hidden" name="Id" id="Id" /> 48 <button type="submit" class="btn btn-default">Resume</button> 49 </div> 50 </div> 51 } 52 53 <br /> 54 <div> 55 <ul> 56 <li>ViewBag.JobKey: @ViewBag.JobKey</li> 57 <li>ViewBag.State: @ViewBag.State</li> 58

轉載于:https://www.cnblogs.com/MuNet/p/6688064.html

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的任务调度及远端管理(基于Quartz.net)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。