Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am having the Web-service method to start spring batch job.If any exception occurred in spring batch processing control is coming back till processor process method. But i need the controller to came back to web-service method there i have to catch and code to email that exception.

Web-service method:

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

    }

Processor process method:

@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:");
  }
}
like image 686
Ayyappak Avatar asked Feb 05 '14 07:02

Ayyappak


People also ask

Can we skip processor in Spring Batch?

Using Custom SkipPolicy For that purpose, Spring Batch framework provides the SkipPolicy interface. We can then provide our own implementation of skip logic and plug it into our step definition.

How can I get JobParameters in Spring Batch processor?

@Configuration @EnableBatchProcessing public class BatchConfiguration { // read, write ,process and invoke job } JobParameters jobParameters = new JobParametersBuilder(). addString("fileName", "xxxx. txt"). toJobParameters(); stasrtjob = jobLauncher.

How does Spring Batch handle exceptions?

By default , if there's an uncaught exception when processing the job, spring batch will stop the job. If the job is restarted with the same job parameters, it will pick up where it left off. The way it knows where the job status is by checking the job repository where it saves all the spring batch job status.

What is JobExplorer in Spring Batch?

public interface JobExplorer. Entry point for browsing executions of running or historical jobs and steps. Since the data may be re-hydrated from persistent storage, it may not contain volatile fields that would have been present when the execution was active.


1 Answers

Here is startJob which I use the to start the job in web application, Tye to throw specific exception

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;
    }

If Item processor is as below

@Override
public KPData process(InputDetail inputData) throws MyException {
  try {
    business logic here
  }
  catch(Exception e) {
    throw new MyException("Some issue");
  }
}
like image 88
Karthik Prasad Avatar answered Sep 28 '22 02:09

Karthik Prasad