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

可能有些同学还不了解这个,其实简单点说任务调度与数据库中的Job是很相似的东西

只不过是运行的物理位置与管理方式有点不一样,从功能上来说我觉得还是差不多的,

存储过程有很大的局限性,耦合性也太高,所以最好把系统的一些Job放在代码层,

于是就有了Quartz.net,我们本篇就是针对Quartz.net的二次开发

一、新建HelloJob

HelloJob.cs,示例Job,每次执行都输出msg变量中的信息

 using Common.Logging;
using Quartz; namespace Job.Items
{
public class HelloJob : IJob
{
public const string Message = "msg";
private static readonly ILog log = LogManager.GetLogger(typeof(HelloJob)); public virtual void Execute(IJobExecutionContext context)
{
var jobKey = context.JobDetail.Key;
var message = context.JobDetail.JobDataMap.GetString(Message);
log.InfoFormat("HelloJob: msg: {0}", message);
}
}
}

HelloJobExample.cs,每5秒执行一次

         public class HelloJobExample
{
public virtual void Run()
{
ISchedulerFactory sf = new StdSchedulerFactory();
IScheduler sched = sf.GetScheduler(); IJobDetail job = JobBuilder.Create<HelloJob>()
.WithIdentity("job1", "group1")
.Build(); JobDataMap map = job.JobDataMap;
map.Put("msg", "Your remotely added job has executed!"); ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
.ForJob(job.Key)
.WithCronSchedule("/5 * * ? * *")
.Build(); sched.ScheduleJob(job, trigger);
sched.Start();
}
}

好了,有效代码就那么多,我们来试试

     class Program
{
static void Main(string[] args)
{
var example = new HelloJobExample();
example.Run(); Console.ReadKey();
}
}

貌似没什么问题,如愿地执行了。

但是我们想想,实际运行中执行任务的服务器一般都是独立出来的,那怎么去管理这些任务的开启、关闭及暂停呢?

肯定不能每次手动去操作,那太麻烦了。我们的希望是在应用中(系统管理后台)去管理这些任务。万幸Quartz.net足够强大,

他是支持远程操作的,没有太深入了解,不过看调用参数应该是通过TCP请求进行操作的,我们试试看

二、Job远程管理

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

2.2、新建Job.Server项目

新建RemoteServer.cs

     public class RemoteServer : ILjrJob
{
public string Name
{
get { return GetType().Name; }
} public virtual void Run()
{
ILog log = LogManager.GetLogger(typeof(RemoteServer)); NameValueCollection properties = new NameValueCollection();
properties["quartz.scheduler.instanceName"] = "RemoteServer";
properties["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz";
properties["quartz.threadPool.threadCount"] = "";
properties["quartz.threadPool.threadPriority"] = "Normal";
properties["quartz.scheduler.exporter.type"] = "Quartz.Simpl.RemotingSchedulerExporter, Quartz";
properties["quartz.scheduler.exporter.port"] = "";
properties["quartz.scheduler.exporter.bindName"] = "QuartzScheduler";
properties["quartz.scheduler.exporter.channelType"] = "tcp";
properties["quartz.scheduler.exporter.channelName"] = "httpQuartz";
properties["quartz.scheduler.exporter.rejectRemoteRequests"] = "true";
}
}

2.3、新建控制器HelloJobController

     public class HelloJobController : Controller
{
public ActionResult Index()
{
try
{
if (HelloJobHelper.Trigger != null)
{
ViewBag.JobKey = "remotelyAddedJob";
ViewBag.State = HelloJobHelper.Scheduler.GetTriggerState(HelloJobHelper.Trigger.Key);
ViewBag.StartTime = HelloJobHelper.Trigger.StartTimeUtc.ToString();
}
else
{
ViewBag.State = "获取Job执行状态失败";
}
}
catch (Exception ex)
{
ViewBag.State = "Job服务器连接失败";
} return View();
}
public ActionResult Run()
{
HelloJobHelper.RunJob(); return RedirectToAction("Index", "HelloJob");
}
public ActionResult Pause()
{
HelloJobHelper.PauseJob(); return RedirectToAction("Index", "HelloJob");
}
public ActionResult Resume()
{
HelloJobHelper.ResumeJob();
return RedirectToAction("Index", "HelloJob");
}
}

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

