恢复卡石英作业
问题描述:
我面临一些石英作业卡住,例如试图永远连接。我正在使用一个方案,其中的想法是我可以检测到这些卡住的工作,因为他们的触发器在过去有一个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;
}
答
问题是我实现(复制粘贴)的逻辑public void execute
只在某些子类中,这是当时的焦点。我更改了public void execute
的代码,以便现在所有子类都可以轻松使用逻辑。
@Override
public void execute(JobExecutionContext context)
throws JobExecutionException
{
runningThread = new AtomicReference<Thread>();
try {
this.runningThread.set(Thread.currentThread());
reallyExecute(context);
} finally {
runningThread.set(null);
}
}