1.创建一个定时调度Quartz类

 using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Topshelf; namespace TopShelfSolution
{ public sealed class QuartzServiceRunner :ServiceControl,ServiceSuspend
{
private readonly IScheduler scheduler; public QuartzServiceRunner()
{
scheduler = StdSchedulerFactory.GetDefaultScheduler();
} public bool Start(HostControl hostControl)
{
scheduler.Start();
return true;
} public bool Stop(HostControl hostControl)
{
scheduler.Shutdown(false);
return true;
} public bool Continue(HostControl hostControl)
{
scheduler.ResumeAll();
return true;
} public bool Pause(HostControl hostControl)
{
scheduler.PauseAll();
return true;
} }
}

2.创建用于处理业务逻辑的类

 using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace TopShelfSolution.QuartzJobs
{
public sealed class QuartzJobForTest :IJob
{
public void Execute(IJobExecutionContext context)
{
//处理业务逻辑
Console.WriteLine(DateTime.Now.ToString("yyyyMMddhhmmssfff")); }
}
}

3.创建quartz.config配置文件

# You can configure your scheduler in either <quartz> configuration section
# or in quartz properties file
# Configuration section has precedence quartz.scheduler.instanceName = ServerScheduler # configure thread pool info
quartz.threadPool.type = Quartz.Simpl.SimpleThreadPool, Quartz
quartz.threadPool.threadCount =
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 =
quartz.scheduler.exporter.bindName = QuartzScheduler
quartz.scheduler.exporter.channelType = tcp
quartz.scheduler.exporter.channelName = httpQuartz

4.创建定时job的配置信息xml文件

<?xml version="1.0" encoding="UTF-8"?>

<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> <job>
<name>QuartzJobForTestJob</name>
<group>TestJobGroup</group>
<description>Test_Sample job for Quartz</description>
<job-type>TopShelfSolution.QuartzJobs.QuartzJobForTest, TopShelfSolution</job-type>
<durable>true</durable>
<recover>false</recover>
</job> <trigger>
<cron>
<name>QuartzJobForTestJobTrigger</name>
<group>TestJobTriggerGroup</group>
<job-name>QuartzJobForTestJob</job-name>
<job-group>TestJobGroup</job-group>
<!--从start-time起,从0秒开始,每1/5秒执行一次IJob.Execute-->
<start-time>--22T00::+:</start-time>
<cron-expression>/ * * * * ?</cron-expression>
</cron>
</trigger> </schedule> </job-scheduling-data>

5.主函数入口

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using Topshelf; namespace TopShelfSolution
{
class Program
{
static void Main(string[] args)
{
HostFactory.Run(u =>
{
u.Service<QuartzServiceRunner>();
u.RunAsLocalSystem(); u.SetDescription("Sample Topshelf Host服务的描述");
u.SetDisplayName("Stuff显示名称");
u.SetServiceName("Stuff服务名称"); //new Ttask().Start();
});
}
}
}

注:每次修改完quartz_jobs.xml文件后, 需重新生成项目,将quartz_jobs.xml 复制到当前项目bin\Release目录下。

