[[batch-job-starter]]
= Single Step Batch Job Starter
[[partintro]]
--
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
https://spring.io/projects/spring-batch[Spring Batch documentation].
--
To obtain the starter for Maven, add the following to your build:
[source,xml]
----
org.springframework.cloud
spring-cloud-starter-single-step-batch-job
2.3.0
----
To obtain the starter for Gradle, add the following to your build:
[source,groovy]
----
compile "org.springframework.cloud:spring-cloud-starter-single-step-batch-job:2.3.0"
----
[[job-definition]]
== 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`.
[[job-definition-properties]]
=== Properties
To begin, the starter provides a set of properties that let you configure the basics of a Job with one Step:
.Job Properties
|===
| Property | Type | Default Value | Description
| `spring.batch.job.jobName`
| `String`
| `null`
| The name of the job.
| `spring.batch.job.stepName`
| `String`
| `null`
| The name of the step.
| `spring.batch.job.chunkSize`
| `Integer`
| `null`
| The number of items to be processed per transaction.
|===
With the above properties configured, you have a job with a single, chunk-based step.
This chunk-based step reads, processes, and writes `Map` 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.
NOTE: 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` as the input and the output item.
[[item-readers]]
== 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.
[[amqpitemreader]]
=== 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
https://docs.spring.io/spring-boot/docs/3.0.x/reference/htmlsingle/#messaging.amqp.rabbitmq[Spring Boot AMQP documentation].
Once you have configured the `AmqpTemplate`, you can enable the batch capabilities to support it
by setting the following properties:
.`AmqpItemReader` Properties
|===
| Property | Type | Default Value | Description
| `spring.batch.job.amqpitemreader.enabled`
| `boolean`
| `false`
| If `true`, the autoconfiguration will execute.
| `spring.batch.job.amqpitemreader.jsonConverterEnabled`
| `boolean`
| `true`
| Indicates if the `Jackson2JsonMessageConverter` should be registered to parse messages.
|===
For more information, see the https://docs.spring.io/spring-batch/docs/4.3.x/api/org/springframework/batch/item/amqp/AmqpItemReader.html[`AmqpItemReader` documentation].
[[flatfileitemreader]]
=== 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:
.`FlatFileItemReader` Properties
|===
| Property | Type | Default Value | Description
| `spring.batch.job.flatfileitemreader.saveState`
| `boolean`
| `true`
| Determines if the state should be saved for restarts.
| `spring.batch.job.flatfileitemreader.name`
| `String`
| `null`
| Name used to provide unique keys in the `ExecutionContext`.
| `spring.batch.job.flatfileitemreader.maxItemcount`
| `int`
| `Integer.MAX_VALUE`
| Maximum number of items to be read from the file.
| `spring.batch.job.flatfileitemreader.currentItemCount`
| `int`
| 0
| Number of items that have already been read. Used on restarts.
| `spring.batch.job.flatfileitemreader.comments`
| `List`
| empty List
| A list of Strings that indicate commented lines (lines to be ignored) in the file.
| `spring.batch.job.flatfileitemreader.resource`
| `Resource`
| `null`
| The resource to be read.
| `spring.batch.job.flatfileitemreader.strict`
| `boolean`
| `true`
| If set to `true`, the reader throws an exception if the resource is not found.
| `spring.batch.job.flatfileitemreader.encoding`
| `String`
| `FlatFileItemReader.DEFAULT_CHARSET`
| Encoding to be used when reading the file.
| `spring.batch.job.flatfileitemreader.linesToSkip`
| `int`
| 0
| Indicates the number of lines to skip at the start of a file.
| `spring.batch.job.flatfileitemreader.delimited`
| `boolean`
| `false`
| 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.batch.job.flatfileitemreader.delimiter`
| `String`
| `DelimitedLineTokenizer.DELIMITER_COMMA`
| If reading a delimited file, indicates the delimiter to parse on.
| `spring.batch.job.flatfileitemreader.quoteCharacter`
| `char`
| `DelimitedLineTokenizer.DEFAULT_QUOTE_CHARACTER`
| Used to determine the character used to quote values.
| `spring.batch.job.flatfileitemreader.includedFields`
| `List`
| empty list
| A list of indices to determine which fields in a record to include in the item.
| `spring.batch.job.flatfileitemreader.fixedLength`
| `boolean`
| `false`
| 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.batch.job.flatfileitemreader.ranges`
| `List`
| empty list
| List of column ranges by which to parse a fixed width record. See the https://docs.spring.io/spring-batch/docs/4.3.x/api/org/springframework/batch/item/file/transform/Range.html[Range documentation].
| `spring.batch.job.flatfileitemreader.names`
| `String []`
| `null`
| List of names for each field parsed from a record. These names are the keys in the `Map` in the items returned from this `ItemReader`.
| `spring.batch.job.flatfileitemreader.parsingStrict`
| `boolean`
| `true`
| If set to `true`, the mapping fails if the fields cannot be mapped.
|===
See the https://docs.spring.io/spring-batch/docs/4.3.x/api/org/springframework/batch/item/file/FlatFileItemReader.html[`FlatFileItemReader` documentation].
[[jdbcCursorItemReader]]
=== 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`:
.`JdbcCursorItemReader` Properties
|===
| Property | Type | Default Value | Description
| `spring.batch.job.jdbccursoritemreader.saveState`
| `boolean`
| `true`
| Determines whether the state should be saved for restarts.
| `spring.batch.job.jdbccursoritemreader.name`
| `String`
| `null`
| Name used to provide unique keys in the `ExecutionContext`.
| `spring.batch.job.jdbccursoritemreader.maxItemcount`
| `int`
| `Integer.MAX_VALUE`
| Maximum number of items to be read from the file.
| `spring.batch.job.jdbccursoritemreader.currentItemCount`
| `int`
| 0
| Number of items that have already been read. Used on restarts.
| `spring.batch.job.jdbccursoritemreader.fetchSize`
| `int`
|
| 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.batch.job.jdbccursoritemreader.maxRows`
| `int`
|
| Maximum number of items to read from the database.
| `spring.batch.job.jdbccursoritemreader.queryTimeout`
| `int`
|
| Number of milliseconds for the query to timeout.
| `spring.batch.job.jdbccursoritemreader.ignoreWarnings`
| `boolean`
| `true`
| Determines whether the reader should ignore SQL warnings when processing.
| `spring.batch.job.jdbccursoritemreader.verifyCursorPosition`
| `boolean`
| `true`
| Indicates whether the cursor's position should be verified after each read to verify that the `RowMapper` did not advance the cursor.
| `spring.batch.job.jdbccursoritemreader.driverSupportsAbsolute`
| `boolean`
| `false`
| Indicates whether the driver supports absolute positioning of a cursor.
| `spring.batch.job.jdbccursoritemreader.useSharedExtendedConnection`
| `boolean`
| `false`
| Indicates whether the connection is shared with other processing (and is therefore part of a transaction).
| `spring.batch.job.jdbccursoritemreader.sql`
| `String`
| `null`
| SQL query from which to read.
|===
You can also specify JDBC DataSource specifically for the reader by using the following properties:
.`JdbcCursorItemReader` Properties
|===
| Property | Type | Default Value | Description
| `spring.batch.job.jdbccursoritemreader.datasource.enable`
| `boolean`
| `false`
| Determines whether `JdbcCursorItemReader` `DataSource` should be enabled.
| `jdbccursoritemreader.datasource.url`
| `String`
| `null`
| JDBC URL of the database.
| `jdbccursoritemreader.datasource.username`
| `String`
| `null`
| Login username of the database.
| `jdbccursoritemreader.datasource.password`
| `String`
| `null`
| Login password of the database.
| `jdbccursoritemreader.datasource.driver-class-name`
| `String`
| `null`
| Fully qualified name of the JDBC driver.
|===
NOTE: The default `DataSource` will be used by the `JDBCCursorItemReader` if the `jdbccursoritemreader_datasource` is not specified.
See the https://docs.spring.io/spring-batch/docs/4.3.x/api/org/springframework/batch/item/database/JdbcCursorItemReader.html[`JdbcCursorItemReader` documentation].
[[kafkaItemReader]]
=== 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
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#messaging.kafka.additional-properties[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:
.`KafkaItemReader` Properties
|===
| Property | Type | Default Value | Description
| `spring.batch.job.kafkaitemreader.name`
| `String`
| `null`
| Name used to provide unique keys in the `ExecutionContext`.
| `spring.batch.job.kafkaitemreader.topic`
| `String`
| `null`
| Name of the topic from which to read.
| `spring.batch.job.kafkaitemreader.partitions`
| `List`
| empty list
| List of partition indices from which to read.
| `spring.batch.job.kafkaitemreader.pollTimeOutInSeconds`
| `long`
| 30
| Timeout for the `poll()` operations.
| `spring.batch.job.kafkaitemreader.saveState`
| `boolean`
| `true`
| Determines whether the state should be saved for restarts.
|===
See the https://docs.spring.io/spring-batch/docs/4.3.x/api/org/springframework/batch/item/kafka/KafkaItemReader.html[`KafkaItemReader` documentation].
[[nativeCompilation]]
=== 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:
[source,xml]
org.springframework.boot
spring-boot-maven-plugin
process-aot
process-aot
-Dspring.batch.job.flatfileitemreader.name=fooReader
-Dspring.batch.job.flatfileitemwriter.name=fooWriter
[[item-processors]]
== 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