简体   繁体   中英

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

I didn't find any useful documentation that is describing the difference between

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

So I'm assuming that if MyJob does some async work that I should go with CreateForAsync ?

Quartz.Net doesn't have good documentation at all which leads me in one of the tasks to dive into the code to figure out exactly what is what.

I checked the source code and it seems that the code for the two methods is exactly the same:

    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;
    }

However, you can see an example of the specific use of CreateForAsync shown 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 -----------------");
    }
}

So even if the code for JobBuilder.Create<> and JobBuilder.CreateForAsync<> is similar but you may need to use it when running async code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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