再用Quartz 做任务调度作业时,有以下步骤:
ISchedulerFactory schedFact = new StdSchedulerFactory();
IScheduler _sched;
_sched = schedFact.GetScheduler();
var triggerJobs = new Dictionary<IJobDetail, Quartz.Collection.ISet<ITrigger>>();
var jobDetail = JobBuilder.Create()
.WithIdentity("JobMonitorJob", "jobs")
.OfType(typeof(JobMonitorJob))
//.OfType(typeof(CommonJob ))
//.WithDescription("JobMonitorJob")
//.UsingJobData("type", "QuartzTest.JobMonitorJob")
//.UsingJobData("method", "Execute")
.Build();
var triggers = new Quartz.Collection.HashSet<ITrigger>();
var trigger = TriggerBuilder.Create()
.ForJob(jobDetail)
.StartAt((DateTimeOffset.Now.AddSeconds(10)))
//.WithSimpleSchedule(x => x.WithIntervalInSeconds(item.Schedule).RepeatForever())
.WithCronSchedule("0/5 * * * * ?")
.Build();
triggers.Add(trigger);
triggerJobs.Add(jobDetail, triggers);
_sched.ScheduleJobs(triggerJobs, false);
_sched.Start();
多任务调度加个中间件
public class CommonJob : IJob
{
private readonly ILog _logger = LogManager.GetLogger(typeof(CommonJob));
public void Execute(IJobExecutionContext context)
{
var map = context.JobDetail.JobDataMap;
var typeName = (string)map["type"];
var methodName = (string)map["method"];
try
{
var type = Type.GetType(typeName);
var method = type.GetMethod(methodName, Type.EmptyTypes);
method.Invoke(Activator.CreateInstance(type), null);
}
catch (Exception ex)
{
JobExecutionException e2 = new JobExecutionException(ex);
e2.RefireImmediately = true;
throw e2;
}
finally
{
GC.Collect();
}
}
}
public class JobMonitorJob :IJob
{
private readonly ILog _logger = LogManager.GetLogger(typeof(JobMonitorJob));
public void Execute(IJobExecutionContext context)
{
_logger.Debug("sdfasfdas");
}
}