Skip to content

Commit 5598e18

Browse files
committed
Add a "Two minutes tutorial" to the README
Issue #4329
1 parent 9f56a50 commit 5598e18

File tree

1 file changed

+119
-2
lines changed

1 file changed

+119
-2
lines changed

README.md

Lines changed: 119 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,126 @@ If you are looking for a runtime orchestration tool for your Batch applications,
88

99
# Getting Started
1010

11-
## Guide
11+
## Two minutes tutorial
12+
13+
This quick tutorial shows you how to setup a minimal project to run a simple batch job with Spring Batch.
14+
15+
In your favorite IDE, create a new Maven-based Java 17+ project and add the following dependencies:
16+
17+
```xml
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.springframework.batch</groupId>
21+
<artifactId>spring-batch-core</artifactId>
22+
<version>${LATEST_VERSION}</version>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.hsqldb</groupId>
26+
<artifactId>hsqldb</artifactId>
27+
<version>${LATEST_VERSION}</version>
28+
<scope>runtime</scope>
29+
</dependency>
30+
</dependencies>
31+
```
32+
33+
Then, create a configuration class to define the datasource and transaction manager that will be used by the job repository:
34+
35+
```java
36+
import javax.sql.DataSource;
37+
38+
import org.springframework.context.annotation.Bean;
39+
import org.springframework.context.annotation.Configuration;
40+
import org.springframework.jdbc.support.JdbcTransactionManager;
41+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
42+
43+
@Configuration
44+
public class DataSourceConfiguration {
45+
46+
@Bean
47+
public DataSource dataSource() {
48+
return new EmbeddedDatabaseBuilder()
49+
.addScript("/org/springframework/batch/core/schema-hsqldb.sql")
50+
.build();
51+
}
52+
53+
@Bean
54+
public JdbcTransactionManager transactionManager(DataSource dataSource) {
55+
return new JdbcTransactionManager(dataSource);
56+
}
57+
58+
}
59+
```
60+
61+
In this tutorial, an embedded [HSQLDB](http://www.hsqldb.org) database is created and initialized with Spring Batch's meta-data tables.
62+
63+
Finally, create a class to define the batch job:
64+
65+
```java
66+
import org.springframework.batch.core.Job;
67+
import org.springframework.batch.core.JobParameters;
68+
import org.springframework.batch.core.Step;
69+
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
70+
import org.springframework.batch.core.job.builder.JobBuilder;
71+
import org.springframework.batch.core.launch.JobLauncher;
72+
import org.springframework.batch.core.repository.JobRepository;
73+
import org.springframework.batch.core.step.builder.StepBuilder;
74+
import org.springframework.batch.repeat.RepeatStatus;
75+
import org.springframework.context.ApplicationContext;
76+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
77+
import org.springframework.context.annotation.Bean;
78+
import org.springframework.context.annotation.Configuration;
79+
import org.springframework.context.annotation.Import;
80+
import org.springframework.jdbc.support.JdbcTransactionManager;
81+
82+
@Configuration
83+
@EnableBatchProcessing
84+
@Import(DataSourceConfiguration.class)
85+
public class HelloWorldJobConfiguration {
86+
87+
@Bean
88+
public Step step(JobRepository jobRepository, JdbcTransactionManager transactionManager) {
89+
return new StepBuilder("step", jobRepository).tasklet((contribution, chunkContext) -> {
90+
System.out.println("Hello world!");
91+
return RepeatStatus.FINISHED;
92+
}, transactionManager).build();
93+
}
94+
95+
@Bean
96+
public Job job(JobRepository jobRepository, Step step) {
97+
return new JobBuilder("job", jobRepository).start(step).build();
98+
}
99+
100+
public static void main(String[] args) throws Exception {
101+
ApplicationContext context = new AnnotationConfigApplicationContext(HelloWorldJobConfiguration.class);
102+
JobLauncher jobLauncher = context.getBean(JobLauncher.class);
103+
Job job = context.getBean(Job.class);
104+
jobLauncher.run(job, new JobParameters());
105+
}
106+
107+
}
108+
```
109+
110+
The job in this tutorial is composed of a single step that prints "Hello world!" to the standard output.
111+
112+
You can now run the `main` method of the `HelloWorldJobConfiguration` class to launch the job. The output should be similar to the following:
113+
114+
```
115+
INFO: Finished Spring Batch infrastructure beans configuration in 8 ms.
116+
INFO: Starting embedded database: url='jdbc:hsqldb:mem:testdb', username='sa'
117+
INFO: No database type set, using meta data indicating: HSQL
118+
INFO: No Micrometer observation registry found, defaulting to ObservationRegistry.NOOP
119+
INFO: No TaskExecutor has been set, defaulting to synchronous executor.
120+
INFO: Job: [SimpleJob: [name=job]] launched with the following parameters: [{}]
121+
INFO: Executing step: [step]
122+
Hello world!
123+
INFO: Step: [step] executed in 10ms
124+
INFO: Job: [SimpleJob: [name=job]] completed with the following parameters: [{}] and the following status: [COMPLETED] in 25ms
125+
```
126+
127+
## Getting Started Guide
12128

13-
This is the quickest way to get started with a new Spring Batch project based on Spring Boot. You find the Getting Started Guide here: [Creating a Batch Service](https://spring.io/guides/gs/batch-processing/).
129+
This guide is a more realistic tutorial that shows a typical ETL batch job that reads data from a flat file, transforms it and writes it to a relational database.
130+
It is a Spring Batch project based on Spring Boot. You find the Getting Started Guide here: [Creating a Batch Service](https://spring.io/guides/gs/batch-processing/).
14131

15132
## Samples
16133

0 commit comments

Comments
 (0)