5

e.g. i have 3 Steps in Job (similar to Step1):

@Autowired
private StepBuilderFactory stepBuilderFactory;

@Bean
public Step step1() {
    return stepBuilderFactory
            .get("step1")
            .<String, String> chunk(1)
            .reader(reader())
            .processor(processor())
            .writer(writer())
            .build();
}

How go to Step 2 and 3 even after exception in Step 1? I mean configuration in Java.

Artur
  • 217
  • 1
  • 5
  • 12
  • May be you want to try `StepExecutionListener`. Its `afterStep` is useful in step flow handling. – Minh Aug 25 '16 at 03:56

3 Answers3

7

Here is an example on how to configure it when creating a flow. That should be similar to configure it directly with the job builder:

return new FlowBuilder<SimpleFlow>("name")
    .start(step1) //
    .next(step2).on(ExitStatus.FAILED.getExitCode()).to(errorStep)
    .from(step2).on(ALL_PATTERN).to(step3)
    .build();
Hansjoerg Wingeier
  • 4,274
  • 4
  • 17
  • 25
2

You can use Skip Listener

@Component
public class CustomSkipListener {

    @OnSkipInRead
    public void onSkipInRead(Throwable t) {
        System.out.println("From onSkipInRead -> " + t.getMessage());
    }

    @OnSkipInWrite
    public void onSkipInWrite(String item, Throwable t) {
        System.out.println("From onSkipInWrite: " + item + " -> " + t.getMessage());
    }

    @OnSkipInProcess
    public void onSkipInProcess(String item, Throwable t) {
        System.out.println("From onSkipInProcess: " + string + " -> " + t.getMessage());
    }
}

Then in your step

@Bean
public Step step1(CustomSkipListener customSkipListener) {
    return stepBuilderFactory
            .get("step1")
            .<String, String> chunk(1)
            .reader(reader())
            .processor(processor())
            .writer(writer())
            .faultTolerant()
            .skipLimit(10)
            .skip(RuntimeException.class)
            .listener(customSkipListener)
            .build();
}

Notice the CHAIN starting from .faultTolerant(). Adding the listener is not mandatory. If you add the listener you can handle the behaviour when skipping happens.

Some helpful links

http://docs.spring.io/spring-batch/apidocs/org/springframework/batch/core/SkipListener.html

http://docs.spring.io/spring-batch/reference/html/configureStep.html#configuringSkip

A0__oN
  • 8,740
  • 6
  • 40
  • 61
0

You can use StepBuilder.faultTolerant method and configuring xml in this fashion.

<batch:skippable-exception-classes>
                    <batch:include class="MyException"/>
</batch:skippable-exception-classes>

This would help in your case.

Please look at this : http://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/core/step/builder/SimpleStepBuilder.html#faultTolerant--

Rishi
  • 1,163
  • 7
  • 12
  • I have used: SkipPolicy and RetryPolicy but after exception Step execution is complete with 'COMPLETE' status. I expect: 1. Step1: when ex than exitStatus: FAILED, otherwise: COMPLETED 2. (no matter that an exception or not in Step1) Step2 3.(no matter that an exception or not in Step1, Step2) Step3` – Artur Aug 25 '16 at 00:18
  • Read this link :https://blog.codecentric.de/en/2012/03/transactions-in-spring-batch-part-3-skip-and-retry/ – Rishi Aug 25 '16 at 00:22