Spring Batch - abstract Step definition in Java configuration?

user1660256

My Spring Batch Job configuration has 5 steps, all of which are identical except for the reader. Is there a way I can abstract out all of the other parts of the step into a "parent" step, so that I don't need to repeat everything? I know this can be done in XML, but I can't figure out the java equivalent.

Here's one of the steps:

public Step quarterlyStep(FileIngestErrorListener listener, ItemReader<DistributionItem> quarterlyReader) {
    return stepBuilderFactory.get("quarterlyStep")
            .<DistributionItem,DistributionItem>chunk(10)
            .reader(quarterlyReader)  // The only thing that changes among 5 different steps
                .listener(listener.asReadListener())
            .processor(processor())
                .listener(listener.asProcessListener())
            .writer(writer())
                .listener(listener.asWriteListener())
            .faultTolerant()
            .skip(ValidationException.class)
            .skip(ExcelFileParseException.class)
            .build();
}

Here's the definition of one of the readers:

@Bean
@JobScope
public PoiItemReader<DistributionItem> yearEndReader(@Value("#{jobExecutionContext['filename']}") String filename) {
    PoiItemReader<PortfolioFundsDistributionItem> reader = new PoiItemReader<>();
    reader.setLinesToSkip(1);
    reader.setRowMapper(yearEndRowMapper());
    reader.setResource(new FileSystemResource(filename));
    return reader;
}
Mahmoud Ben Hassine

You can do something like:

private StepBuilderFactory stepBuilderFactory;

private SimpleStepBuilder<Integer, Integer> createBaseStep(String stepName) {
    return stepBuilderFactory.get(stepName)
            .<Integer, Integer>chunk(5)
            .processor(itemProcessor())
            .writer(itemWriter());
}

@Bean
public Step step1(ItemReader<Integer> itemReader) {
    return createBaseStep("step1")
            .reader(itemReader)
            .build();
}

@Bean
public Step step2(ItemReader<Integer> itemReader) {
    return createBaseStep("step2")
            .reader(itemReader)
            .build();
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Partial definition of generic abstract function

分類Dev

How to set spring.main.allow-bean-definition-overriding to true in a Spring boot 2.1.0 starter configuration

分類Dev

Spring batch flow declaration using java config

分類Dev

How can I know the total number of chunks/items to be processed at the beginning of a Spring Batch Step?

分類Dev

Spring Batch:scope( "step")が失敗しました

分類Dev

How to access execution context from a Spring Batch Step? Error: No context holder available for job scope

分類Dev

Translate Spring XML configuration to Java config

分類Dev

spring security 4.0.0 java configuration with no xml

分類Dev

Implementing a composition relationship in an abstract class definition

分類Dev

Spring rest MultiPart file upload with Java configuration without Spring boot

分類Dev

複数のSpring Boot Batch Configurationクラスを持つ方法

分類Dev

Java constructor of an abstract class

分類Dev

Use of an abstract class in Java

分類Dev

Overriding beans in Java-based spring configuration hierarchy

分類Dev

Overriding beans in Java-based spring configuration hierarchy

分類Dev

"Step"スコープBeanのJUnitテストの記述-スコープ名 "step"のスコープが登録されていません(Spring Batch 3.0.10)

分類Dev

Cucumber is unable to find the Step Definition even it have proper Stepdefinition designed

分類Dev

Getting undefined step definition warning in Cucumber Protractor setup

分類Dev

Java Constants definition style

分類Dev

Spring Batch + Spring Boot + Couchbase

分類Dev

CallableTaskletAdapter Spring Batch

分類Dev

Spring Batch Writer

分類Dev

Spring Batch FlatFile Formatting

分類Dev

Spring Batch MultiLineItemReader with MultiResourcePartitioner

分類Dev

Transaction management with Spring Batch

分類Dev

Spring Batch : custom ItemReader

分類Dev

Different implementations for abstract class in Java

分類Dev

Java constructor of a subclass of an abstract class

分類Dev

Spring Boot + Spring Batch + Spring JPA

Related 関連記事

  1. 1

    Partial definition of generic abstract function

  2. 2

    How to set spring.main.allow-bean-definition-overriding to true in a Spring boot 2.1.0 starter configuration

  3. 3

    Spring batch flow declaration using java config

  4. 4

    How can I know the total number of chunks/items to be processed at the beginning of a Spring Batch Step?

  5. 5

    Spring Batch:scope( "step")が失敗しました

  6. 6

    How to access execution context from a Spring Batch Step? Error: No context holder available for job scope

  7. 7

    Translate Spring XML configuration to Java config

  8. 8

    spring security 4.0.0 java configuration with no xml

  9. 9

    Implementing a composition relationship in an abstract class definition

  10. 10

    Spring rest MultiPart file upload with Java configuration without Spring boot

  11. 11

    複数のSpring Boot Batch Configurationクラスを持つ方法

  12. 12

    Java constructor of an abstract class

  13. 13

    Use of an abstract class in Java

  14. 14

    Overriding beans in Java-based spring configuration hierarchy

  15. 15

    Overriding beans in Java-based spring configuration hierarchy

  16. 16

    "Step"スコープBeanのJUnitテストの記述-スコープ名 "step"のスコープが登録されていません(Spring Batch 3.0.10)

  17. 17

    Cucumber is unable to find the Step Definition even it have proper Stepdefinition designed

  18. 18

    Getting undefined step definition warning in Cucumber Protractor setup

  19. 19

    Java Constants definition style

  20. 20

    Spring Batch + Spring Boot + Couchbase

  21. 21

    CallableTaskletAdapter Spring Batch

  22. 22

    Spring Batch Writer

  23. 23

    Spring Batch FlatFile Formatting

  24. 24

    Spring Batch MultiLineItemReader with MultiResourcePartitioner

  25. 25

    Transaction management with Spring Batch

  26. 26

    Spring Batch : custom ItemReader

  27. 27

    Different implementations for abstract class in Java

  28. 28

    Java constructor of a subclass of an abstract class

  29. 29

    Spring Boot + Spring Batch + Spring JPA

ホットタグ

アーカイブ