You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This spreadsheet contains a first name and a last name on each row, separated by a comma. This is a fairly common pattern that Spring handles out-of-the-box, as you will see.
41
-
36
+
This spreadsheet contains a first name and a last name on each row, separated by a comma.
37
+
This is a fairly common pattern that Spring can handle without customization.
42
38
43
-
Next, you write a SQL script to create a table to store the data.
39
+
Next, you need to write an SQL script to create a table to store the data. You can find
40
+
such a script in `src/main/resources/schema-all.sql`:
You can instantiate the `Person` class either with first and last name through a constructor, or by setting the properties.
98
+
You can instantiate the `Person` class either with first and last name through a
99
+
constructor or by setting the properties.
66
100
67
101
68
-
== Create an intermediate processor
102
+
== Create an Intermediate Processor
69
103
70
-
A common paradigm in batch processing is to ingest data, transform it, and then pipe it out somewhere else. Here you write a simple transformer that converts the names to uppercase.
104
+
A common paradigm in batch processing is to ingest data, transform it, and then pipe it
105
+
out somewhere else. Here, you need to write a simple transformer that converts the names
106
+
to uppercase. The following listing (from
107
+
`src/main/java/com/example/batchprocessing/PersonItemProcessor.java`) shows how to do
`PersonItemProcessor` implements Spring Batch's `ItemProcessor` interface. This makes it easy to wire the code into a batch job that you define further down in this guide. According to the interface, you receive an incoming `Person` object, after which you transform it to an upper-cased `Person`.
79
-
80
-
NOTE: There is no requirement that the input and output types be the same. In fact, after one source of data is read, sometimes the application's data flow needs a different data type.
117
+
`PersonItemProcessor` implements Spring Batch's `ItemProcessor` interface. This makes it
118
+
easy to wire the code into a batch job that you will define later in this guide. According
119
+
to the interface, you receive an incoming `Person` object, after which you transform it to
120
+
an upper-cased `Person`.
81
121
122
+
NOTE: The input and output types need not be the same. In fact, after one source of data
123
+
is read, sometimes the application's data flow needs a different data type.
82
124
83
-
== Put together a batch job
125
+
== Put Together a Batch Job
84
126
85
-
Now you put together the actual batch job. Spring Batch provides many utility classes that reduce the need to write custom code. Instead, you can focus on the business logic.
127
+
Now you need to put together the actual batch job. Spring Batch provides many utility
128
+
classes that reduce the need to write custom code. Instead, you can focus on the business
129
+
logic. The following example, (from
130
+
`src/main/java/com/exampe/batchprocessing/BatchConfiguration.java`) shows how configure a
For starters, the `@EnableBatchProcessing` annotation adds many critical beans that support jobs and saves you a lot of leg work. This example uses a memory-based database (provided by `@EnableBatchProcessing`), meaning that when it's done, the data is gone.
140
+
For starters, the `@EnableBatchProcessing` annotation adds many critical beans that
141
+
support jobs and save you a lot of leg work. This example uses a memory-based database
142
+
(provided by `@EnableBatchProcessing`), meaning that, when it is done, the data is gone.
143
+
The following listing (from
144
+
`src/main/java/com/example/batchprocessing/BatchConfiguration.java`) shows the definition
145
+
of the batch job (the reader, the processor, and the writer):
The first chunk of code defines the input, processor, and output.
104
155
105
-
* `reader()` creates an `ItemReader`. It looks for a file called `sample-data.csv` and parses each line item with enough information to turn it into a `Person`.
106
-
* `processor()` creates an instance of our `PersonItemProcessor` you defined earlier, meant to uppercase the data.
107
-
* `write(DataSource)` creates an `ItemWriter`. This one is aimed at a JDBC destination and automatically gets a copy of the dataSource created by `@EnableBatchProcessing`. It includes the SQL statement needed to insert a single `Person` driven by Java bean properties.
156
+
* `reader()` creates an `ItemReader`. It looks for a file called `sample-data.csv` and
157
+
parses each line item with enough information to turn it into a `Person`.
158
+
* `processor()` creates an instance of the `PersonItemProcessor` that you defined earlier,
159
+
meant to converth the data to upper case.
160
+
* `write(DataSource)` creates an `ItemWriter`. This one is aimed at a JDBC destination and
161
+
automatically gets a copy of the dataSource created by `@EnableBatchProcessing`. It
162
+
includes the SQL statement needed to insert a single `Person`, driven by Java bean
163
+
properties.
108
164
109
-
The next chunk focuses on the actual job configuration.
165
+
The next chunk (from `src/main/java/com/example/batchprocessing/BatchConfiguration.java`)
The first method defines the job, and the second one defines a single step. Jobs are built
176
+
from steps, where each step can involve a reader, a processor, and a writer.
116
177
117
-
The first method defines the job and the second one defines a single step. Jobs are built from steps, where each step can involve a reader, a processor, and a writer.
178
+
In this job definition, you need an incrementer, because jobs use a database to maintain
179
+
execution state. You then list each step, (though this job has only one step). The job
180
+
ends, and the Java API produces a perfectly configured job.
118
181
119
-
In this job definition, you need an incrementer because jobs use a database to maintain execution state. You then list each step, of which this job has only one step. The job ends, and the Java API produces a perfectly configured job.
182
+
In the step definition, you define how much data to write at a time. In this case, it
183
+
writes up to ten records at a time. Next, you configure the reader, processor, and writer
184
+
by using the bits injected earlier.
120
185
121
-
In the step definition, you define how much data to write at a time. In this case, it writes up to ten records at a time. Next, you configure the reader, processor, and writer using the injected bits from earlier.
186
+
NOTE: `chunk()` is prefixed `<Person,Person>` because it is a generic method. This
187
+
represents the input and output types of each "`chunk`" of processing and lines up with
188
+
`ItemReader<Person>` and `ItemWriter<Person>`.
122
189
123
-
NOTE: chunk() is prefixed `<Person,Person>` because it's a generic method. This represents the input and output types of each "chunk" of processing, and lines up with `ItemReader<Person>` and `ItemWriter<Person>`.
190
+
The last bit of batch configuration is a way to get notified when the job completes. The
This code listens for when a job is `BatchStatus.COMPLETED`, and then uses `JdbcTemplate` to inspect the results.
132
-
202
+
The `JobCompletionNotificationListener` listens for when a job is `BatchStatus.COMPLETED`
203
+
and then uses `JdbcTemplate` to inspect the results.
133
204
134
-
== Make the application executable
205
+
== Make the Application Executable
135
206
136
207
Although batch processing can be embedded in web apps and WAR files, the simpler approach demonstrated below creates a standalone application. You package everything in a single, executable JAR file, driven by a good old Java `main()` method.
137
208
209
+
The Spring Initializr created an application class for you. For this simple example, it
210
+
works without further modification. The folowing listing (from
211
+
`src/main/java/com/example/batchprocessing/BatchProcessingApplication.java`) shows the
0 commit comments