您可以创建名为 JobBase 的新抽象类,例如实现 IJob 接口并插入抽象方法:
public abstract void ExecuteJob(IJobExecutionContext context);
在 JobBase 上,您可以像这样实现方法 Execute
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();
}
}
每个 Job 都会实现 JobExecute 方法。
public class TestJob :JobBase
{
private ILog logger;
public TeJob(ILog logger):base(logger)
{
}
public override ExecuteJob(IJobExecutionContext context)
{
}
}
假设使用一些工厂来创建作业
对于停止作业,您将调用方法scheduler.Interrupt(new JobKey(jobName));