Skip to content

Commit da06a40

Browse files
committed
Converted to asciidoctor
1 parent d45f654 commit da06a40

File tree

4 files changed

+91
-578
lines changed

4 files changed

+91
-578
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ build/
2626
# Spring Bootstrap artifacts
2727

2828
dependency-reduced-pom.xml
29+
README.html
+86-53
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,167 @@
1-
<#assign project_id="gs-batch-processing">
1+
:spring_boot_version: 0.5.0.M6
2+
:Component: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/stereotype/Component.html
3+
:EnableAutoConfiguration: http://docs.spring.io/spring-boot/docs/{spring_boot_version}/api/org/springframework/boot/autoconfigure/EnableAutoConfiguration.html
4+
:SpringApplication: http://docs.spring.io/spring-boot/docs/{spring_boot_version}/api/org/springframework/boot/SpringApplication.html
5+
:toc:
6+
:icons: font
7+
:source-highlighter: prettify
8+
:project_id: gs-batch-processing
29
This guide walks you through the process of creating a basic batch-driven solution.
310

4-
What you'll build
5-
-----------------
11+
== What you'll build
612

713
You'll build a service that imports data from a CSV spreadsheet, transforms it with custom code, and stores the final results in a database.
814

915

10-
What you'll need
11-
----------------
16+
== What you'll need
1217

13-
- About 15 minutes
14-
- <@prereq_editor_jdk_buildtools/>
18+
include::https://raw.github.com/spring-guides/getting-started-macros/master/prereq_editor_jdk_buildtools.adoc[]
1519

16-
## <@how_to_complete_this_guide jump_ahead='Create a business class'/>
20+
:jump_ahead: Create a business class
21+
include::https://raw.github.com/spring-guides/getting-started-macros/master/how_to_complete_this_guide.adoc[]
1722

1823

19-
<a name="scratch"></a>
20-
Set up the project
21-
------------------
22-
<@build_system_intro/>
24+
[[scratch]]
25+
== Set up the project
26+
include::https://raw.github.com/spring-guides/getting-started-macros/master/build_system_intro.adoc[]
2327

24-
<@create_directory_structure_hello/>
28+
include::https://raw.github.com/spring-guides/getting-started-macros/master/create_directory_structure_hello.adoc[]
2529

2630

27-
<@create_both_builds/>
31+
include::https://raw.github.com/spring-guides/getting-started-macros/master/create_both_builds.adoc[]
2832

29-
<@bootstrap_starter_pom_disclaimer/>
33+
`build.gradle`
34+
// AsciiDoc source formatting doesn't support groovy, so using java instead
35+
[source,java]
36+
----
37+
include::initial/build.gradle[]
38+
----
39+
40+
include::https://raw.github.com/spring-guides/getting-started-macros/master/bootstrap_starter_pom_disclaimer.adoc[]
3041

31-
### Create business data
3242

3343
Typically your customer or a business analyst supplies a spreadsheet. In this case, you make it up.
3444

35-
<@snippet path="src/main/resources/sample-data.csv" prefix="initial"/>
45+
`src/main/resources/sample-data.csv`
46+
[source,csv]
47+
----
48+
include::initial/src/main/resources/sample-data.csv[]
49+
----
3650

3751
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.
3852

39-
### Define the destination for your data
4053

4154
Next, you write a SQL script to create a table to store the data.
4255

43-
<@snippet path="src/main/resources/schema-all.sql" prefix="initial"/>
56+
`src/main/resources/schema-all.sql`
57+
[source,sql]
58+
----
59+
include::initial/src/main/resources/schema-all.sql[]
60+
----
4461

45-
> **Note:** Spring Boot runs `schema-@@platform@@.sql` automatically during startup. `-all` is the default for all platforms.
62+
NOTE: Spring Boot runs `schema-@@platform@@.sql` automatically during startup. `-all` is the default for all platforms.
4663

4764

48-
<a name="initial"></a>
49-
Create a business class
50-
-----------------------
65+
[[initial]]
66+
== Create a business class
5167

5268
Now that you see the format of data inputs and outputs, you write code to represent a row of data.
5369

54-
<@snippet path="src/main/java/hello/Person.java" prefix="complete"/>
70+
`src/main/java/hello/Person.java`
71+
[source,java]
72+
----
73+
include::complete/src/main/java/hello/Person.java[]
74+
----
5575

5676
You can instantiate the `Person` class either with first and last name through a constructor, or by setting the properties.
5777

5878

59-
Create an intermediate processor
60-
--------------------------------
79+
== Create an intermediate processor
6180

6281
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.
6382

64-
<@snippet path="src/main/java/hello/PersonItemProcessor.java" prefix="complete"/>
83+
`src/main/java/hello/PersonItemProcessor.java`
84+
[source,java]
85+
----
86+
include::complete/src/main/java/hello/PersonItemProcessor.java[]
87+
----
6588

6689
`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`.
6790

68-
> **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.
91+
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.
6992

7093

71-
Put together a batch job
72-
------------------------
94+
== Put together a batch job
7395

7496
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.
7597

76-
<@snippet path="src/main/java/hello/BatchConfiguration.java" prefix="complete"/>
98+
`src/main/java/hello/BatchConfiguration.java`
99+
[source,java]
100+
----
101+
include::complete/src/main/java/hello/BatchConfiguration.java[]
102+
----
77103

