This version is still in development and is not considered stable yet. For the latest stable version, please use spring-cloud-task 3.1.3!spring-doc.cn

Single Step Batch Job Starter

This section goes into how to develop a Spring Batch Job with a single Step by using the starter included in Spring Cloud Task. This starter lets you use configuration to define an ItemReader, an ItemWriter, or a full single-step Spring Batch Job. For more about Spring Batch and its capabilities, see the Spring Batch documentation.spring-doc.cn

To obtain the starter for Maven, add the following to your build:spring-doc.cn

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-single-step-batch-job</artifactId>
    <version>2.3.0</version>
</dependency>

To obtain the starter for Gradle, add the following to your build:spring-doc.cn

compile "org.springframework.cloud:spring-cloud-starter-single-step-batch-job:2.3.0"

Defining a Job

You can use the starter to define as little as an ItemReader or an ItemWriter or as much as a full Job. In this section, we define which properties are required to be defined to configure a Job.spring-doc.cn

Properties

To begin, the starter provides a set of properties that let you configure the basics of a Job with one Step:spring-doc.cn

Table 1. Job Properties
Property Type Default Value Description

spring.batch.job.jobNamespring-doc.cn

Stringspring-doc.cn

nullspring-doc.cn

The name of the job.spring-doc.cn

spring.batch.job.stepNamespring-doc.cn

Stringspring-doc.cn

nullspring-doc.cn

The name of the step.spring-doc.cn

spring.batch.job.chunkSizespring-doc.cn

Integerspring-doc.cn

nullspring-doc.cn

The number of items to be processed per transaction.spring-doc.cn

With the above properties configured, you have a job with a single, chunk-based step. This chunk-based step reads, processes, and writes Map<String, Object> instances as the items. However, the step does not yet do anything. You need to configure an ItemReader, an optional ItemProcessor, and an ItemWriter to give it something to do. To configure one of these, you can either use properties and configure one of the options that has provided autoconfiguration or you can configure your own with the standard Spring configuration mechanisms.spring-doc.cn

If you configure your own, the input and output types must match the others in the step. The ItemReader implementations and ItemWriter implementations in this starter all use a Map<String, Object> as the input and the output item.

Autoconfiguration for ItemReader Implementations

This starter provides autoconfiguration for four different ItemReader implementations: AmqpItemReader, FlatFileItemReader, JdbcCursorItemReader, and KafkaItemReader. In this section, we outline how to configure each of these by using the provided autoconfiguration.spring-doc.cn

AmqpItemReader

You can read from a queue or topic with AMQP by using the AmqpItemReader. The autoconfiguration for this ItemReader implementation is dependent upon two sets of configuration. The first is the configuration of an AmqpTemplate. You can either configure this yourself or use the autoconfiguration provided by Spring Boot. See the Spring Boot AMQP documentation. Once you have configured the AmqpTemplate, you can enable the batch capabilities to support it by setting the following properties:spring-doc.cn

Table 2. AmqpItemReader Properties
Property Type Default Value Description

spring.batch.job.amqpitemreader.enabledspring-doc.cn

booleanspring-doc.cn

falsespring-doc.cn

If true, the autoconfiguration will execute.spring-doc.cn

spring.batch.job.amqpitemreader.jsonConverterEnabledspring-doc.cn

booleanspring-doc.cn

truespring-doc.cn

Indicates if the Jackson2JsonMessageConverter should be registered to parse messages.spring-doc.cn

For more information, see the AmqpItemReader documentation.spring-doc.cn

FlatFileItemReader

FlatFileItemReader lets you read from flat files (such as CSVs and other file formats). To read from a file, you can provide some components yourself through normal Spring configuration (LineTokenizer, RecordSeparatorPolicy, FieldSetMapper, LineMapper, or SkippedLinesCallback). You can also use the following properties to configure the reader:spring-doc.cn

Table 3. FlatFileItemReader Properties
Property Type Default Value Description

spring.batch.job.flatfileitemreader.saveStatespring-doc.cn

booleanspring-doc.cn