         public static void RunJob()
{
if (!scheduler.CheckExists(jobKey))
{
IJobDetail job = JobBuilder.Create<HelloJob>()
.WithIdentity(jobKey)
.Build(); JobDataMap map = job.JobDataMap;
map.Put("msg", "Your remotely added job has executed!"); ITrigger trigger = TriggerBuilder.Create()
.WithIdentity(triggerKey)
.ForJob(job.Key)
.WithCronSchedule("/5 * * ? * *")
.Build(); scheduler.ScheduleJob(job, trigger); JobDetail = job;
Trigger = trigger;
}
}

暂停比较简单

         public static void PauseJob()
{
scheduler.PauseJob(jobKey);
}

2.5、View

 @{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Bootstrap.cshtml";
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<style>
.col-sm-offset-2 {
margin-left:20px;
}
</style>
</head>
<body>
<br />
@using (Html.BeginForm("Run", "HelloJob", null, FormMethod.Post, new { @id = "form1", @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="hidden" name="Id" id="Id" />
<button type="submit" class="btn btn-default">Run</button>
</div>
</div>
} @using (Html.BeginForm("Pause", "HelloJob", null, FormMethod.Post, new { @id = "form2", @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="hidden" name="Id" id="Id" />
<button type="submit" class="btn btn-default">Pause</button>
</div>
</div>
} @using (Html.BeginForm("Resume", "HelloJob", null, FormMethod.Post, new { @id = "form3", @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="hidden" name="Id" id="Id" />
<button type="submit" class="btn btn-default">Resume</button>
</div>
</div>
} <br />
<div>
<ul>
<li>ViewBag.JobKey: @ViewBag.JobKey</li>
<li>ViewBag.State: @ViewBag.State</li>
<li>ViewBag.StartTime: @ViewBag.StartTime</li>
<li>ViewBag.ExecuteTimes: @ViewBag.ExecuteTimes</li>
</ul>
</div> </body>
</html>

2.6 好了,我们先运行服务端,开起来就好了

2.7、运行Web

2.7.1 点击Run

2.7.2、点击Pause(暂停)

2.7.3、点击Resume(恢复)

2.8、最后看看项目代码层次,涉及3个:MVC、Job.Items、Job.Server

好了,基本的功能有了。这篇就到这里

任务调度及远端管理(基于Quartz.net)的更多相关文章

  1. 任务调度之持久化(基于Quartz.net)

    上一篇我们了解了任务调度及他的远端管理方式,传送门:任务调度及远端管理(基于Quartz.net) 这篇我们要完成任务调度的持久化功能,即新增修改删除之类的功能,这必须得要有的,不然都不知道后台都有什 ...

  2. 任务调度之集群(基于Quartz.net)

    上一篇我们完成了任务调度的持久化,传送门:任务调度之持久化(基于Quartz.net) 这篇我们来完成Quartz.net的一个比较优秀的功能,即集群:集群可以提高任务调度服务的容灾性, 当一个节点宕 ...

  3. RDIFramework.NET框架基于Quartz.Net实现任务调度详解及效果展示

    在上一篇Quartz.Net实现作业定时调度详解,我们通过实例代码详细讲解与演示了基于Quartz.NET开发的详细方法.本篇我们主要讲述基于RDIFramework.NET框架整合Quartz.NE ...

  4. Window服务基于Quartz.Net组件实现定时任务调度(二)

    前言: 在上一章中,我们通过利用控制台实现定时任务调度,已经大致了解了如何基于Quartz.Net组件实现任务,至少包括三部分:job(作业),trigger(触发器),scheduler(调度器). ...

  5. 一行代码完成定时任务调度,基于Quartz的UI可视化操作组件 GZY.Quartz.MUI

    前言 之前发布过第一个版本,有兴趣的可以去看看: NET Core 基于Quartz的UI可视化操作组件 GZY.Quartz.MUI 简介 GitHub开源地址:l2999019/GZY.Quart ...

  6. Quartz.NET总结(五)基于Quartz.net 的开源任务管理平台

    前面总结了很多,关于Quartz.net 的文章,介绍了如何使用Quartz.net.不清楚的朋友,可以看我之前的系列文章,http://www.cnblogs.com/zhangweizhong/c ...

  7. 基于Quartz.NET构建自己的动态作业调度器

    在日常的开发中,运行定时任务基本上已经是很普遍的需求了,可以通过windows服务+timer组件来实现,也可以使用第三方框架来集成,Quartz.NET就是一款从JAVA的Quartz移植过来的一个 ...

  8. 基于Quartz实现简单的定时发送邮件

    一.什么是Quartz Quartz 是一个轻量级任务调度框架,只需要做些简单的配置就可以使用:它可以支持持久化的任务存储,即使是任务中断或服务重启后,仍可以继续运行.Quartz既可以做为独立的应用 ...

  9. Java 基于quartz实现定时 之二(XML方式配置)

    <!-- 在spring核心配置文件中进行如下配置 --> <!-- Spring基于quartz定时任务 --> <bean id="triggerByBea ...

随机推荐

  1. leetcode139

    class Solution { public: bool wordBreak(string s, vector<string> wordDict) { vector<, false ...

  2. 深度学习原理与框架-Tensorflow卷积神经网络-cifar10图片分类(代码) 1.tf.nn.lrn(局部响应归一化操作) 2.random.sample(在列表中随机选值) 3.tf.one_hot(对标签进行one_hot编码)

    1.tf.nn.lrn(pool_h1, 4, bias=1.0, alpha=0.001/9.0, beta=0.75) # 局部响应归一化,使用相同位置的前后的filter进行响应归一化操作 参数 ...

  3. MySql 的基本使用之连接数据库、选择数据库、查看表结构

    1.连接 mysql  mysql  -u username -p -h host -P port 端口号:默认 3306. 如果是连接本地数据库,可以直接使用 mysql -uroot -p 2.选 ...

  4. Sql Server数据库之约束

    一.约束的分类 实体约束:关于行的约束,比如某一行出现的值就不允许别的行出现,如主键 域约束:关于列的约束,对表中所有行的某些列进行约束,如check约束 参照完整性约束:如果某列的值必须与其他列的值 ...

  5. es6之更优雅的条件语句

    在使用JavaScript时,条件判断是经常会用到的,一些简单的判断条件还可以接受,当遇到比较复杂多重条件时就比较恶心了.这里使用es6的小技巧使判断更优雅. 1.使用 Arrary.includes ...

  6. Windows 10同步时间的方法

    今天在安装了Windows 10 1809(October 2018 update)之后发现时间不能同步,以前并没有出现这种情况. 1) 打开控制面板,找到时钟域地区 2) 选择日期和时间 3) 选择 ...

  7. Python基础-python数据类型(四)

    python数据类型 在python中,变量就是变量,它没有类型,我们所说的类型是变量所指的内存中对象的类型. python中的数据类型: 1.数字 python中没有专门定义常量的方式,通常使用大写 ...

  8. eclipse中启动项目报内存溢出问题通过修改配置解决

     标注:添加下面的参数还是挺管用的,本人亲测可试,同时启用两个项目,总是报堆内存不足,加了下面的参数后变可以同时正常运行了. 错误如下: Error occurred during initializ ...

  9. Error running Tomcat8: Address localhost:xxxx is already in use

    参考自: https://blog.csdn.net/huazhongkejidaxuezpp/article/details/41813683 第一步,命令提示符号,执行命令:netstat -an ...

  10. ES6自我总结笔记(阮一峰ES6入门)

    [let和const命令] 1.var的作用域是函数体内,不是块级作用域 2.let是更完美的var,let的变量的作用是块级作用域 3.let声明的全局变量不是全局对象属性,不可以通过window. ...