Skip to content

Commit 9f56a50

Browse files
committed
Add hello world sample
Issue #4329
1 parent db7fa32 commit 9f56a50

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed

spring-batch-samples/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Here is a list of samples with checks to indicate which features each one demons
2323

2424
| Job/Feature | skip | retry | restart | automatic mapping | asynch launch | validation | delegation | write behind | non-sequential | asynch process | filtering |
2525
|:--------------------------------------------------------------|:----:|:-----:|:-------:|:-----------------:|:-------------:|:----------:|:----------:|:------------:|:--------------:|:--------------:|:---------:|
26+
| [Hello world Job Sample](#hello-world-job-sample) | | | | | | | | | | X | |
2627
| [Amqp Job Sample](#amqp-job-sample) | | | | | | | | | | X | |
2728
| [BeanWrapperMapper Sample](#beanwrappermapper-sample) | | | | X | | | | | | | |
2829
| [Composite ItemWriter Sample](#composite-itemwriter-sample) | | | | | | | X | | | | |
@@ -78,6 +79,13 @@ $>../mvnw -Dtest=[JobName]FunctionalTests#test[JobName] test
7879

7980
Please refer to the README of each sample for launching instructions.
8081

82+
### Hello world Job sample
83+
84+
This sample is a single-step job that prints "Hello world!" to the standard
85+
output. It shows the basic setup to configure and use Spring Batch.
86+
87+
[Hello world sample](src/main/java/org/springframework/batch/samples/helloworld/README.md)
88+
8189
### Jdbc Readers and Writers sample
8290

8391
The purpose of this sample is to show to usage of the
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.batch.samples.helloworld;
17+
18+
import org.springframework.batch.core.Job;
19+
import org.springframework.batch.core.Step;
20+
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
21+
import org.springframework.batch.core.job.builder.JobBuilder;
22+
import org.springframework.batch.core.repository.JobRepository;
23+
import org.springframework.batch.core.step.builder.StepBuilder;
24+
import org.springframework.batch.repeat.RepeatStatus;
25+
import org.springframework.batch.samples.common.DataSourceConfiguration;
26+
import org.springframework.context.annotation.Bean;
27+
import org.springframework.context.annotation.Configuration;
28+
import org.springframework.context.annotation.Import;
29+
import org.springframework.jdbc.support.JdbcTransactionManager;
30+
31+
@Configuration
32+
@EnableBatchProcessing
33+
@Import(DataSourceConfiguration.class)
34+
public class HelloWorldJobConfiguration {
35+
36+
@Bean
37+
public Step step(JobRepository jobRepository, JdbcTransactionManager transactionManager) {
38+
return new StepBuilder("step", jobRepository).tasklet((contribution, chunkContext) -> {
39+
System.out.println("Hello world!");
40+
return RepeatStatus.FINISHED;
41+
}, transactionManager).build();
42+
}
43+
44+
@Bean
45+
public Job job(JobRepository jobRepository, Step step) {
46+
return new JobBuilder("job", jobRepository).start(step).build();
47+
}
48+
49+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
### Hello world sample
2+
3+
## About
4+
5+
This sample is a single-step job that prints "Hello world!" to the standard
6+
output. It shows the basic setup to configure and use Spring Batch.
7+
8+
## Run the sample
9+
10+
You can run the sample from the command line as following:
11+
12+
```
13+
$>cd spring-batch-samples
14+
$>../mvnw -Dtest=HelloWorldJobFunctionalTests#testLaunchJob test
15+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.batch.samples.helloworld;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.batch.core.BatchStatus;
22+
import org.springframework.batch.core.Job;
23+
import org.springframework.batch.core.JobExecution;
24+
import org.springframework.batch.core.JobParameters;
25+
import org.springframework.batch.core.launch.JobLauncher;
26+
import org.springframework.context.ApplicationContext;
27+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
28+
29+
import static org.junit.jupiter.api.Assertions.assertEquals;
30+
31+
class HelloWorldJobFunctionalTests {
32+
33+
@Test
34+
public void testLaunchJob() throws Exception {
35+
// given
36+
ApplicationContext context = new AnnotationConfigApplicationContext(HelloWorldJobConfiguration.class);
37+
JobLauncher jobLauncher = context.getBean(JobLauncher.class);
38+
Job job = context.getBean(Job.class);
39+
40+
// when
41+
JobExecution jobExecution = jobLauncher.run(job, new JobParameters());
42+
43+
// then
44+
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
45+
}
46+
47+
}

0 commit comments

Comments
 (0)