繁体   English   中英

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

[英]How to throw exception from spring batch processor process() method to Spring batch job started method?

我正在使用Web服务方法来启动spring批处理作业。如果在Spring批处理控件中发生任何异常,则会返回处理器处理方法。 但是我需要控制器回到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;
    }

如果物品处理器如下

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM