一.准备工作

  • 创建.net core 控制台应用程序
  • 添加NuGet程序包

二.配置Quartz
- 创建appsettings.json文件,右键文件属性,并更改属性为始终复制 内容

  "quartz": {"scheduler": {"instanceName": "CoreSolution.Job"},"threadPool": {"type": "Quartz.Simpl.SimpleThreadPool, Quartz","threadPriority": "Normal","threadCount": 10},"plugin": {"jobInitializer": {"type": "Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz.Plugins","fileNames": "quartz_jobs.xml"}}}

三.创建QuartzOption 类

public class QuartzOption{public QuartzOption(IConfiguration config){if (config == null){throw new ArgumentNullException(nameof(config));}var section = config.GetSection("quartz");section.Bind(this);}public Scheduler Scheduler { get; set; }public ThreadPool ThreadPool { get; set; }public Plugin Plugin { get; set; }public NameValueCollection ToProperties(){var properties = new NameValueCollection{["quartz.scheduler.instanceName"] = Scheduler?.InstanceName,["quartz.threadPool.type"] = ThreadPool?.Type,["quartz.threadPool.threadPriority"] = ThreadPool?.ThreadPriority,["quartz.threadPool.threadCount"] = ThreadPool?.ThreadCount.ToString(),["quartz.plugin.jobInitializer.type"] = Plugin?.JobInitializer?.Type,["quartz.plugin.jobInitializer.fileNames"] = Plugin?.JobInitializer?.FileNames};return properties;}}public class Scheduler{public string InstanceName { get; set; }}public class ThreadPool{public string Type { get; set; }public string ThreadPriority { get; set; }public int ThreadCount { get; set; }}public class Plugin{public JobInitializer JobInitializer { get; set; }}public class JobInitializer{public string Type { get; set; }public string FileNames { get; set; }}

