Quartz.Net任务调度总结
Quartz.Net使用经验总结:
学习参考的例子不错,分享一下:
(1)https://www.cnblogs.com/jys509/p/4628926.html,该博文介绍比较全面
(2)https://www.cnblogs.com/abeam/p/8042531.html 该博文有关于任务调度及配置文件详细解释,很棒。
(3)https://www.cnblogs.com/JentZhang/p/9597597.html 不错的学习例子
(4)https://www.cnblogs.com/abeam/p/8042531.html :关于"使用 Topshelf 结合 Quartz.NET 创建 Windows 服务"的总结
(5) https://www.jianshu.com/p/f5118bd69d34 :关于Quartz 2.x 与3.x版本差异分析不错
第一步:新建控制台应用程序:QuartzJobDemo
先直接看目录结构:

第二步直接上源码:
1.作业调度:ServiceRunner.cs源码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//using Common.Logging;//引用下面的吧
using log4net;//_logger.InfoFormat("TestJob测试"); ---》为了输出,引用这个吧
using Quartz;
using Quartz.Impl;
using Topshelf;
namespace QuartzJobDemo.Services
{
public sealed class ServiceRunner: ServiceControl,ServiceSuspend
{
private readonly IScheduler _scheduler;
private readonly ILog _logger = LogManager.GetLogger(typeof(TestJob));
#region Quartz 2.X版本
public ServiceRunner()
{
_scheduler = StdSchedulerFactory.GetDefaultScheduler();
}
#endregion
#region Quartz 3.X版本
//public ServiceRunner()
//{
// scheduler = StdSchedulerFactory.GetDefaultScheduler().GetAwaiter().GetResult();
//}
#endregion
public bool Start(HostControl hostControl)
{
_logger.InfoFormat(@"开始服务:{0}", DateTime.Now);
_scheduler.Start();
return true;
}
public bool Stop(HostControl hostControl)
{
_logger.InfoFormat(@"停止服务:{0}", DateTime.Now);
_scheduler.Shutdown(false);
return true;
}
public bool Continue(HostControl hostControl)
{
_logger.InfoFormat(@"继续服务:{0}", DateTime.Now);
_scheduler.ResumeAll();
return true;
}
public bool Pause(HostControl hostControl)
{
_logger.InfoFormat(@"暂停服务:{0}", DateTime.Now);
_scheduler.PauseAll();
return true;
}
}
}
2.作业执行TestJob.cs源码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//using Common.Logging;//引用下面的吧
using log4net;//_logger.InfoFormat("TestJob测试"); ---》为了输出,引用这个吧
using Quartz;
namespace QuartzJobDemo
{
public sealed class TestJob: IJob
{
private readonly ILog _logger = LogManager.GetLogger(typeof(TestJob));
#region Quartz 2.X版本
public void Execute(IJobExecutionContext context)
{
_logger.InfoFormat("TestJob测试");
}
#endregion
#region Quartz 3.X版本
//public Task Execute(IJobExecutionContext context)
//{
// _logger.InfoFormat("TestJob测试");
// return Task.FromResult(true);
//}
/**
* 重点问题是新的IJob实现Execute返回的是Task,老的是void,很多在使用的时候对于这个地方的返回不知道如何处理,看代码增加一行:
return Task.FromResult(true);
*/
#endregion
}
}
3.主程序Program.cs源码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QuartzJobDemo.Services;
using Topshelf;
namespace QuartzJobDemo
{
/**
* 说明:学习地址:https://www.cnblogs.com/jys509/p/4628926.html
*注意点 (1)
* Quartz版本:2.6.1
* TopShelf版本:3.0.2 TopShelf版本与 Topshelf.Log4Net版本最好一一对应!!!!!
* Topshelf.Log4Net版本:3.0.2
*注意点 (2)
*三个配置文件的属性都是改为始终复制:quartz_jobs.xml log4net.config quartz.config
* 关于配置文件的具体说明:https://www.cnblogs.com/abeam/p/8044460.html
* 注意点(3)
* TestJob.cs中还是引用: using log4net;不要引用using Common.Logging;
*
*这个学习地址: https://www.cnblogs.com/abeam/p/8042531.html 也可以看看!!!+
*/
class Program
{
static void Main(string[] args)
{
log4net.Config.XmlConfigurator.ConfigureAndWatch(
new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config"));
HostFactory.Run(x =>
{
#region Quartz 2.X版本: 2.6.1
x.UseLog4Net();
x.Service<ServiceRunner>();
x.SetDescription("QuartzJobDemo服务描述");
x.SetDisplayName("QuartzJobDemo服务显示名称");
x.SetServiceName("QuartzJobDemo服务名称");
x.EnablePauseAndContinue();
#endregion
#region Quartz 2.X版本: 2.6.1
//x.UseLog4Net();
//x.Service<ServiceRunner>(s => {
// s.ConstructUsing(name => new ServiceRunner());
// s.WhenStarted((tc, hc) => tc.Start(hc));
// s.WhenStopped((tc, hc) => tc.Stop(hc));
//});
//x.RunAsLocalService();
//x.StartAutomaticallyDelayed();
//x.SetDescription("TestQuartzJob");
//x.SetDisplayName("TestQuartzJob");
//x.SetServiceName("TestQuartzJob");
//x.EnablePauseAndContinue();
#endregion
});
}
}
}
三个配置文件(自己手动创建即可)源码:
附配置文件详解:
4.log4net.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<!--日志路径-->
<param name= "File" value= "D:\QuartzJobDemo_Log\servicelog\"/>
<!--是否是向文件中追加日志-->
<param name= "AppendToFile" value= "true"/>
<!--log保留天数-->
<param name= "MaxSizeRollBackups" value= "10"/>
<!--日志文件名是否是固定不变的-->
<param name= "StaticLogFileName" value= "false"/>
<!--日志文件名格式为:2008-08-31.log-->
<param name= "DatePattern" value= "yyyy-MM-dd".read.log""/>
<!--日志根据日期滚动-->
<param name= "RollingStyle" value= "Date"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n %loggername" />
</layout>
</appender>
<!-- 控制台前台显示日志 -->
<appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
<mapping>
<level value="ERROR" />
<foreColor value="Red, HighIntensity" />
</mapping>
<mapping>
<level value="Info" />
<foreColor value="Green" />
</mapping>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%n%date{HH:mm:ss,fff} [%-5level] %m" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="Info" />
<param name="LevelMax" value="Fatal" />
</filter>
</appender>
<root>
<!--(高) OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL (低) -->
<level value="all" />
<appender-ref ref="ColoredConsoleAppender"/>
<appender-ref ref="RollingLogFileAppender"/>
</root>
</log4net>
</configuration>
5.quartz.config: 这个虽然提示无效的text标记,没关系不影响
# You can configure your scheduler in either <quartz> configuration section
# or in quartz properties file
# Configuration section has precedence
quartz.scheduler.instanceName = QuartzJobDemo
# configure thread pool info
quartz.threadPool.type = Quartz.Simpl.SimpleThreadPool, Quartz
quartz.threadPool.threadCount = 10
quartz.threadPool.threadPriority = Normal
# job initialization plugin handles our xml reading, without it defaults are used
quartz.plugin.xml.type = Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz
quartz.plugin.xml.fileNames = ~/quartz_jobs.xml
# export this server to remoting context
#quartz.scheduler.exporter.type = Quartz.Simpl.RemotingSchedulerExporter, Quartz
#quartz.scheduler.exporter.port = 555
#quartz.scheduler.exporter.bindName = QuartzScheduler
#quartz.scheduler.exporter.channelType = tcp
#quartz.scheduler.exporter.channelName = httpQuartz
6.quartz_job.xml:
<?xml version="1.0" encoding="utf-8" ?>
<!-- This file contains job definitions in schema version 2.0 format -->
<job-scheduling-data xmlns="http://quartznet.sourceforge.net/JobSchedulingData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0">
<processing-directives>
<overwrite-existing-data>true</overwrite-existing-data>
</processing-directives>
<schedule>
<!--TestJob测试 任务配置-->
<job>
<name>TestJob</name>
<group>Test</group>
<description>TestJob测试</description>
<job-type>QuartzJobDemo.TestJob,QuartzJobDemo</job-type><!--命名空间.作业名,命名空间-->
<!--<job-type>QuartzJobDemo.QuartzJobs.TestJob,QuartzJobDemo</job-type>-->
<durable>true</durable>
<recover>false</recover>
</job>
<trigger>
<cron>
<name>TestJobTrigger</name>
<group>Test</group>
<job-name>TestJob</job-name>
<job-group>Test</job-group>
<start-time>2019-12-20T00:00:00+08:00</start-time>
<cron-expression>0/1 * * * * ?</cron-expression>
</cron>
</trigger>
</schedule>
</job-scheduling-data>
附:配置文件详解:转自:https://www.cnblogs.com/abeam/p/8044460.html,博主总结很好(博主总结的另一篇关于任务调度的例子也很适合学习:https://www.cnblogs.com/abeam/p/8042531.html)
直接截图吧,搞一天累了:





Quartz.Net任务调度总结的更多相关文章
- Spring Quartz实现任务调度
任务调度 在企业级应用中,经常会制定一些"计划任务",即在某个时间点做某件事情 核心是以时间为关注点,即在一个特定的时间点,系统执行指定的一个操作 任务调度涉及多线程并发.线程池维 ...
- Quartz实现任务调度
一.任务调度概述 在企业级应用中,经常会制定一些"计划任务",即在某个时间点做某件事情,核心是以时间为关注点,即在一个特定的时间点,系统执行指定的一个操作,任务调度涉及多线程并发. ...
- quartz.net任务调度:源码及使用文档
目录: 1.quartz.net任务调度:源码及使用文档 2.quartz.net插件类库封装 前言 前段时间把自己封装quartz.net 类库的过程总结到博客园,有网友想要看一下源码,所以就把源码 ...
- 项目ITP(五) spring4.0 整合 Quartz 实现任务调度
前言 系列文章:[传送门] 项目需求: 二维码推送到一体机上,给学生签到扫描用.然后需要的是 上课前20分钟 ,幸好在帮带我的学长做 p2p 的时候,接触过.自然 quartz 是首选.所以我就配置了 ...
- 项目一:第十四天 1.在realm中动态授权 2.Shiro整合ehcache 缓存realm中授权信息 3.动态展示菜单数据 4.Quartz定时任务调度框架—Spring整合javamail发送邮件 5.基于poi实现分区导出
1 Shiro整合ehCache缓存授权信息 当需要进行权限校验时候:四种方式url拦截.注解.页面标签.代码级别,当需要验证权限会调用realm中的授权方法 Shiro框架内部整合好缓存管理器, ...
- Java&Quartz实现任务调度
目录 Java&Quartz实现任务调度 1.Quartz的作用 2.预备 3.Quartz核心 3.1.Job接口 3.2.JobDetail类 3.3 JobExecutionContex ...
- Quartz.Net任务调度框架
Quartz.Net是一个开源的任务调度框架,非常强大,能够通过简单的配置帮助我们定时具体的操作. 相对于我们用的线程里面while(true)然后sleep来执行某个操作,应该算的上是高端,大气,上 ...
- Spring 中使用Quartz实现任务调度
前言:Spring中使用Quartz 有两种方式,一种是继承特定的基类:org.springframework.scheduling.quartz.QuartzJobBean,另一种则不需要,(推荐使 ...
- ASP.NET MVC5 实现基于Quartz.NET任务调度
工作之余.技术?.记是不可能记住的. 只有写点东西 才能维持得了生活这样子的.好早就像写一篇关于任务调度的文章.终究是太懒了 一.Quartz.NET介绍 Quartz.NET是一个强大.开源.轻量的 ...
- 浅谈Quartz定时任务调度
一 开发概述 对于具有一定规模的大多数企业来说,存在着这样一种需求:存在某个或某些任务,需要系统定期,自动地执行,然而,对大多数企业来说,该技术的实现,却是他们面临的一大难点和挑战. 对于大部分企 ...
随机推荐
- katalon设置Android SDK路径
本文链接:https://blog.csdn.net/feiniao8651/article/details/82809147文章允许转载,请注明来源:https://blog.csdn.net/fe ...
- 基于Websocket的websocketd
WebSocket是什么 WebSocket是HTML5下面的一种技术,设计出来的目的就是要取代轮询和 Comet 技术,使客户端浏览器具备像 C/S 架构下桌面系统的实时通讯能力. 浏览器通过 Ja ...
- 使用maven构建dubbo服务的可执行jar包+Dubbo 程序实例
https://blog.csdn.net/zsg88/article/details/76100482 https://blog.csdn.net/zsg88/article/details/762 ...
- Android系统分析之Audio音频流, 音频策略, 输出设备之间的关系
音频流, 音频策略, 输出设备之间的关系 只针对 AudioManager.STREAM_VOICE_CALL 音频流类型进行分析 涉及到的类: hardware/libhardware_legacy ...
- JVM常用指标查询
一.what‘s going on in Java Application 当系统卡顿,应用莫名被杀掉,我们应该怎么排查?在我已知的系统挂掉的情况,只经历过两种:1.内存申请不了(OOM):2.CPU ...
- c++ vector数组的使用
介绍: 首先我们要明白以下几点: 1.vector数组是一个能存放任意数据类型(类,结构,普通变量类型等)的动态数组!,在数据结构中就相当于顺序储存的线性表,寻找元素非常快,但是插入元素的时间却很大( ...
- SQL truncate 、delete与drop区别(转)
相同点: 1.truncate和不带where子句的delete.以及drop都会删除表内的数据. 2.drop.truncate都是DDL语句(数据定义语言),执行后会自动提交. 不同点: 1. t ...
- spring ObjectFactory
AspectJAwareAdvisorAutoProxyCreator implements PostProcessor postProcessorAfterInitialization(Object ...
- Redis持久化(转载)
原文地址:http://www.jianshu.com/p/2f14bc570563?from=jiantop.com 数据持久化 Redis提供了将数据定期自动持久化至硬盘的能力,包括RDB和AOF ...
- Java-Shiro(五):Shiro Realm讲解(二)IniRealm的用法、JdbcRelam的用法、自定义Realm
引入 上一篇在讲解Realm简介时,介绍过Realm包含大概4类缺省的Realm,本章主要讲解: 1)IniRealm的用法: 2)JdbcRealm基于mysql 默认表及查询语句实现认证.授权 ...