quartz.net使用(通过配置文件进行配置)

在项目Nuget包管理器中搜索:quartz,安装完成之后再项目中引用即可

 

先定义一个Job,需要实现IJob接口:

public class TestJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            log4net.ILog log = log4net.LogManager.GetLogger(typeof(TestJob));
            if (log.IsInfoEnabled)
            {
                log.Info("记录当前时间" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")+"\r\n");
            }
            Console.WriteLine("执行调度任务test"+" --- "+ DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
        }
    }

配置quartz_jobs.xml(附Cron生成器链接)

<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>
      <!--任务名称,同一个group中多个job的name不能相同-->
      <name>SimpleJob</name>
      <!--任务分组-->
      <group>sampleGroup</group>
      <!--任务描述-->
      <description>Sample job for Quartz Server</description>
      <!--完整命名空间的类名及所属程序集名称-->
      <job-type>WindowsService.Job.SimpleJob, WindowsService</job-type>
      <durable>true</durable>
      <recover>false</recover>
    </job>
    <!--任务触发器-->
    <trigger>
      <!--简单任务的触发器,可以调度用于重复执行的任务-->
      <!--<simple>
        --><!--触发器名称,同一个分组中的名称必须不同--><!--
        <name>sampleSimpleTrigger</name>
        --><!--触发器组--><!--
        <group>sampleGroup</group>
        --><!--描述--><!--
        <description>Simple trigger to simply fire sample job</description>
        --><!--要调度的任务名称,该job-name必须和对应job节点中的name完全相同--><!--
        <job-name>SimpleJob</job-name>
        --><!--调度任务(job)所属分组,该值必须和job中的group完全相同--><!--
        <job-group>sampleGroup</job-group>
        --><!--任务开始时间--><!--
        --><!--<start-time>2017-08-21T11:10:00+08:00</start-time>--><!--
        <misfire-instruction>SmartPolicy</misfire-instruction>
        --><!--任务执行次数  -1 为无限次执行--><!--
        <repeat-count>-1</repeat-count>
        --><!--任务触发间隔(毫秒)--><!--
        <repeat-interval>1000</repeat-interval>
        --><!--每3秒中执行一次--><!--
      </simple>-->
      <cron>
        <name>JobTrigger</name>
        <group>JobTriggers</group>
        <description>cron trigger</description>
        <job-name>SimpleJob</job-name>
        <job-group>sampleGroup</job-group>
        <!--<start-time>2015-11-26T22:19:00+08:00</start-time>-->
        <cron-expression>0/2 * * * * ?</cron-expression>
      </cron>
    </trigger>
  </schedule>
</job-scheduling-data>

配置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

main函数中进行调用

static void Main(string[] args)
        {
            StdSchedulerFactory std = new StdSchedulerFactory();
            IScheduler scheduler = std.GetScheduler();
            scheduler.Start();
        }

运行结果,每隔两秒输出语句

 

posted @ 2017-08-22 16:21  多1份努力~  阅读(376)  评论(0编辑  收藏  举报