Is it possible to kill a current running Quartz Job?

前端 未结 3 809
梦谈多话
梦谈多话 2021-01-04 05:48

I remember that we cannot kill the current running Quartz Job but we can interrupt and have a boolean check wherever is necessary whether we need to proceed further with the

3条回答
  •  情书的邮戳
    2021-01-04 06:10

    you can create new abstract class called JobBase for example that implements IJob interface and insert abstract method: public abstract void ExecuteJob(IJobExecutionContext context);

    On JobBase you can implements method Execute like this

    public abstract class JobBase : IJob,IInterruptableJob
    {
         private Thread currentThread;
         private ILog logger;
         public JobBase(ILog logger)
         {
            this.logger=logger;
         }
            public void Execute(IJobExecutionContext context)
            {
    
                var thread = new Thread(()=> 
                {
                    try
                    {
                        this.ExecuteJob(context);
                    }
                    catch(Exception ex)
                    {
                        this.logger.ErrorFormat("Unhandled exception {0}",ex.ToString());
    
                    }
                });
    
                thread.Start();
                this.currentThread = thread;
                this.currentThread.Join();  
            }
            public abstract void ExecuteJob(IJobExecutionContext context);
    
            public void Interrupt()
            {   
                currentThread.Abort();
            }
    }
    

    Each Job will implements JobExecute method.

      public class TestJob :JobBase
    {
        private ILog logger;
        public TeJob(ILog logger):base(logger)
        {
        }
    
        public override ExecuteJob(IJobExecutionContext context)
        {
        }
    }
    

    Assumes that use some factory for creating a Job

    For Stopping a Job you will call method scheduler.Interrupt(new JobKey(jobName));

提交回复
热议问题