truespring-doc.cn

Determines if the state should be saved for restarts.spring-doc.cn

spring.batch.job.flatfileitemreader.namespring-doc.cn

Stringspring-doc.cn

nullspring-doc.cn

Name used to provide unique keys in the ExecutionContext.spring-doc.cn

spring.batch.job.flatfileitemreader.maxItemcountspring-doc.cn

intspring-doc.cn

Integer.MAX_VALUEspring-doc.cn

Maximum number of items to be read from the file.spring-doc.cn

spring.batch.job.flatfileitemreader.currentItemCountspring-doc.cn

intspring-doc.cn

0spring-doc.cn

Number of items that have already been read. Used on restarts.spring-doc.cn

spring.batch.job.flatfileitemreader.commentsspring-doc.cn

List<String>spring-doc.cn

empty Listspring-doc.cn

A list of Strings that indicate commented lines (lines to be ignored) in the file.spring-doc.cn

spring.batch.job.flatfileitemreader.resourcespring-doc.cn

Resourcespring-doc.cn

nullspring-doc.cn

The resource to be read.spring-doc.cn

spring.batch.job.flatfileitemreader.strictspring-doc.cn

booleanspring-doc.cn

truespring-doc.cn

If set to true, the reader throws an exception if the resource is not found.spring-doc.cn

spring.batch.job.flatfileitemreader.encodingspring-doc.cn

Stringspring-doc.cn

FlatFileItemReader.DEFAULT_CHARSETspring-doc.cn

Encoding to be used when reading the file.spring-doc.cn

spring.batch.job.flatfileitemreader.linesToSkipspring-doc.cn

intspring-doc.cn

0spring-doc.cn

Indicates the number of lines to skip at the start of a file.spring-doc.cn

spring.batch.job.flatfileitemreader.delimitedspring-doc.cn

booleanspring-doc.cn

falsespring-doc.cn

Indicates whether the file is a delimited file (CSV and other formats). Only one of this property or spring.batch.job.flatfileitemreader.fixedLength can be true at the same time.spring-doc.cn

spring.batch.job.flatfileitemreader.delimiterspring-doc.cn

Stringspring-doc.cn

DelimitedLineTokenizer.DELIMITER_COMMAspring-doc.cn

If reading a delimited file, indicates the delimiter to parse on.spring-doc.cn

spring.batch.job.flatfileitemreader.quoteCharacterspring-doc.cn

charspring-doc.cn

DelimitedLineTokenizer.DEFAULT_QUOTE_CHARACTERspring-doc.cn

Used to determine the character used to quote values.spring-doc.cn

spring.batch.job.flatfileitemreader.includedFieldsspring-doc.cn

List<Integer>spring-doc.cn

empty listspring-doc.cn

A list of indices to determine which fields in a record to include in the item.spring-doc.cn

spring.batch.job.flatfileitemreader.fixedLengthspring-doc.cn

booleanspring-doc.cn

falsespring-doc.cn

Indicates if a file’s records are parsed by column numbers. Only one of this property or spring.batch.job.flatfileitemreader.delimited can be true at the same time.spring-doc.cn

spring.batch.job.flatfileitemreader.rangesspring-doc.cn

List<Range>spring-doc.cn

empty listspring-doc.cn

List of column ranges by which to parse a fixed width record. See the Range documentation.spring-doc.cn

spring.batch.job.flatfileitemreader.namesspring-doc.cn

String []spring-doc.cn

nullspring-doc.cn

List of names for each field parsed from a record. These names are the keys in the Map<String, Object> in the items returned from this ItemReader.spring-doc.cn

spring.batch.job.flatfileitemreader.parsingStrictspring-doc.cn

booleanspring-doc.cn

truespring-doc.cn

If set to true, the mapping fails if the fields cannot be mapped.spring-doc.cn

JdbcCursorItemReader

The JdbcCursorItemReader runs a query against a relational database and iterates over the resulting cursor (ResultSet) to provide the resulting items. This autoconfiguration lets you provide a PreparedStatementSetter, a RowMapper, or both. You can also use the following properties to configure a JdbcCursorItemReader:spring-doc.cn

