简体   繁体   中英

Unable to create job with trigger with Quartz.NET

I've got a .NET Core 3 worker service that uses Quartz.NET to schedule jobs. In this cut down example I have a single job with a single trigger. It doesn't seem to run and I don't get any exceptions.

  • How do I debug Quartz.NET and get a list of jobs and triggers?
  • Why isn't the job in the example below running?

I've tried creating the job and trigger within a group and without a group with no effect.

public class Worker : BackgroundService
{
    private readonly ILogger<Worker> _logger;
    private readonly ISchedulerFactory _schedulerFactory;

    public Worker(ILogger<Worker> logger, ISchedulerFactory schedulerFactory)
    {
        _logger = logger;
        _schedulerFactory = schedulerFactory;
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        NameValueCollection props = new NameValueCollection
        {
            { "quartz.serializer.type", "binary" }
        };

        StdSchedulerFactory factory = new StdSchedulerFactory(props);

        IScheduler schedulder = await factory.GetScheduler().ConfigureAwait(false);
        await schedulder.Start().ConfigureAwait(false);

        var pingJob = JobBuilder.Create<PingJob>()
            .WithIdentity("ping-job")
            .Build();

        ITrigger pingJob1Trigger = TriggerBuilder.Create()
            .WithIdentity("ping-job")
            .WithSimpleSchedule(x => x
                .WithInterval(TimeSpan.FromSeconds(1))
                .RepeatForever())
            .StartNow()
            .Build();

        await schedulder.ScheduleJob(pingJob, pingJob1Trigger, stoppingToken).ConfigureAwait(false);
    }
}

The job:

public class PingJob : IJob
{
    private readonly ILogger<PingJob> _logger;

    public PingJob(ILogger<PingJob> logger)
    {
        _logger = logger;
    }

    public async Task Execute(IJobExecutionContext context)
    {
        _logger.LogInformation("Ping");


        await Task.CompletedTask;
    }
}

Quartz will create jobs using the default parameterless constructor. Set a breakpoint in your current PingJob construcor, start the debugger and observe that the breakpoint won't be hit.

Here is the relevant part of the documentation which describes this behaviour:

When a trigger fires, the Job it is associated to is instantiated via the JobFactory configured on the Scheduler. The default JobFactory simply activates a new instance of the job class. You may want to create your own implementation of JobFactory to accomplish things such as having your application's IoC or DI container produce/initialize the job instance.

See the IJobFactory interface, and the associated Scheduler.SetJobFactory(fact) method.

Now add a parameterless constructor

public PingJob() 
{ 
}

set a breakpoint in it, start the debugger and it will be hit. You can also set a breakpoint in the Execute Method and it will be hit now also.

You can now replace the Execute method as follows and it will print Ping on the console:

public async Task Execute(IJobExecutionContext context)
{
    await Console.Out.WriteLineAsync("Ping");
}

How can we get your orginial code to work? As the documentation says we need to use JobFactory . See for example the stackoverflow question .net Core Quartz Dependency Injection

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