복붙노트

[SPRING] 스프링 배치 프로세서 프로세스 () 메소드에서 Spring 배치 작업 시작 메소드로 예외를 던지려면?

SPRING

스프링 배치 프로세서 프로세스 () 메소드에서 Spring 배치 작업 시작 메소드로 예외를 던지려면?

봄 일괄 처리 작업을 시작하려면 웹 서비스 메서드가 필요합니다. 일괄 처리 컨트롤에서 예외가 발생하면 프로세서 처리 메서드까지 돌아오고 있습니다. 하지만 나는 그 예외를 이메일로 보내기 위해 코드를 잡아야하고 코드를 작성해야하는 웹 서비스 메소드로 돌아온 컨트롤러가 필요하다.

웹 서비스 방법 :

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:");
  }
}

해결법

  1. ==============================

    1.다음은 웹 애플리케이션에서 작업을 시작하는 데 사용하는 startJob입니다. 특정 예외를 발생시키는 유형

    다음은 웹 애플리케이션에서 작업을 시작하는 데 사용하는 startJob입니다. 특정 예외를 발생시키는 유형

    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");
      }
    }
    
  2. from https://stackoverflow.com/questions/21571225/how-to-throw-exception-from-spring-batch-processor-process-method-to-spring-ba by cc-by-sa and MIT license