Table 4. JdbcCursorItemReader Properties
Property Type Default Value Description

spring.batch.job.jdbccursoritemreader.saveStatespring-doc.cn

booleanspring-doc.cn

truespring-doc.cn

Determines whether the state should be saved for restarts.spring-doc.cn

spring.batch.job.jdbccursoritemreader.namespring-doc.cn

Stringspring-doc.cn

nullspring-doc.cn

Name used to provide unique keys in the ExecutionContext.spring-doc.cn

spring.batch.job.jdbccursoritemreader.maxItemcountspring-doc.cn

intspring-doc.cn

Integer.MAX_VALUEspring-doc.cn

Maximum number of items to be read from the file.spring-doc.cn

spring.batch.job.jdbccursoritemreader.currentItemCountspring-doc.cn

intspring-doc.cn

0spring-doc.cn

Number of items that have already been read. Used on restarts.spring-doc.cn

spring.batch.job.jdbccursoritemreader.fetchSizespring-doc.cn

intspring-doc.cn

A hint to the driver to indicate how many records to retrieve per call to the database system. For best performance, you usually want to set it to match the chunk size.spring-doc.cn

spring.batch.job.jdbccursoritemreader.maxRowsspring-doc.cn

intspring-doc.cn

Maximum number of items to read from the database.spring-doc.cn

spring.batch.job.jdbccursoritemreader.queryTimeoutspring-doc.cn

intspring-doc.cn

Number of milliseconds for the query to timeout.spring-doc.cn

spring.batch.job.jdbccursoritemreader.ignoreWarningsspring-doc.cn

booleanspring-doc.cn

truespring-doc.cn

Determines whether the reader should ignore SQL warnings when processing.spring-doc.cn

spring.batch.job.jdbccursoritemreader.verifyCursorPositionspring-doc.cn

booleanspring-doc.cn

truespring-doc.cn

Indicates whether the cursor’s position should be verified after each read to verify that the RowMapper did not advance the cursor.spring-doc.cn

spring.batch.job.jdbccursoritemreader.driverSupportsAbsolutespring-doc.cn

booleanspring-doc.cn

falsespring-doc.cn

Indicates whether the driver supports absolute positioning of a cursor.spring-doc.cn

spring.batch.job.jdbccursoritemreader.useSharedExtendedConnectionspring-doc.cn

booleanspring-doc.cn

falsespring-doc.cn

Indicates whether the connection is shared with other processing (and is therefore part of a transaction).spring-doc.cn

spring.batch.job.jdbccursoritemreader.sqlspring-doc.cn

Stringspring-doc.cn

nullspring-doc.cn

SQL query from which to read.spring-doc.cn

You can also specify JDBC DataSource specifically for the reader by using the following properties: .JdbcCursorItemReader Propertiesspring-doc.cn

Property Type Default Value Description

spring.batch.job.jdbccursoritemreader.datasource.enablespring-doc.cn

booleanspring-doc.cn

falsespring-doc.cn

Determines whether JdbcCursorItemReader DataSource should be enabled.spring-doc.cn

jdbccursoritemreader.datasource.urlspring-doc.cn

Stringspring-doc.cn

nullspring-doc.cn

JDBC URL of the database.spring-doc.cn

jdbccursoritemreader.datasource.usernamespring-doc.cn

Stringspring-doc.cn

nullspring-doc.cn

Login username of the database.spring-doc.cn

jdbccursoritemreader.datasource.passwordspring-doc.cn

Stringspring-doc.cn

nullspring-doc.cn

Login password of the database.spring-doc.cn

jdbccursoritemreader.datasource.driver-class-namespring-doc.cn

Stringspring-doc.cn

nullspring-doc.cn

Fully qualified name of the JDBC driver.spring-doc.cn

The default DataSource will be used by the JDBCCursorItemReader if the jdbccursoritemreader_datasource is not specified.

KafkaItemReader

