Handle JobExecutionException in Quartz.net

后端 未结 3 1644
無奈伤痛
無奈伤痛 2021-02-12 13:34

Probably a stupid question... but here goes anyway...

I have set up quartz, and can schedule jobs, and I can confirm that jobs (implementing the IJob interface) are work

3条回答
  •  终归单人心
    2021-02-12 13:49

    I solved this problem by using a base class to catch all the exceptions:

    public abstract class JobBase : IJob
    {
        protected JobBase()
        {
        }
    
        public abstract void ExecuteJob(JobExecutionContext context);
    
        public void Execute(JobExecutionContext context)
        {
            string logSource = context.JobDetail.FullName;
    
            try
            {
                ExecuteJob(context);
            }
            catch (Exception e)
            {
               // Log exception
            }
        }
    }
    

    Your Job class should like this:

    public class SomeJob : JobBase
    {
        public SomeJob()
        {
        }
    
        public override void ExecuteJob(JobExecutionContext context)
        {
            // Do the actual job here
        }
    }
    

提交回复
热议问题