四.添加一个job

  • 需要继承IJob接口
 public class TestJob : IJob{public Task Execute(IJobExecutionContext context){//你的业务代码....WriteLogHelper.WriteLogMany("开始执行testJob");return Task.FromResult(true);}}

五.实现IJobFactory

  public class JobFactory : IJobFactory{protected readonly IServiceProvider Container;public JobFactory(IServiceProvider container){Container = container;}public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler){return Container.GetService(bundle.JobDetail.JobType) as IJob;}public void ReturnJob(IJob job){(job as IDisposable)?.Dispose();}}

六.创建Quartz调度的配置文件 quartz_jobs.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>CoreSolution.Job.jobs.TestJob,CoreSolution.Job</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><cron-expression>0 0 1 * * ?</cron-expression><!--每天凌晨1点执行一次--></cron></trigger><!--TestJob2测试 任务配置--><job><name>TestJob2</name><group>Test</group><description>TestJob2测试</description><job-type>CoreSolution.Job.jobs.TestJob2,CoreSolution.Job</job-type><durable>true</durable><recover>false</recover></job><trigger><cron><name>TestJob2Trigger</name><group>Test</group><job-name>TestJob2</job-name><job-group>Test</job-group><cron-expression>0/30 * * * * ?</cron-expression><!--30s执行一次--></cron></trigger></schedule>
</job-scheduling-data>

七.添加QuartzServer

 public class QuartzServer : ServiceControl, ServiceSuspend{//调度器private IScheduler scheduler;public QuartzServer(){scheduler = StdSchedulerFactory.GetDefaultScheduler().GetAwaiter().GetResult();}public bool Start(HostControl hostControl){scheduler.Start();Console.WriteLine("Quartz任务调度启动");return true;}public bool Continue(HostControl hostControl){scheduler.ResumeAll();return true;}public bool Pause(HostControl hostControl){scheduler.PauseAll();return true;}public bool Stop(HostControl hostControl){scheduler.Shutdown();Console.WriteLine("Quartz任务调度关闭");return true;}}

八.配置TopShelf

    public class Program{static void Main(string[] args){var rc = HostFactory.Run(x =>{x.Service<QuartzServer>(s =>{s.ConstructUsing(name => new QuartzServer());s.WhenStarted((tc, hc) => tc.Start(hc));s.WhenStopped((tc, hc) => tc.Stop(hc));s.WhenContinued((tc, hc) => tc.Continue(hc));s.WhenPaused((tc, hc) => tc.Pause(hc));});x.RunAsLocalSystem();//使用本地服务账户运行x.SetDescription("QuartzAndTopshelf服务描述");//服务描述x.SetDisplayName("QuartzAndTopshelf");//服务显示名称x.SetServiceName("QuartzAndTopshelf");//服务名称});var exitCode = (int)Convert.ChangeType(rc, rc.GetTypeCode());Environment.ExitCode = exitCode;Console.ReadKey();}}

在项目文件中加上项目的运行环境相关配置

  <PropertyGroup><OutputType>Exe</OutputType><TargetFramework>netcoreapp2.1</TargetFramework><RuntimeIdentifier>win10-x64</RuntimeIdentifier></PropertyGroup>

运行

  • 项目编译完成后进入bin\Debug\netcoreapp2.1\win10-x64目录,在此处以管理员运行cmd,然后执行依次CoreSolution.Job.exe install 安装服务 ,CoreSolution.Job.exe start 启动服务。
  • 安装启动成功后在服务里面可以看到。

项目结构图

.Net Core+Topshelf+Quartz创建Windows定时任务相关推荐

  1. .NET Core 使用Topshelf方式创建Windows服务

    Topshelf是一个.NET Standard库,它消除了在.NET Framework和.NET Core中创建Windows服务的那些麻烦. 安装 Install-Package Topshel ...

  2. .NET 使用Topshelf方式创建Windows服务

    阅读目录 安装 代码 部署服务 调试服务 Topshelf是一个.NET Standard库,它消除了在.NET Framework和.NET Core中创建Windows服务的那些麻烦. 安装 In ...

  3. 使用Topshelf快捷创建Windows服务

    Topshelf相关 GitHub地址: https://github.com/Topshelf/Topshelf 官方文档 :http://docs.topshelf-project.com/en/ ...

  4. 使用Topshelf轻松创建Windows服务

    目录 介绍 入门 示例1-基本的winservice 所需的NuGet软件包 Topshelf Bootstrap.cs 示例2-Scheduledservice 所需的NuGet软件包 Topshe ...

  5. C#使用Topshelf和Quartz开发处理定时任务的Windows服务程序

    C#使用Topshelf框架和Quartz开发处理定时任务的Windows服务程序 背景 依赖 C#代码示例 任务调度的配置文件 测试 服务安装.启动.停止.卸载 背景 有些业务是运行在后台,需要界面 ...

  6. 使用.NET Core创建Windows服务(二) - 使用Topshelf方式

    原文:Creating Windows Services In .NET Core – Part 2 – The "Topshelf" Way 作者:Dotnet Core Tut ...

  7. 使用.NET Core创建Windows服务(二) - 使用Topshelf方式

    使用.NET Core创建Windows服务 使用微软推荐方式 使用Topshelf方式 在前一篇文章中,我给大家介绍了,如何基于微软推荐方式使用.NET Core创建Windows服务.我们发现使用 ...

  8. 使用.NET Core创建Windows服务 - 使用.NET Core工作器方式

    原文:Creating Windows Services In .NET Core – Part 3 – The ".NET Core Worker" Way 作者:Dotnet ...

  9. 使用.NET Core创建Windows服务(一) - 使用官方推荐方式

    原文:Creating Windows Services In .NET Core – Part 1 – The "Microsoft" Way 创建Windows服务来运行批处理 ...

最新文章

  1. 改变静态文本notify 属性_CocosCreator脚本属性个性化定制——下拉列表属性、滑动条属性...
  2. CycleGAN非配对图像生成,定制你的卡通照
  3. Linux 文件安全之随机数生成器
  4. win7重装系统时,使用PE工具箱进入系统看到的“C盘变成0.2G,D盘变成48G左右”这是什么回事?...
  5. 【转】 pycharm快捷键、常用设置、配置管理(后两者详见原博客)
  6. 540.有序数组中的单一元素(力扣leetcode) 博主可答疑该问题
  7. Web 方向学习路线
  8. 第33篇-steam密码参数分析
  9. 机械专业怎么学matlab,MATLAB在机械类专业课教学中的应用
  10. 采用计算机发布调度命令时 必须严格遵守,关于调度命令规范格式.doc
  11. 虚拟串口模拟器和串口调试助手使用教程
  12. 本地简易股票量化回测框架
  13. OPPO R7Plusm(全网通)root、刷入twrp recovery、卡刷刷入CM系统教程合集_ recovery.img文件下载 联想A7600-m线刷刷机教程 手机卡在双4G双百兆无法开
  14. matlab 三角函数 积化和差,瞬间记住三角函数和差化积积化和差公式
  15. ASICC码对照表整理
  16. c语言字符串中字母降序数字升序,将字符串以ASCII码降序排列
  17. 移动音乐播放平台-酷狗音乐2021提供下载
  18. 如何系统自学Java成功就业?
  19. JavaScript语言精粹读书笔记
  20. JAVA Mall 项目致力于打造一个完整的电商系统,采用微服务架构设计

热门文章

  1. Caused by java.lang.ClassNotFoundException org.springframework.boot.context
  2. 虚拟机安装无人参与应答文件_关于通过vmware安装windows8的几个问题及解决--无人参与应答文件包含的产品密钥无效...
  3. JSP简单标签的开发
  4. iOS 之 UI界面概述
  5. 计算机组成原理实验运动码表,计算机组成原理实验【参考】.doc
  6. 用matlab生成高斯白噪声和均匀白噪声及其频谱
  7. 文章合集Raspberry Pi/树莓派
  8. 爬虫实战(二) 用Python爬取网易云歌单
  9. WEBRTC 录音与会议录音【转】
  10. 最新价值2500元 网易web白帽子黑客培训视频