繁体   English   中英

quartz.net 何时使用 JobBuilder.Create<> vs JobBuilder.CreateForAsync<>()

[英]quartz.net when to use JobBuilder.Create<> vs JobBuilder.CreateForAsync<>()

我没有找到任何有用的文档来描述两者之间的区别

JobBuilder.Create<MyJob> vs JobBuilder.CreateForAsync<MyJob>()

所以我假设如果MyJob做了一些async工作,我应该 go 和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的具体使用示例:

 /// <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<> 的代码相似,但您可能需要在运行异步代码时使用它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM