如何安排時間從執行任務開始時停止並終止一個作業 ? 例如,確定如果一小時後任務仍在工作,它將停止並移除。 (我並不想重複任務) 我在用java org.quartz.Scheduler和org.quartz.JobDetail使用如何確定在執行過程中停止org.quartz.JobDetail的時間?
千恩萬謝
如何安排時間從執行任務開始時停止並終止一個作業 ? 例如,確定如果一小時後任務仍在工作,它將停止並移除。 (我並不想重複任務) 我在用java org.quartz.Scheduler和org.quartz.JobDetail使用如何確定在執行過程中停止org.quartz.JobDetail的時間?
千恩萬謝
你可能要考慮的org.quartz.InterruptableJob接口(它擴展了Job)和調度器上的interrupt()方法。
如果您想要在工作線程池中的線程上實際調用Thread.interrupt(),您可能需要實現org.quartz.spi.ThreadPool。
我認爲如果你在作業本身編寫了這個功能,你可能會更容易。我這樣說的幾個原因:
If you change your class to implement StatefulJob instead of Job, Quartz will take care of this for you. From the StatefulJob javadoc:
stateful jobs are not allowed to execute concurrently, which means new triggers that occur before the completion of the execute(xx) method will be delayed.
StatefulJob extends Job and does not add any new methods, so all you need to do to get the behaviour you want is change this:
public class YourJob implements org.quartz.Job {
void execute(JobExecutionContext context) {/*implementation omitted*/}
}
To this:
public class YourJob implements org.quartz.StatefulJob {
void execute(JobExecutionContext context) {/*implementation omitted*/}
}
在開始工作之前如何確定任務的使用壽命,並且在到達停止時間的時刻不要調用方法? – cls
您可以隨時添加一個作業來殺死其他作業並相應地安排作業。 – Sathwick