gpt4 book ai didi

java - 使用异步 TaskManager 处理作业/步骤异常

转载 作者:行者123 更新时间:2023-12-01 10:21:15 25 4
gpt4 key购买 nike

我找不到在异步上下文中处理 spring-batch 异常的正确方法。

当我将 ThreadPoolTask​​Manager 设置为我的 JobLauncher 时,不再记录真正的作业/步骤异常。相反,日志将类似于:

org.springframework.batch.core.JobInterruptedException: Job interrupted by step execution
at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:165)
at ...

我试图通过添加一个 JobExecutionListener 来解决这个问题,如下所示:

@Override
public void afterJob(JobExecution jobExecution) {
List<Throwable> jobExceptions = jobExecution.getFailureExceptions();
if (CollectionUtils.isNotEmpty(jobExceptions)) {
Throwable lastJobException = jobExceptions.get(jobExceptions.size() - 1);
LOGGER.error("Spring-Batch error at job level", lastJobException);
String lastJobExceptionMessage = ExceptionUtils.getRootCauseMessage(lastJobException);

// storing message in ExecutionContext for the batch-admin webapp
String message = "";
if (jobExecution.getExecutionContext().get(Consts.JOB_EXECUTION_MESSAGE_KEY) != null) {
message = jobExecution.getExecutionContext().getString(Consts.JOB_EXECUTION_MESSAGE_KEY);
}
message += "\n" + lastJobExceptionMessage;
jobExecution.getExecutionContext().put(Consts.JOB_EXECUTION_MESSAGE_KEY, message);
}

}

但我仍然以 JobInterruptedException 结束。有没有办法检索中断的初始原因(可能是读取器/处理器/写入器代码中的错误?

最佳答案

我认为你的诊断不正确。仅在 SimpleStepHandler 中抛出该异常并附带该错误消息:

if (currentStepExecution.getStatus() == BatchStatus.STOPPING
|| currentStepExecution.getStatus() == BatchStatus.STOPPED) {
// Ensure that the job gets the message that it is stopping
execution.setStatus(BatchStatus.STOPPING);
throw new JobInterruptedException("Job interrupted by step execution");
}

并且仅当该步骤本身没有抛出JobInterruptedException时。发生这种情况的最明显的情况是作业停止。请参阅this example ,其输出以

结尾
INFO: Executing step: [step1]Feb 24, 2016 1:25:02 PM org.springframework.batch.core.repository.support.SimpleJobRepository checkForInterruptionINFO: Parent JobExecution is stopped, so passing message on to StepExecutionFeb 24, 2016 1:25:02 PM org.springframework.batch.core.step.ThreadStepInterruptionPolicy isInterruptedINFO: Step interrupted through StepExecutionFeb 24, 2016 1:25:02 PM org.springframework.batch.core.step.AbstractStep executeINFO: Encountered interruption executing step step1 in job myJob : Job interrupted status detected.Feb 24, 2016 1:25:02 PM org.springframework.batch.core.repository.support.SimpleJobRepository checkForInterruptionINFO: Parent JobExecution is stopped, so passing message on to StepExecutionFeb 24, 2016 1:25:02 PM org.springframework.batch.core.job.AbstractJob executeINFO: Encountered interruption executing job: Job interrupted by step executionFeb 24, 2016 1:25:02 PM org.springframework.batch.core.launch.support.SimpleJobLauncher$1 runINFO: Job: [SimpleJob: [name=myJob]] completed with the following parameters: [{}] and the following status: [STOPPED]Status is: STOPPED

This other example表明使用线程池时抛出异常不会改变任何事情。最终输出为

INFO: Executing step: [step1]Feb 24, 2016 1:28:44 PM org.springframework.batch.core.step.AbstractStep executeSEVERE: Encountered an error executing step step1 in job myJobjava.lang.RuntimeException: My exception    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)(...)Feb 24, 2016 1:28:44 PM org.springframework.batch.core.launch.support.SimpleJobLauncher$1 runINFO: Job: [SimpleJob: [name=myJob]] completed with the following parameters: [{}] and the following status: [FAILED]Status is: FAILED, job execution id 0  #1 step1 FAILEDStep step1java.lang.RuntimeException: My exception    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)(...)

关于java - 使用异步 TaskManager 处理作业/步骤异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35600278/

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