1、使用corn表达式
ITrigger trigger2 = TriggerBuilder.Create() .WithIdentity("mytrigger", "group1") .StartNow() .WithCronSchedule("/5 * * ? * *") //时间表达式,5秒一次 .Build();
2、传参数
IJobDetail job2 = JobBuilder.Create<DumbJob>() .WithIdentity("myJob", "group1") .UsingJobData("jobSays", "Hello World!") .Build();
3、接收参数
async Task IJob.Execute(IJobExecutionContext context) { JobDataMap dataMap = context.JobDetail.JobDataMap; string content = dataMap.GetString("jobSays"); await Console.Out.WriteLineAsync("作业执行,jobSays:" + content); }
完整代码:
public class DumbJob : IJob { /// <summary> /// context 可以获取当前Job的各种状态。 /// </summary> /// <param name="context"></param> async Task IJob.Execute(IJobExecutionContext context) { JobDataMap dataMap = context.JobDetail.JobDataMap; string content = dataMap.GetString("jobSays"); await Console.Out.WriteLineAsync("作业执行,jobSays:" + content); } } class Program { static void Main(string[] args) { IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler().Result; scheduler.Start(); //开启调度器 ITrigger trigger2 = TriggerBuilder.Create() .WithIdentity("mytrigger", "group1") .StartNow() .WithCronSchedule("/5 * * ? * *") //时间表达式,5秒一次 .Build(); IJobDetail job2 = JobBuilder.Create<DumbJob>() .WithIdentity("myJob", "group1") .UsingJobData("jobSays", "Hello World!") .Build(); scheduler.ScheduleJob(job2, trigger2); Console.Read(); } }