quartz.net 何时使用 JobBuilder.Create<> 与 JobBuilder.CreateForAsync<>()
quartz.net when to use JobBuilder.Create<> vs JobBuilder.CreateForAsync<>()
我没有找到任何有用的文档来描述
之间的区别
JobBuilder.Create<MyJob> vs JobBuilder.CreateForAsync<MyJob>()
所以我假设如果 MyJob
做了一些 async
工作,我应该选择 CreateForAsync
?
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;
}
但是,你可以看到CreateForAsync
具体使用的例子显示here:
/// <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<> 的代码相似,但您可能需要在 运行 异步代码时使用它。
我没有找到任何有用的文档来描述
之间的区别JobBuilder.Create<MyJob> vs JobBuilder.CreateForAsync<MyJob>()
所以我假设如果 MyJob
做了一些 async
工作,我应该选择 CreateForAsync
?
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;
}
但是,你可以看到CreateForAsync
具体使用的例子显示here:
/// <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<> 的代码相似,但您可能需要在 运行 异步代码时使用它。