一、在Quartz.NET中quartz.properties的配置文件,忽略不修改,考虑下面:
var props = new NameValueCollection { { "quartz.serializer.type", "binary" } };
二、IScheduler 、IJobDetail、ITrigger
StdSchedulerFactory schedFact = new StdSchedulerFactory(props); IScheduler scheduler = await schedFact.GetScheduler(); // await对应async
IJobDetail job1 = JobBuilder.Create<MyJob>() .WithIdentity(string.Format("myJob_{0}", item), string.Format("group_{0}", item)) .UsingJobData("jobSays", "Hello World!") // 测试用,多余参数 .UsingJobData("name", item) .Build(); job1.JobDataMap.Add("myKey", myObject); //scheduler.Context.Put("myKey",myObject);// 不要用这个,这是个坑,多任务调用,myKey只有一个值 ITrigger trigger1 = TriggerBuilder.Create() .WithIdentity(string.Format("触发器名称_{0}", item), string.Format("触发器组_{0}", item)) .StartNow() //现在开始 .WithSimpleSchedule(x => x //触发时间,5秒一次。 .WithIntervalInSeconds(5) .RepeatForever()) //不间断重复执行 .Build();
三、自定义Job
public class MyJob : IJob
public Task Execute(IJobExecutionContext context) { JobDataMap dataMap = context.JobDetail.JobDataMap; //string content = dataMap.GetString("jobSays"); string name = dataMap.GetString("name"); var myObject = dataMap.Get("myKey") as MyObject; //var myObject = (MyObject)context.Scheduler.Context.Get("myKey");// 这是个坑 // 测试信息打印 //Console.WriteLine("作业执行,jobSays:" + content); myObject?.AddTask(name).ConfigureAwait(false); return Task.FromResult(0); }