gpt4 book ai didi

java - 如何从 spring 批处理器 process() 方法向 Spring 批处理作业启动方法抛出异常?

转载 作者:搜寻专家 更新时间:2023-11-01 02:26:31 24 4
gpt4 key购买 nike

我正在使用 Web 服务方法来启动 spring 批处理作业。如果在 spring 批处理控制中发生任何异常,则会返回到处理器处理方法。但是我需要 Controller 返回到 web 服务方法,我必须在那里捕获并编写代码以通过电子邮件发送该异常。

网络服务方法:

public void processInputFiles() throws ServiceFault {

String[] springConfig = { CONTEXT_FILE_NAME };

ApplicationContext context = new ClassPathXmlApplicationContext(springConfig);
try {
setClientInfo();

JobLauncher jobLauncher = (JobLauncher) context.getBean(JOB_LAUNCHER);
Job job = (Job) context.getBean(REMITTANCE_JOB);

jobLauncher.run(job, new JobParameters());
}catch (Exception e) {
String errorMessage = "LockboxService exception:: Could not process Remittance(CSV) files";
final Message message = MessageFactory.createErrorMessage(MyService.class, errorMessage, e);
ErrorSenderFactory.getInstance().send(message, new Instruction[] { Instruction.ERROR_EMAIL });

}

处理器处理方法:

@Override
public Transmission process(InputDetail remrow) throws ServiceException {
try {
business logic here
}
catch(Exception e) {
throw new Exception("Unable to process row having the int number:");
}
}

最佳答案

这是 startJob,我用它来启动 Web 应用程序中的作业,Tye 抛出特定的异常

public boolean StartJob()
throws MyException{



try {

final JobParameters jobParameters = new JobParametersBuilder()
.addLong("time", System.nanoTime())
.addString("file", jobInputFolder.getAbsolutePath())
.toJobParameters();

final JobExecution execution = jobLauncher.run(job,
jobParameters);
final ExitStatus status = execution.getExitStatus();

if (ExitStatus.COMPLETED.getExitCode().equals(
status.getExitCode())) {
result = true;
} else {
final List<Throwable> exceptions = execution
.getAllFailureExceptions();
for (final Throwable throwable : exceptions) {

if (throwable instanceof MyException) {
throw (MyException) throwable;
}
if (throwable instanceof FlatFileParseException) {

Throwable rootException = throwable.getCause();
if (rootException instanceof IncorrectTokenCountException) {

throw new MyException(logMessage, errorCode);
}
if (rootException instanceof BindException) {
BindException bindException = (BindException) rootException;
final FieldError fieldError = bindException
.getFieldError();
final String field = fieldError.getField();

throw new MyException(logMessage, errorCode);
}
}

}
}
}
} catch (JobExecutionAlreadyRunningException ex) {

} catch (JobRestartException ex) {

} catch (JobInstanceAlreadyCompleteException ex) {

} catch (JobParametersInvalidException ex) {

} catch (IOException ex) {

} finally {

}

return result;
}

如果Item处理器如下

@Override
public KPData process(InputDetail inputData) throws MyException {
try {
business logic here
}
catch(Exception e) {
throw new MyException("Some issue");
}
}

关于java - 如何从 spring 批处理器 process() 方法向 Spring 批处理作业启动方法抛出异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21571225/

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