Quartz.NET 2.2 试用

/*
 * 由SharpDevelop创建。
 * 用户: Changweihua
 * 日期: 2013/11/2
 * 时间: 15:32
 *
 * changweihua@outlook.com
 * http://www.cmono.net
 *
 */
using System;
using Quartz;
using Quartz.Impl;
using Quartz.Impl.Triggers;
using System.IO;
namespace QuartzSample
{
    class Program
    {
        public static void Main(string[] args)
        {
                                                                 
            ISchedulerFactory factory = new StdSchedulerFactory();
                                                                 
            IScheduler scheduler = factory.GetScheduler();
                                                                 
            //手动添加 Job
            IJobDetail detail = new JobDetailImpl("HelloWorld2", "HelloGroup2", typeof(HelloWorldJob2), true, false);
            ICronTrigger trigger =  new CronTriggerImpl("HelloWorld2", "HelloGroup2", "0/6 * * * * ?");
            scheduler.ScheduleJob(detail, trigger);
                                                                 
            scheduler.Start();
                                                                 
            if(scheduler.IsStarted)
            {
                if(scheduler.CheckExists(new JobKey("HelloWorld2", "HelloGroup2")))
                {
                                                                         
                    Console.WriteLine("包含该任务");
                }
                else
                {
                    Console.WriteLine("不包含该任务");
                }
            }
                                                                 
            Console.ReadKey(true);
        }
    }
                                                         
    public class HelloWorldJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            String  path = AppDomain.CurrentDomain.BaseDirectory+"Job\\";
                                                                 
            if(!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
                                                                 
            File.AppendAllText(path+"job1.txt", DateTime.Now+Environment.NewLine);
                                                                 
        }
    }
                                                         
    public class HelloWorldJob2 : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            String  path = AppDomain.CurrentDomain.BaseDirectory+"Job\\";
                                                                 
            if(!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
                                                                 
            File.AppendAllText(path+"job2.txt", DateTime.Now+Environment.NewLine);
                                                                 
        }
    }
                                                         
}

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

quartz_jobs.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>HelloWorldJob</name>
        <group>sampleGroup</group>
        <description>Sample job for Quartz Server</description>
        <job-type>QuartzSample.HelloWorldJob, QuartzSample</job-type>
        <durable>true</durable>
        <recover>false</recover>
    </job>
    <trigger>
      <cron>
        <name>sampleSimpleTrigger</name>
        <group>sampleSimpleGroup</group>
        <description>Simple trigger to simply fire sample job</description>
        <job-name>HelloWorldJob</job-name>
        <job-group>sampleGroup</job-group>
        <cron-expression>0/3 * * * * ?</cron-expression> <!--每3秒中执行一次-->
      </cron>
    </trigger>
  </schedule>
</job-scheduling-data>


知识共享许可协议
《Quartz.NET 2.2 试用》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     143.38ms