123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- using System;
- using Quartz;
- using Quartz.Impl;
- using Quartz.Impl.Matchers;
- using Quartz.Impl.Triggers;
- using Quartz.Spi;
- using System.Collections.Specialized;
- using System.Data;
- using System.Data.SqlClient;
- using System.Linq;
- using System.Threading.Tasks;
- using CoreEntity.TimeJob;
- using System.Collections.Generic;
- using Common.Wechat;
- using System.Threading;
- namespace CoreEntity.TimedTask
- {
- public class CancelPushRecordTimedJob
- {
- public const int HoursToCancel = Config.HoursToCancel;
- public class CancelJob : IJob
- {
- public CancelJob()
- {
- }
- public Task Execute(IJobExecutionContext context)
- {
- JobDataMap dataMap = context.JobDetail.JobDataMap;
- string RecordId = dataMap.GetString("RecordId");
- string result;
- CancelTask.CancelRecord(RecordId, out result );
- return Console.Out.WriteLineAsync("Greetings from CacncelJob!--"+ result);
- }
- }
- public static async void CancelPushRecord(string RecordId,DateTime start, int hour)
- {
- try
- {
- // Grab the Scheduler instance from the Factory
- NameValueCollection props = new NameValueCollection
- {
- { "quartz.serializer.type", "binary" }
- };
- StdSchedulerFactory factory = new StdSchedulerFactory(props);
- IScheduler scheduler = await factory.GetScheduler();
- // 启动任务调度器
- await scheduler.Start();
- // 定义一个 Job
- string key = "job_" + RecordId;
- IJobDetail job = JobBuilder.Create<CancelJob>()
- .WithIdentity(key, "CancelRecord")
- .UsingJobData("RecordId", RecordId)
- .Build();
- DateTime past = DateTime.Now;
- ISimpleTrigger trigger = (ISimpleTrigger)TriggerBuilder.Create()
- .WithIdentity("trigger_" + key) // 给任务一个名字
- //.StartAt(start) // 设置任务开始时间
- //.StartAt(past)
- //.StartNow()
- .StartAt(DateBuilder.FutureDate(hour, IntervalUnit.Hour))
- .ForJob(key, "CancelRecord") //给任务指定一个分组
- //.WithSimpleSchedule(x => x
- //.WithIntervalInHours(hour)
- //.WithIntervalInSeconds(1) //循环的时间 1秒1次
- //.RepeatForever()
- //.WithRepeatCount(0))
- .Build();
- ////每天9:30:00执行
- //CronTriggerImpl trigger = (CronTriggerImpl)TriggerBuilder.Create()
- // .WithIdentity("trigger1") // 给任务一个名字
- // .StartAt(DateTime.Now) // 设置任务开始时间
- // .ForJob("job1", "group1") //给任务指定一个分组
- // .WithCronSchedule(second + " " + minute + " " + hour + " * * ?")
- // .Build();
- //GroupMatcher<JobKey> groupMatcher = GroupMatcher<JobKey>.GroupEquals("CancelRecord");
- //IReadOnlyCollection<JobKey> set = await scheduler.GetJobKeys(groupMatcher);
- //if (set.Count > 0)
- //{
- // foreach (JobKey jobKey in set)
- // {
- // if (jobKey.Name == "job_" +RecordId)
- // {
- // bool delsuccess = await scheduler.DeleteJob(jobKey);
- // }
- // }
- //}
- // 等待执行任务
- await scheduler.ScheduleJob(job, trigger);
- // some sleep to show what's happening
- //await Task.Delay(TimeSpan.FromMilliseconds(2000));
- }
- catch (SchedulerException se)
- {
- await Console.Error.WriteLineAsync(se.ToString());
- }
- }
- }
- }
|