Topshelf + Quartz2.5 创建基于windows服务的更多相关文章

  1. 使用Topshelf组件构建简单的Windows服务

    很多时候都在讨论是否需要了解一个组件或者一个语言的底层原理这个问题,其实我个人觉得,对于这个问题,每个人都有自己的看法,个人情况不同,选择的方式也就会不同了.我个人觉得无论学习什么,都应该尝试着去了解 ...

  2. asp.net基于windows服务实现定时发送邮件的方法

    本文实例讲述了asp.net基于windows服务实现定时发送邮件的方法.分享给大家供大家参考,具体如下: //定义组件 private System.Timers.Timer time; publi ...

  3. 安装自创建的windows服务。

    安装自创建的windows服务. 使用工具InstallUtil.exe进行安装和卸载创建的windows服务 安装:C:/WINDOWS/Microsoft.NET/Framework/v2.0.5 ...

  4. 为MongoDB创建一个Windows服务

    一:选型,根据机器的操作系统类型来选择合适的版本,使用下面的命令行查询机器的操作系统版本 wmic os get osarchitecture 二:下载并安装 附上下载链接 点击安装包,我这里是把文件 ...

  5. Windows2012中安装Nginx并创建为Windows服务

    安装Nginx 下载windows版nginx (http://nginx.org/download/nginx-1.10.0.zip),之后解压到需要放置的位置(D:\xampp\nginx) 将N ...

  6. tomcat创建一个windows服务

    具体步骤如下: 1.把JDK解压到C:\Program Files\Java下,Tomcat解压到D:\tomcat下 2.配置环境变量 JAVA_HOME:C:\Program Files\Java ...

  7. 基于Windows服务的聊天程序(一)

    本文将演示怎么通过C#开发部署一个Windows服务,该服务提供各客户端的信息通讯,适用于局域网.采用TCP协议,单一服务器连接模式为一对多:多台服务器的情况下,当客户端连接数超过预设值时可自动进行负 ...

  8. 基于Windows服务的WCF

    (1)创建WCF 代码示例: [ServiceContract] public interface ILimsDBService { [OperationContract] int ExecuteSq ...

  9. [翻译] 使用 .NET Core 3.0 创建一个 Windows 服务

    原文: .NET Core Workers as Windows Services 在 .NET Core 3.0 中,我们引入了一种名为 Worker Service 的新型应用程序模板.此模板旨在 ...

随机推荐

  1. (转)winform下TCP通信的简单应用

    本文转载自:http://blog.csdn.net/wanlong360599336/article/details/7557064 先看效果图: TCP比较繁琐的就是三次握手定理,每次再发送数据前 ...

  2. linux命令echo和cat比较

    当前主要比较echo 和 cat的重定向功能 1.echo 1 > /proc/xxx 解析: echo 进行重定向的时候,仅仅是将字符"1" 输出到 /proc/xxx文件 ...

  3. Py修行路 python基础 (二十五)线程与进程

    操作系统是用户和硬件沟通的桥梁 操作系统,位于底层硬件与应用软件之间的一层 工作方式:向下管理硬件,向上提供接口 操作系统进行切换操作: 把CPU的使用权切换给不同的进程. 1.出现IO操作 2.固定 ...

  4. Python Twisted系列教程13:使用Deferred新功能实现新客户端

    作者:dave@http://krondo.com/deferred-all-the-way-down/  译者:杨晓伟(采用意译) 你可以从这里从头阅读这个系列. 介绍 回忆下第10部分中的客户端5 ...

  5. SqlServer——常见问题汇总

    1.存储过程手动执行正常,应用程序高并发允许时,数据成倍数增加 通常此类问题是由于存储过程中使用了永久表作为中间表,用以存储临时数据.当高并发时,比如同时执行3次,则同时往中间表中插入3倍的数据,得到 ...

  6. leetcode872

    class Solution { public: vector<int> v1; vector<int> v2; void GetLeaf(TreeNode* tree, in ...

  7. Burpsuite模块—-Intruder模块详解

    一.简介 Burp Intruder是一个强大的工具,用于自动对Web应用程序自定义的攻击,Burp Intruder 是高度可配置的,并被用来在广范围内进行自动化攻击.你可以使用 Burp Intr ...

  8. easyui之datagrid之formatter(后台传递常量自动转换值)

    1,datagrid之formatter formatter格式化函数有三个参数: value:字段值(一般为后台传递给前台的值): row:当前行数据: index:当前行索引. return值是显 ...

  9. 时区时差换算(GMT,UTC,PST,PDT)

    2014年美国冬令时标准时间Stardand Time于11月2号开始实施,直到2015年3月8号为止. 冬令时,是指在冬天使用的标准时间.在使用日光节约时制(夏令时)的地区,夏天时钟拨快一小时,冬天 ...

  10. Python代码的编译

    Python代码的编译 Python代码在解释执行之前,是会被编译成.pyc或者.pyo文件的,它们是中间字节码表示的文件,之后Python虚拟机才会去解释执行它们. 1.pyc文件 ======== ...