QuartzNet使用
quartz.config # You can configure your scheduler in either <quartz> configuration section # or in quartz properties file # Configuration section has precedence quartz.scheduler.instanceName = RemoteServer # 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.Plugins 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.scheduler.exporter.rejectRemoteRequests = true
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> <!--cancelOrderJob测试 任务配置--> <job> <name>cancelOrderJob</name> <group>ABC.OMS.Order</group> <description>取消订单计划任务</description> <job-type>QuartzTest.Jobs.CancelOrderJob,QuartzTest.Jobs</job-type> <durable>true</durable> <recover>false</recover> </job> <trigger> <cron> <name>cancelOrderTrigger</name> <group>ABC.OMS.Order</group> <job-name>cancelOrderJob</job-name> <job-group>ABC.OMS.Order</job-group> <start-time>2015-01-22T00:00:00+08:00</start-time> <cron-expression>0/2 * * * * ? </cron-expression> </cron> </trigger> </schedule> </job-scheduling-data>
class Program { static void Main(string[] args) { // var properties = new NameValueCollection(); // properties["quartz.scheduler.instanceName"] = "RemoteServer"; // properties["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz"; // properties["quartz.threadPool.threadCount"] = "5"; // properties["quartz.threadPool.threadPriority"] = "Normal"; // properties["quartz.scheduler.exporter.type"] = "Quartz.Simpl.RemotingSchedulerExporter, Quartz"; // properties["quartz.scheduler.exporter.port"] = "555"; // properties["quartz.scheduler.exporter.bindName"] = "QuartzScheduler"; // properties["quartz.scheduler.exporter.channelType"] = "tcp"; // properties["quartz.scheduler.exporter.channelName"] = "httpQuartz"; // properties["quartz.scheduler.exporter.rejectRemoteRequests"] = "true"; //ISchedulerFactory schedFact = new StdSchedulerFactory(properties); ISchedulerFactory schedFact = new StdSchedulerFactory(); IScheduler sched = schedFact.GetScheduler().Result; sched.Start(); IJobDetail cancelOrderJob = JobBuilder.Create<CancelOrderJob>() .WithIdentity("cancelOrderJob", "ABC.OMS.Order") .Build(); ITrigger cancelOrderTrigger = TriggerBuilder.Create() .WithIdentity("cancelOrderTrigger", "ABC.OMS.Order") .StartNow() .WithSimpleSchedule(x => x .WithIntervalInSeconds(3) .RepeatForever()) .Build(); IJobDetail job1 = JobBuilder.Create<PaymentConfirmedJod>() .WithIdentity("PaymentConfirmedJod", "group1") .Build(); ITrigger trigger1 = TriggerBuilder.Create() .WithIdentity("PaymentConfirmedTrigger", "group1") .StartNow() .WithSimpleSchedule(x => x .WithIntervalInSeconds(2) .RepeatForever()) .Build(); sched.ScheduleJob(cancelOrderJob, cancelOrderTrigger); sched.ScheduleJob(job1, trigger1); System.Console.ReadKey(); } }
namespace QuartzTest.Jobs { [DisallowConcurrentExecution] //设置JOB不允许并行执行 public class CancelOrderJob : IJob { System.Threading.Tasks.Task IJob.Execute(IJobExecutionContext context) { System.Console.WriteLine(string.Format("[{0}]订单已取消", DateTime.Now)); Thread.Sleep(5000); return System.Threading.Tasks.Task.CompletedTask; } } }