Ingesting a partition of data from a Kafka topic is useful and exactly what the KafkaItemReader can do. To configure a KafkaItemReader, two pieces of configuration are required. First, configuring Kafka with Spring Boot’s Kafka autoconfiguration is required (see the Spring Boot Kafka documentation). Once you have configured the Kafka properties from Spring Boot, you can configure the KafkaItemReader itself by setting the following properties:spring-doc.cn

Table 5. KafkaItemReader Properties
Property Type Default Value Description

spring.batch.job.kafkaitemreader.namespring-doc.cn

Stringspring-doc.cn

nullspring-doc.cn

Name used to provide unique keys in the ExecutionContext.spring-doc.cn

spring.batch.job.kafkaitemreader.topicspring-doc.cn

Stringspring-doc.cn

nullspring-doc.cn

Name of the topic from which to read.spring-doc.cn

spring.batch.job.kafkaitemreader.partitionsspring-doc.cn

List<Integer>spring-doc.cn

empty listspring-doc.cn

List of partition indices from which to read.spring-doc.cn

spring.batch.job.kafkaitemreader.pollTimeOutInSecondsspring-doc.cn

longspring-doc.cn

30spring-doc.cn

Timeout for the poll() operations.spring-doc.cn

spring.batch.job.kafkaitemreader.saveStatespring-doc.cn

booleanspring-doc.cn

truespring-doc.cn

Determines whether the state should be saved for restarts.spring-doc.cn

Native Compilation

The advantage of Single Step Batch Processing is that it lets you dynamically select which reader and writer beans to use at runtime when you use the JVM. However, when you use native compilation, you must determine the reader and writer at build time instead of runtime. The following example does so:spring-doc.cn

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>process-aot</id>
            <goals>
                <goal>process-aot</goal>
            </goals>
            <configuration>
                <jvmArguments>
                    -Dspring.batch.job.flatfileitemreader.name=fooReader
                    -Dspring.batch.job.flatfileitemwriter.name=fooWriter
                </jvmArguments>
            </configuration>
        </execution>
    </executions>
</plugin>

ItemProcessor Configuration

The single-step batch job autoconfiguration accepts an ItemProcessor if one is available within the ApplicationContext. If one is found of the correct type (ItemProcessor<Map<String, Object>, Map<String, Object>>), it is autowired into the step.spring-doc.cn

Autoconfiguration for ItemWriter implementations

This starter provides autoconfiguration for ItemWriter implementations that match the supported ItemReader implementations: AmqpItemWriter, FlatFileItemWriter, JdbcItemWriter, and KafkaItemWriter. This section covers how to use autoconfiguration to configure a supported ItemWriter.spring-doc.cn

AmqpItemWriter

To write to a RabbitMQ queue, you need two sets of configuration. First, you need an AmqpTemplate. The easiest way to get this is by using Spring Boot’s RabbitMQ autoconfiguration. See the Spring Boot AMQP documentation.spring-doc.cn

Once you have configured the AmqpTemplate, you can configure the AmqpItemWriter by setting the following properties:spring-doc.cn

Table 6. AmqpItemWriter Properties
Property Type Default Value Description

spring.batch.job.amqpitemwriter.enabledspring-doc.cn

booleanspring-doc.cn

falsespring-doc.cn

If true, the autoconfiguration runs.spring-doc.cn

spring.batch.job.amqpitemwriter.jsonConverterEnabledspring-doc.cn

booleanspring-doc.cn

truespring-doc.cn

Indicates whether Jackson2JsonMessageConverter should be registered to convert messages.spring-doc.cn

FlatFileItemWriter

To write a file as the output of the step, you can configure FlatFileItemWriter. Autoconfiguration accepts components that have been explicitly configured (such as LineAggregator, FieldExtractor, FlatFileHeaderCallback, or a FlatFileFooterCallback) and components that have been configured by setting the following properties specified:spring-doc.cn

Table 7. FlatFileItemWriter Properties
Property Type Default Value Description

spring.batch.job.flatfileitemwriter.resourcespring-doc.cn

