1
我面臨一些石英作業卡住,例如試圖永遠連接。我正在使用一個方案,其中的想法是我可以檢測到這些卡住的工作,因爲他們的觸發器在過去有一個nextFiretime。然後,我嘗試通過中斷它們來阻止這些線程。但是,中斷似乎不起作用,線程仍然運行,而nextFiretime未更新。 如何正確中斷它們?恢復卡石英作業
工作代碼:
protected AtomicReference<Thread> runningThread = null;
/*
* (non-Javadoc)
*
* @see org.quartz.Job#execute(org.quartz.JobExecutionContext)
*/
@Override
public void execute(JobExecutionContext context)
throws JobExecutionException
{
runningThread = new AtomicReference<Thread>();
try {
this.runningThread.set(Thread.currentThread());
} finally {
runningThread.set(null);
}
}
/*
* (non-Javadoc)
*
* @see org.quartz.InterruptableJob#interrupt()
*/
@Override
public void interrupt() throws UnableToInterruptJobException
{
Thread thread = runningThread.getAndSet(null);
if (thread != null)
thread.interrupt();
}
打斷實際的jobscheduler代碼:
public int interruptLongRunningJobs(int ms) {
int jobsInterrupted = 0;
String jobsInterruptedList = "";
Date limitInThePast = new Date(System.currentTimeMillis() - ms);
Scheduler scheduler = this.getJobScheduler();
// All scheduled jobs
try {
for (String groupName : scheduler.getJobGroupNames()) {
for (JobKey jobKey : scheduler.getJobKeys(GroupMatcher.jobGroupEquals(groupName))) {
JobDetail jobDetail = scheduler.getJobDetail(jobKey);
final List<? extends Trigger> triggers = scheduler.getTriggersOfJob(jobKey);
Date nextFireTime = null;
if (triggers.size() > 0)
{
nextFireTime = triggers.get(0).getNextFireTime();
if(nextFireTime != null) {
if(nextFireTime.before(limitInThePast)) {
String jobString = jobDetail.getKey() + "@" + jobDetail.getJobClass().getSimpleName();
logger.debug("JobScheduler::interruptLongRunningJobs interrupting: " + jobString);
scheduler.interrupt(jobDetail.getKey());
if(!jobsInterruptedList.isEmpty()) {
jobsInterruptedList += ", ";
}
jobsInterruptedList += jobString;
++jobsInterrupted;
}
}
}
}
}
} catch (SchedulerException e) {
logger.debug("JobScheduler::interruptLongRunningJobs failed: " + e.getMessage());
}
if(jobsInterrupted>0) {
logger.debug("JobScheduler::interruptLongRunningJobs interrupted jobs#= " + jobsInterrupted);
emailSomething("JobScheduler::interruptLongRunningJobs interrupted jobs#= " + jobsInterrupted,
"These jobs have been interrupted and canceled because they exceeded the maximum running time as detected by triggers with nextFireTime in the past:\r\n" +
jobsInterruptedList
);
}
return jobsInterrupted;
}