我没有找到任何有用的文档来描述两者之间的区别
JobBuilder.Create<MyJob> vs JobBuilder.CreateForAsync<MyJob>()
所以我假设如果MyJob
做了一些async
工作,我应该使用CreateForAsync
发布于 2020-04-30 21:50:10
Quartz.Net根本没有很好的文档,这导致我进入了一个任务,深入到代码中去弄清楚什么是什么。
我检查了源代码,两个方法的代码似乎完全相同:
public static JobBuilder Create<T>() where T : IJob
{
JobBuilder b = new JobBuilder();
b.OfType(typeof(T));
return b;
}
public static JobBuilder CreateForAsync<T>() where T : IJob
{
JobBuilder b = new JobBuilder();
b.OfType(typeof(T));
return b;
}
但是,您可以查看显示为here的CreateForAsync
的具体用法示例
/// <summary>
/// This example will show how to run asynchronous jobs.
/// </summary>
/// <author>Marko Lahma</author>
public class RunningAsynchronousJobsExample : IExample
{
public virtual async Task Run()
{
ILog log = LogProvider.GetLogger(typeof(RunningAsynchronousJobsExample));
ISchedulerFactory sf = new StdSchedulerFactory();
IScheduler sched = await sf.GetScheduler();
log.Info("------- Initialization Complete -----------");
log.Info("------- Scheduling Jobs -------------------");
IJobDetail job = JobBuilder
.CreateForAsync<AsyncJob>()
.WithIdentity("asyncJob")
.Build();
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("triggerForAsyncJob")
.StartAt(DateTimeOffset.UtcNow.AddSeconds(1))
.WithSimpleSchedule(x => x.WithIntervalInSeconds(20).RepeatForever())
.Build();
await sched.ScheduleJob(job, trigger);
log.Info("------- Starting Scheduler ----------------");
// start the schedule
await sched.Start();
log.Info("------- Started Scheduler -----------------");
await Task.Delay(TimeSpan.FromSeconds(5));
log.Info("------- Cancelling job via scheduler.Interrupt() -----------------");
await sched.Interrupt(job.Key);
log.Info("------- Waiting five minutes... -----------");
// wait five minutes to give our job a chance to run
await Task.Delay(TimeSpan.FromMinutes(5));
// shut down the scheduler
log.Info("------- Shutting Down ---------------------");
await sched.Shutdown(true);
log.Info("------- Shutdown Complete -----------------");
}
}
因此,即使JobBuilder.Create<>和JobBuilder.CreateForAsync<>的代码相似,但您可能需要在运行异步代码时使用它。
https://stackoverflow.com/questions/61524653
复制相似问题