(一)创建服务 QuarzService

using System.ServiceProcess;
using System.Text;

using Quartz;
using Quartz.Impl;

using WinNet.Log;
namespace QuarzService
{
public partial class QuartzService : ServiceBase
{

private IScheduler scheduler;

public QuartzService()
{
InitializeComponent();

ISchedulerFactory schedulerFactory = new StdSchedulerFactory();
scheduler = schedulerFactory.GetScheduler();

}

protected override void OnStart(string[] args)
{
scheduler.Start();

WriteLog.logTxt("Quartz服务成功启动", "scheduler");

}

protected override void OnStop()
{
scheduler.Shutdown();
WriteLog.logTxt("Quartz服务成功终止", "scheduler");

}

protected override void OnPause()
{
scheduler.PauseAll();

}

protected override void OnContinue()
{
scheduler.ResumeAll();
}
}
}

(二)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 = 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

写日志方法:

using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WinNet.Log
{
public class WriteLog
{
public WriteLog()
{
}

public static void logTxt(string Msg, string name)
{
string logPath = Path.GetDirectoryName(Application.ExecutablePath);
System.IO.StreamWriter sw = System.IO.File.AppendText(logPath + "/" + name + "_" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt");
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + Msg);
sw.Close();
sw.Dispose();
}
}
}

(三)创建一个任务:WinNet.JobTask

using Quartz;

using WinNet.Log;

namespace WinNet.JobTask
{
public class JobGeneral:IJob
{
//private static readonly Common.Logging.ILog logger = Common.Logging.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
#region IJob Members

public void Execute(IJobExecutionContext context)
{
try
{

WriteLog.logTxt("DemoJob1 任务开始运行", "scheduler");
for (int i = 0; i < 10; i++)
{
WriteLog.logTxt("DemoJob1 正在运行 " + i, "scheduler");

}

WriteLog.logTxt("DemoJob1任务运行结束", "scheduler");
}
catch (Exception ex)
{
WriteLog.logTxt("Error:"+ex.ToString (), "scheduler");
}
}

#endregion
}
}

(四)安装与卸载服务

安装服务,写成bat 文件

%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe QuarzService.exe
Net Start QuarzService
sc config QuarzService start= auto

卸载服务,写成bat 文件

%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe /u QuarzService.exe

源码:http://files.cnblogs.com/files/aran/ServiceSchedulerDesign.rar

Quartz 之 windowService的更多相关文章

  1. Quartz.Net在windows服务中的使用

    写在前面 这几天在弄一个项目,需要定时抓取一些数据,当时也想直接用timer算了.因为之前也弄过这样的项目,但是一想,已经用过了,再去使用同一种思路,未免太乏味了.就换了一种新玩法.这里将之前看到的一 ...

  2. Aspnet Zero中使用Windows service (Topshelf)来承载Quartz.net任务

    Aspnet Zero使用Windows service (Topshelf)来承载Quartz.net任务 网上有很多关于如何使用Topshelf创建ABP的Quartz windows服务,但很少 ...

  3. 免费开源的DotNet任务调度组件Quartz.NET(.NET组件介绍之五)

    很多的软件项目中都会使用到定时任务.定时轮询数据库同步,定时邮件通知等功能..NET Framework具有“内置”定时器功能,通过System.Timers.Timer类.在使用Timer类需要面对 ...

  4. Quartz

    Quartz是一个开源的作业调度框架,它完全由Java写成,并设计用于J2SE和J2EE应用中.它提供了巨大的灵 活性而不牺牲简单性.你能够用它来为执行一个作业而创建简单的或复杂的调度. eg: ja ...

  5. Spring Quartz实现任务调度

    任务调度 在企业级应用中,经常会制定一些"计划任务",即在某个时间点做某件事情 核心是以时间为关注点,即在一个特定的时间点,系统执行指定的一个操作 任务调度涉及多线程并发.线程池维 ...

  6. topshelf和quartz内部分享

    阅读目录: 介绍 基础用法 调试及安装 可选配置 多实例支持及相关资料 quartz.net 上月在公司内部的一次分享,现把PPT及部分交流内容整理成博客. 介绍 topshelf是创建windows ...

  7. Quartz.net持久化与集群部署开发详解

    序言 我前边有几篇文章有介绍过quartz的基本使用语法与类库.但是他的执行计划都是被写在本地的xml文件中.无法做集群部署,我让它看起来脆弱不堪,那是我的罪过. 但是quart.net是经过许多大项 ...

  8. Quartz.net开源作业调度框架使用详解

    前言 quartz.net作业调度框架是伟大组织OpenSymphony开发的quartz scheduler项目的.net延伸移植版本.支持 cron-like表达式,集群,数据库.功能性能强大更不 ...

  9. quartz.net 时间表达式----- Cron表达式详解

    序言 Cron表达式:就是用简单的xxoo符号按照一定的规则,就能把各种时间维度表达的淋漓尽致,无所不在其中,然后在用来做任务调度(定时服务)的quart.net中所认知执行,可想而知这是多么的天衣无 ...

随机推荐

  1. response对象详解

    (响应 javax.servlet.http.HttpServletResponse) 方法名 说明 addCookie 添加一个Cookie对象 addHeader 添加Http文件指定名字头信息 ...

  2. Android网络传输中必用的两个加密算法:MD5 和 RSA (附java完毕測试代码)

    MD5和RSA是网络传输中最经常使用的两个算法,了解这两个算法原理后就能大致知道加密是怎么一回事了.但这两种算法使用环境有差异,刚好互补. 一.MD5算法 首先MD5是不可逆的,仅仅能加密而不能解密. ...

  3. java并发 使用ScheduledExecutor的温室控制器--thinking in java 21.7.5

    package org.rui.thread.newc; import java.text.DateFormat; import java.text.SimpleDateFormat; import ...

  4. J2EE之ServletContext读取资源文件

    ServletContext读取资源文件内容的方式有两种: 方法1. public void doGet(HttpServletRequest request, HttpServletResponse ...

  5. Openstack中间DVR Part1 -- 东西走向的交通处理

    作者:Liping Mao  发表于:2014-07-04 版权声明:能够随意转载.转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明 在Openstack中L3router会造成流量集中 ...

  6. cocosbuilder中的Callbacks和sound effects

    cocosbuilder3中有增加了 Callback和sound effects 的timeline 这个东西用来在动画播放过程中控制音效和回调动作,非常方便 按住option键(alt), 点击t ...

  7. 嵌入式Linux-GNU Make 使用手册(中译版)

    GNU Make 使用手册(中译版) 翻译:于凤昌 译者注:本人在阅读Linux源代码过程中发现如果要全面了解Linux的结构.理解Linux的编程总体设计及思想必须首先全部读通Linux源代码中各级 ...

  8. 如何在 Objective-C 的环境下实现 defer

    关注仓库,及时获得更新:https://github.com/draveness/iOS-Source-Code-Analyze Follow: https://github.com/Dravenes ...

  9. hdu1016JAVA

    import java.util.Arrays;import java.util.Scanner;public class Main { public static int kase=0,n; pub ...

  10. Java基础知识强化之IO流笔记57:数据输入输出流(操作基本数据类型)

    1. 数据输入输出流(操作基本数据类型) (1)数据输入流:DataInputStream DataInputStream(InputStream in) (2)数据输出流:DataOutputStr ...