2014-02-05 6 views
3

봄 일괄 작업을 시작하는 웹 서비스 메서드가 있습니다. 일괄 처리 컨트롤에서 예외가 발생하면 프로세서 처리 메서드가 돌아올 것입니다. 하지만 컨트롤러를 웹 서비스 메소드로 되돌려 놓아야합니다. 예외를 이메일로 보내야합니다.스프링 배치 프로세서 process() 메소드에서 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:"); 
    } 
} 

답변

4

나는이 웹 응용 프로그램에서 작업을 시작하는 데 사용할 startJob입니다, 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"); 
    } 
} 
+1

감사합니다. Karthik, 실제로 문제를 해결했습니다. getAllFailureExceptions()를 사용하여 모든 예외를 캡처하는 것을 알지 못했습니다. 방법. – Ayyappak

관련 문제