Quartz Scheduler作业自动终止

时间:2016-04-08 07:53:04

标签: quartz-scheduler

如何创建在给定时间后自动终止的Quartz Scheduler作业(如果正在运行的作业需要花费太多时间)?

1 个答案:

答案 0 :(得分:0)

要从作业内部停止作业,最简单的方法是在特定时间后抛出异常。例如:

public class MyJob : IJob
{
    Timer _t;

    public MyJob()
    {
        TimeSpan maxRunningTime = TimeSpan.FromMinutes(1);
        _t = new Timer(delegate { throw new JobExecutionException("took to long"); }, null, (int) maxRunningTime.TotalMilliseconds,
            -1);
    }

    public void Execute(IJobExecutionContext context)
    {
        // do your word
        // destroy T before leaving

        _t = null;
    }
}

希望有所帮助:)