Resourcespring-doc.cn

nullspring-doc.cn

The resource to be read.spring-doc.cn

spring.batch.job.flatfileitemwriter.delimitedspring-doc.cn

booleanspring-doc.cn

falsespring-doc.cn

Indicates whether the output file is a delimited file. If true, spring.batch.job.flatfileitemwriter.formatted must be false.spring-doc.cn

spring.batch.job.flatfileitemwriter.formattedspring-doc.cn

booleanspring-doc.cn

falsespring-doc.cn

Indicates whether the output file a formatted file. If true, spring.batch.job.flatfileitemwriter.delimited must be false.spring-doc.cn

spring.batch.job.flatfileitemwriter.formatspring-doc.cn

Stringspring-doc.cn

nullspring-doc.cn

The format used to generate the output for a formatted file. The formatting is performed by using String.format.spring-doc.cn

spring.batch.job.flatfileitemwriter.localespring-doc.cn

Localespring-doc.cn

Locale.getDefault()spring-doc.cn

The Locale to be used when generating the file.spring-doc.cn

spring.batch.job.flatfileitemwriter.maximumLengthspring-doc.cn

intspring-doc.cn

0spring-doc.cn

Max length of the record. If 0, the size is unbounded.spring-doc.cn

spring.batch.job.flatfileitemwriter.minimumLengthspring-doc.cn

intspring-doc.cn

0spring-doc.cn

The minimum record length.spring-doc.cn

spring.batch.job.flatfileitemwriter.delimiterspring-doc.cn

Stringspring-doc.cn

,spring-doc.cn

The String used to delimit fields in a delimited file.spring-doc.cn

spring.batch.job.flatfileitemwriter.encodingspring-doc.cn

Stringspring-doc.cn

FlatFileItemReader.DEFAULT_CHARSETspring-doc.cn

Encoding to use when writing the file.spring-doc.cn

spring.batch.job.flatfileitemwriter.forceSyncspring-doc.cn

booleanspring-doc.cn

falsespring-doc.cn

Indicates whether a file should be force-synced to the disk on flush.spring-doc.cn

spring.batch.job.flatfileitemwriter.namesspring-doc.cn

String []spring-doc.cn

nullspring-doc.cn

List of names for each field parsed from a record. These names are the keys in the Map<String, Object> for the items received by this ItemWriter.spring-doc.cn

spring.batch.job.flatfileitemwriter.appendspring-doc.cn

booleanspring-doc.cn

falsespring-doc.cn

Indicates whether a file should be appended to if the output file is found.spring-doc.cn

spring.batch.job.flatfileitemwriter.lineSeparatorspring-doc.cn

Stringspring-doc.cn

FlatFileItemWriter.DEFAULT_LINE_SEPARATORspring-doc.cn

What String to use to separate lines in the output file.spring-doc.cn

spring.batch.job.flatfileitemwriter.namespring-doc.cn

Stringspring-doc.cn

nullspring-doc.cn

Name used to provide unique keys in the ExecutionContext.spring-doc.cn

spring.batch.job.flatfileitemwriter.saveStatespring-doc.cn

booleanspring-doc.cn

truespring-doc.cn

Determines whether the state should be saved for restarts.spring-doc.cn

spring.batch.job.flatfileitemwriter.shouldDeleteIfEmptyspring-doc.cn

booleanspring-doc.cn

falsespring-doc.cn

If set to true, an empty file (there is no output) is deleted when the job completes.spring-doc.cn

spring.batch.job.flatfileitemwriter.shouldDeleteIfExistsspring-doc.cn

booleanspring-doc.cn

truespring-doc.cn

If set to true and a file is found where the output file should be, it is deleted before the step begins.spring-doc.cn

spring.batch.job.flatfileitemwriter.transactionalspring-doc.cn

booleanspring-doc.cn

FlatFileItemWriter.DEFAULT_TRANSACTIONALspring-doc.cn

Indicates whether the reader is a transactional queue (indicating that the items read are returned to the queue upon a failure).spring-doc.cn

