博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
任务调度及远端管理(基于Quartz.net)
阅读量:6251 次
发布时间:2019-06-22

本文共 5573 字,大约阅读时间需要 18 分钟。

这篇文章我们来了解一些项目中的一个很重要的功能:任务调度

可能有些同学还不了解这个,其实简单点说任务调度与数据库中的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
() 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
() 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  7 8  9  10 
11 Index 12 17 18 19
20 @using (Html.BeginForm("Run", "HelloJob", null, FormMethod.Post, new { @id = "form1", @class = "form-horizontal", role = "form" })) 21 { 22 @Html.AntiForgeryToken() 23
24
25
26
27
28
29 } 30 31 @using (Html.BeginForm("Pause", "HelloJob", null, FormMethod.Post, new { @id = "form2", @class = "form-horizontal", role = "form" })) 32 { 33 @Html.AntiForgeryToken() 34
35
36
37
38
39
40 } 41 42 @using (Html.BeginForm("Resume", "HelloJob", null, FormMethod.Post, new { @id = "form3", @class = "form-horizontal", role = "form" })) 43 { 44 @Html.AntiForgeryToken() 45
46
47
48
49
50
51 } 52 53
54
55
    56
  • ViewBag.JobKey: @ViewBag.JobKey
  • 57
  • ViewBag.State: @ViewBag.State
  • 58

转载于:https://www.cnblogs.com/MuNet/p/6688064.html

你可能感兴趣的文章
win7读取linux硬盘序列号,Windows 下获取硬盘序列号
查看>>
linux音频设备接口,OSS--跨平台的音频接口简介
查看>>
华为网卡linux驱动安装,Linux Nvidia显卡驱动安装
查看>>
linux sql撤销,取消请求的sql语句
查看>>
c语言学习 二维指针,二维数组和指针(C语言)
查看>>
图像压缩算法构造最优解c语言,C语言与程序设计第12章递归.ppt
查看>>
c语言飞机源代码,C语言写的飞机源码
查看>>
C语言 如果某个数大于10 归零,C:当指针实际指向某个东西时,函数继续接收归零指针(示例代码)...
查看>>
c c 语言项目实战 pdf,[计算机]C实战项目.pdf
查看>>
linux中solr创建core,Solr6.6 创建core
查看>>
android的边框阴影,android 自定义shape 带阴影边框效果
查看>>
android centos 的编码,Centos 安装 android sdk
查看>>
反编译android 状态栏沉浸,手把手教你傻瓜式开启状态栏沉浸模式
查看>>
android l job scheduler api,Android JobScheduler API
查看>>
html css 扑克牌桌面,纯CSS实现画扑克牌
查看>>
html5资源分享,12款实用的HTML5干货分享
查看>>
n9 android模拟器,Android软件将兼容诺基亚N9
查看>>
html防替换资源,html前端进行资源重载及刷新资源
查看>>
html5css做星星的形状,css评分效果的星星示例技术分享
查看>>
html实现放大镜效果,利用jquery实现放大镜特效
查看>>