gpt4 book ai didi

java - 恢复卡住的 quartz 作业

转载 作者:太空宇宙 更新时间:2023-11-04 11:42:52 24 4
gpt4 key购买 nike

我面临着一些被卡住的 quartz 工作,例如尝试永远连接。我正在使用一种方案,其想法是我可以检测这些卡住的作业,因为它们的触发器的 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 voidexecute 中的逻辑,而这些子类是当时的焦点。我更改了 public voidexecute 的代码,以便所有子类现在都可以轻松使用该逻辑。

@Override
public void execute(JobExecutionContext context)
throws JobExecutionException
{
runningThread = new AtomicReference<Thread>();
try {
this.runningThread.set(Thread.currentThread());
reallyExecute(context);
} finally {
runningThread.set(null);
}
}

关于java - 恢复卡住的 quartz 作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42626762/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com