JdbcBatchItemWriter

To write the output of a step to a relational database, this starter provides the ability to autoconfigure a JdbcBatchItemWriter. The autoconfiguration lets you provide your own ItemPreparedStatementSetter or ItemSqlParameterSourceProvider and configuration options by setting the following properties:spring-doc.cn

Table 8. JdbcBatchItemWriter Properties
Property Type Default Value Description

spring.batch.job.jdbcbatchitemwriter.namespring-doc.cn

Stringspring-doc.cn

nullspring-doc.cn

Name used to provide unique keys in the ExecutionContext.spring-doc.cn

spring.batch.job.jdbcbatchitemwriter.sqlspring-doc.cn

Stringspring-doc.cn

nullspring-doc.cn

The SQL used to insert each item.spring-doc.cn

spring.batch.job.jdbcbatchitemwriter.assertUpdatesspring-doc.cn

booleanspring-doc.cn

truespring-doc.cn

Whether to verify that every insert results in the update of at least one record.spring-doc.cn

You can also specify JDBC DataSource specifically for the writer by using the following properties: .JdbcBatchItemWriter Propertiesspring-doc.cn

Property Type Default Value Description

spring.batch.job.jdbcbatchitemwriter.datasource.enablespring-doc.cn

booleanspring-doc.cn

falsespring-doc.cn

Determines whether JdbcCursorItemReader DataSource should be enabled.spring-doc.cn

jdbcbatchitemwriter.datasource.urlspring-doc.cn

Stringspring-doc.cn

nullspring-doc.cn

JDBC URL of the database.spring-doc.cn

jdbcbatchitemwriter.datasource.usernamespring-doc.cn

Stringspring-doc.cn

nullspring-doc.cn

Login username of the database.spring-doc.cn

jdbcbatchitemwriter.datasource.passwordspring-doc.cn

Stringspring-doc.cn

nullspring-doc.cn

Login password of the database.spring-doc.cn

jdbcbatchitemreader.datasource.driver-class-namespring-doc.cn

Stringspring-doc.cn

nullspring-doc.cn

Fully qualified name of the JDBC driver.spring-doc.cn

The default DataSource will be used by the JdbcBatchItemWriter if the jdbcbatchitemwriter_datasource is not specified.

KafkaItemWriter

To write step output to a Kafka topic, you need KafkaItemWriter. This starter provides autoconfiguration for a KafkaItemWriter by using facilities from two places. The first is Spring Boot’s Kafka autoconfiguration. (See the Spring Boot Kafka documentation.) Second, this starter lets you configure two properties on the writer.spring-doc.cn

Table 9. KafkaItemWriter Properties
Property Type Default Value Description

spring.batch.job.kafkaitemwriter.topicspring-doc.cn

Stringspring-doc.cn

nullspring-doc.cn

The Kafka topic to which to write.spring-doc.cn

spring.batch.job.kafkaitemwriter.deletespring-doc.cn

booleanspring-doc.cn

falsespring-doc.cn

Whether the items being passed to the writer are all to be sent as delete events to the topic.spring-doc.cn

For more about the configuration options for the KafkaItemWriter, see the KafkaItemWiter documentation.spring-doc.cn

Spring AOT

When using Spring AOT with Single Step Batch Starter you must set the reader and writer name properties at compile time (unless you create a bean(s) for the reader and or writer). To do this you must include the name of the reader and writer that you wish to use as and argument or environment variable in the boot maven plugin or gradle plugin. For example if you wish to enable the FlatFileItemReader and FlatFileItemWriter in Maven it would look like:spring-doc.cn

    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <executions>
            <execution>
            <id>process-aot</id>
            <goals>
                <goal>process-aot</goal>
            </goals>
            </execution>
        </executions>
        <configuration>
            <arguments>
                <argument>--spring.batch.job.flatfileitemreader.name=foobar</argument>
                <argument>--spring.batch.job.flatfileitemwriter.name=fooWriter</argument>
            </arguments>
        </configuration>
    </plugin>