Table of Contents

Interface IJobFactory

Namespace
Quartz.Extensibility
Assembly
Quartz.dll

A JobFactory is responsible for producing instances of IJob classes.

public interface IJobFactory

Remarks

This interface may be of use to those wishing to have their application produce IJob instances via some special mechanism, such as to give the opportunity for dependency injection.

Methods

CreateJob(TriggerFiredBundle, IScheduler, CancellationToken)

Called by the scheduler at the time of the trigger firing, in order to produce a IJob instance on which to call Execute.

ValueTask<JobScope> CreateJob(TriggerFiredBundle bundle, IScheduler scheduler, CancellationToken cancellationToken = default)

Parameters

bundle TriggerFiredBundle

The TriggerFiredBundle from which the IJobDetail and other info relating to the trigger firing can be obtained.

scheduler IScheduler

a handle to the scheduler that is about to execute the job

cancellationToken CancellationToken

The cancellation instruction.

Returns

ValueTask<JobScope>

The newly instantiated job, together with any per-fire state the factory wants handed back to ReturnJob(JobScope, CancellationToken).

Remarks

It should be extremely rare for this method to throw an exception - basically only the case where there is no way at all to instantiate and prepare the Job for execution. When the exception is thrown, the Scheduler will move all triggers associated with the Job into the Error state, which will require human intervention (e.g. an application restart after fixing whatever configuration problem led to the issue with instantiating the Job).

Implementations may perform asynchronous work before returning the job instance, but prefer to keep a synchronous body synchronous. An async method restores the caller's ExecutionContext when its synchronous part returns, so any AsyncLocal<T> the factory sets while building the job is discarded instead of reaching Execute(IJobExecutionContext, CancellationToken) (#1528). Return a completed ValueTask<TResult> when nothing needs awaiting.

Exceptions

SchedulerException

The job could not be instantiated.

ReturnJob(JobScope, CancellationToken)

Allows the job factory to destroy/cleanup the job once it has finished executing.

ValueTask ReturnJob(JobScope scope, CancellationToken cancellationToken = default)

Parameters

scope JobScope

The scope returned by CreateJob(TriggerFiredBundle, IScheduler, CancellationToken), carrying the job and its state.

cancellationToken CancellationToken

The cancellation instruction.

Returns

ValueTask

Remarks

The scheduler calls this for every scope CreateJob(TriggerFiredBundle, IScheduler, CancellationToken) returned, whether the job succeeded, failed or was vetoed. The scheduler does not call it when CreateJob(TriggerFiredBundle, IScheduler, CancellationToken) itself throws, so a factory that has already allocated by the time it fails is responsible for its own cleanup on that path — the factories shipped with Quartz do that by calling this method themselves before rethrowing, so an override may see a job that never executed.

See Also