78104
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.
79105

80106
Break it down:
81107

82-
<@snippet "src/main/java/hello/BatchConfiguration.java" "readerwriterprocessor" "/complete"/>
83-
108+
`src/main/java/hello/BatchConfiguration.java`
109+
[source,java]
110+
----
111+
include::/complete/src/main/java/hello/BatchConfiguration.java[tag=readerwriterprocessor]
112+
----
113+
.
84114
The first chunk of code defines the input, processor, and output.
85115
- `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`.
86116
- `processor()` creates an instance of our `PersonItemProcessor` you defined earlier, meant to uppercase the data.
87117
- `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.
88118

89119
The next chunk focuses on the actual job configuration.
90120

91-
<@snippet "src/main/java/hello/BatchConfiguration.java" "jobstep" "/complete"/>
92-
121+
`src/main/java/hello/BatchConfiguration.java`
122+
[source,java]
123+
----
124+
include::/complete/src/main/java/hello/BatchConfiguration.java[tag=jobstep]
125+
----
126+
.
93127
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.
94128

95129
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.
96130

97131
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.
98132

99-
> **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>`.
133+
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>`.
100134

101135

102-
Make the application executable
103-
-------------------------------
136+
== Make the application executable
104137

105138
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.
106139

107-
### Create an Application class
108140

109-
<@snippet path="src/main/java/hello/Application.java" prefix="complete"/>
141+
`src/main/java/hello/Application.java`
142+
[source,java]
143+
----
144+
include::complete/src/main/java/hello/Application.java[]
145+
----
110146

111-
The `main()` method defers to the [`SpringApplication`][] helper class, providing `Application.class` as an argument to its `run()` method. This tells Spring to read the annotation metadata from `Application` and to manage it as a component in the [Spring application context][u-application-context].
147+
The `main()` method defers to the {SpringApplication}[`SpringApplication`] helper class, providing `Application.class` as an argument to its `run()` method. This tells Spring to read the annotation metadata from `Application` and to manage it as a component in the link:/understanding/application-context[Spring application context].
112148

113-
The `@ComponentScan` annotation tells Spring to search recursively through the `hello` package and its children for classes marked directly or indirectly with Spring's [`@Component`][] annotation. This directive ensures that Spring finds and registers `BatchConfiguration`, because it is marked with `@Configuration`, which in turn is a kind of `@Component` annotation.
149+
The `@ComponentScan` annotation tells Spring to search recursively through the `hello` package and its children for classes marked directly or indirectly with Spring's {Component}[`@Component`] annotation. This directive ensures that Spring finds and registers `BatchConfiguration`, because it is marked with `@Configuration`, which in turn is a kind of `@Component` annotation.
114150

115-
The [`@EnableAutoConfiguration`][] annotation switches on reasonable default behaviors based on the content of your classpath. For example, it looks for any class that implements the `CommandLineRunner` interface and invokes its `run()` method. In this case, it runs the demo code for this guide.
151+
The {EnableAutoConfiguration}[`@EnableAutoConfiguration`] annotation switches on reasonable default behaviors based on the content of your classpath. For example, it looks for any class that implements the `CommandLineRunner` interface and invokes its `run()` method. In this case, it runs the demo code for this guide.
116152

117153
For demonstration purposes, there is code to create a `JdbcTemplate`, query the database, and print out the names of people the batch job inserts.
118154

119-
<@build_an_executable_jar_subhead/>
155+
include::https://raw.github.com/spring-guides/getting-started-macros/master/build_an_executable_jar_subhead.adoc[]
120156

121-
<@build_an_executable_jar_with_both/>
157+
include::https://raw.github.com/spring-guides/getting-started-macros/master/build_an_executable_jar_with_both.adoc[]
122158

123-
<@run_the_application_with_both module="batch job"/>
159+
:module: batch job
160+
include::https://raw.github.com/spring-guides/getting-started-macros/master/run_the_application_with_both.adoc[]
124161

125162
The job prints out a line for each person that gets transformed. After the job runs, you can also see the output from querying the database.
126163

127-
```sh
164+
....
128165
Converting (firstName: Jill, lastName: Doe) into (firstName: JILL, lastName: DOE)
129166
Converting (firstName: Joe, lastName: Doe) into (firstName: JOE, lastName: DOE)
130167
Converting (firstName: Justin, lastName: Doe) into (firstName: JUSTIN, lastName: DOE)
@@ -135,16 +172,12 @@ Found <firstName: JOE, lastName: DOE> in the database.
135172
Found <firstName: JUSTIN, lastName: DOE> in the database.
136173
Found <firstName: JANE, lastName: DOE> in the database.
137174
Found <firstName: JOHN, lastName: DOE> in the database.
138-
```
175+
....
139176

140177

141-
Summary
142-
-------
178+
== Summary
143179

144180
Congratulations! You built a batch job that ingested data from a spreadsheet, processed it, and wrote it to a database.
145181

146182

147-
[`SpringApplication`]: http://docs.spring.io/spring-boot/docs/0.5.0.M3/api/org/springframework/boot/SpringApplication.html
148-
[`@EnableAutoConfiguration`]: http://docs.spring.io/spring-boot/docs/0.5.0.M3/api/org/springframework/boot/autoconfigure/EnableAutoConfiguration.html
149-
[`@Component`]: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/stereotype/Component.html
150183

0 commit comments

Comments
 (0)