Skip to content

Commit eb3e839

Browse files
committed
Add sample app to show MongoDB support
(cherry picked from commit 2006100)
1 parent 94b1ee2 commit eb3e839

File tree

7 files changed

+273
-0
lines changed

7 files changed

+273
-0
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,7 @@ project('spring-batch-samples') {
636636
optional "org.postgresql:postgresql:$postgresqlVersion"
637637
optional "org.springframework:spring-web:$springVersion"
638638
optional "org.springframework.data:spring-data-commons:$springDataCommonsVersion"
639+
optional "org.springframework.data:spring-data-mongodb:$springDataMongodbVersion"
639640
optional "org.springframework.amqp:spring-amqp:$springAmqpVersion"
640641
optional ("org.springframework.amqp:spring-rabbit:$springAmqpVersion") {
641642
exclude group: "org.springframework", module: "spring-messaging"

spring-batch-samples/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ jpa | | | |
6060
multiRecordtype | | x | | | | | | | x | | | x
6161
multiResource | x | | | | | | | x | | | | x
6262
[XML Input Output](#xml-input-output) | | | x | | | | | | | x | |
63+
[MongoDB sample](#mongodb-sample) | | | | | x | | | | x | | |
6364

6465

6566
### Common Sample Source Structures
@@ -986,3 +987,19 @@ and import the ready-to-use dashboard in `spring-batch-samples/src/grafana/sprin
986987

987988
Finally, run the `org.springframework.batch.sample.metrics.BatchMetricsApplication`
988989
class without any argument to start the sample.
990+
991+
# MongoDB sample
992+
993+
This sample is a showcase of MongoDB support in Spring Batch. It copies data from
994+
an input collection to an output collection using `MongoItemReader` and `MongoItemWriter`.
995+
996+
To run the sample, you need to have a MongoDB server up and running on `localhost:27017`
997+
(you can change these defaults in `mongodb-sample.properties`). If you use docker,
998+
you can run the following command to start a MongoDB server:
999+
1000+
```
1001+
$>docker run --name mongodb --rm -d -p 27017:27017 mongo
1002+
```
1003+
1004+
Once MongoDB is up and running, run the `org.springframework.batch.sample.mongodb.MongoDBSampleApp`
1005+
class without any argument to start the sample.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2020 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.sample.mongodb;
17+
18+
import java.util.HashMap;
19+
import java.util.Map;
20+
21+
import org.springframework.batch.core.Job;
22+
import org.springframework.batch.core.Step;
23+
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
24+
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
25+
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
26+
import org.springframework.batch.item.data.MongoItemReader;
27+
import org.springframework.batch.item.data.MongoItemWriter;
28+
import org.springframework.batch.item.data.builder.MongoItemReaderBuilder;
29+
import org.springframework.batch.item.data.builder.MongoItemWriterBuilder;
30+
import org.springframework.context.annotation.Bean;
31+
import org.springframework.data.domain.Sort;
32+
import org.springframework.data.mongodb.core.MongoTemplate;
33+
34+
/**
35+
* This sample job will copy data from collection "person_in" into collection "person_out"
36+
* using {@link MongoItemReader} and {@link MongoItemWriter}.
37+
*
38+
* @author Mahmoud Ben Hassine
39+
*/
40+
@EnableBatchProcessing
41+
public class JobConfiguration {
42+
43+
private JobBuilderFactory jobBuilderFactory;
44+
45+
private StepBuilderFactory stepBuilderFactory;
46+
47+
public JobConfiguration(JobBuilderFactory jobBuilderFactory, StepBuilderFactory stepBuilderFactory) {
48+
this.jobBuilderFactory = jobBuilderFactory;
49+
this.stepBuilderFactory = stepBuilderFactory;
50+
}
51+
52+
@Bean
53+
public MongoItemReader<Person> mongoItemReader(MongoTemplate mongoTemplate) {
54+
Map<String, Sort.Direction> sortOptions = new HashMap<>();
55+
sortOptions.put("name", Sort.Direction.DESC);
56+
return new MongoItemReaderBuilder<Person>()
57+
.name("personItemReader")
58+
.collection("person_in")
59+
.targetType(Person.class)
60+
.template(mongoTemplate)
61+
.jsonQuery("{}")
62+
.sorts(sortOptions)
63+
.build();
64+
}
65+
66+
@Bean
67+
public MongoItemWriter<Person> mongoItemWriter(MongoTemplate mongoTemplate) {
68+
return new MongoItemWriterBuilder<Person>()
69+
.template(mongoTemplate)
70+
.collection("person_out")
71+
.build();
72+
}
73+
74+
@Bean
75+
public Step step(MongoItemReader<Person> mongoItemReader, MongoItemWriter<Person> mongoItemWriter) {
76+
return this.stepBuilderFactory.get("step")
77+
.<Person, Person>chunk(2)
78+
.reader(mongoItemReader)
79+
.writer(mongoItemWriter)
80+
.build();
81+
}
82+
83+
@Bean
84+
public Job job(Step step) {
85+
return this.jobBuilderFactory.get("job")
86+
.start(step)
87+
.build();
88+
}
89+
90+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2020 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.sample.mongodb;
17+
18+
import com.mongodb.client.MongoClient;
19+
import com.mongodb.client.MongoClients;
20+
21+
import org.springframework.beans.factory.annotation.Value;
22+
import org.springframework.context.annotation.Bean;
23+
import org.springframework.context.annotation.Configuration;
24+
import org.springframework.context.annotation.PropertySource;
25+
import org.springframework.data.mongodb.core.MongoTemplate;
26+
27+
@Configuration
28+
@PropertySource("classpath:/mongodb-sample.properties")
29+
public class MongoDBConfiguration {
30+
31+
@Value("${mongodb.host}")
32+
private String mongodbHost;
33+
34+
@Value("${mongodb.port}")
35+
private int mongodbPort;
36+
37+
@Value("${mongodb.database}")
38+
private String mongodbDatabase;
39+
40+
@Bean
41+
public MongoTemplate mongoTemplate() {
42+
String connectionString = "mongodb://" +
43+
this.mongodbHost + ":" + this.mongodbPort + "/" + this.mongodbDatabase;
44+
MongoClient mongoClient = MongoClients.create(connectionString);
45+
return new MongoTemplate(mongoClient, "test");
46+
}
47+
48+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2020 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.sample.mongodb;
17+
18+
import java.util.Arrays;
19+
import java.util.List;
20+
21+
import com.mongodb.client.MongoCollection;
22+
import org.bson.Document;
23+
24+
import org.springframework.batch.core.Job;
25+
import org.springframework.batch.core.JobParameters;
26+
import org.springframework.batch.core.launch.JobLauncher;
27+
import org.springframework.context.ApplicationContext;
28+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
29+
import org.springframework.data.mongodb.core.MongoTemplate;
30+
31+
/**
32+
* Ensure a MongoDB instance is running on "localhost:27017",
33+
* otherwise modify mongodb-sample.properties file as needed.
34+
*
35+
* If you use docker, you can run a mongo db server with:
36+
* "docker run --name mongodb --rm -d -p 27017:27017 mongo"
37+
*
38+
* @author Mahmoud Ben Hassine
39+
*/
40+
public class MongoDBSampleApp {
41+
42+
public static void main(String[] args) throws Exception {
43+
Class<?>[] configurationClasses = {JobConfiguration.class, MongoDBConfiguration.class};
44+
ApplicationContext context = new AnnotationConfigApplicationContext(configurationClasses);
45+
MongoTemplate mongoTemplate = context.getBean(MongoTemplate.class);
46+
47+
// clear collections and insert some documents in "person_in"
48+
MongoCollection<Document> personsIn = mongoTemplate.getCollection("person_in");
49+
MongoCollection<Document> personsOut = mongoTemplate.getCollection("person_out");
50+
personsIn.deleteMany(new Document());
51+
personsOut.deleteMany(new Document());
52+
personsIn.insertMany(Arrays.asList(
53+
new Document("name", "foo1"),
54+
new Document("name", "foo2"),
55+
new Document("name", "foo3"),
56+
new Document("name", "foo4"))
57+
);
58+
59+
// run the job
60+
JobLauncher jobLauncher = context.getBean(JobLauncher.class);
61+
Job job = context.getBean(Job.class);
62+
jobLauncher.run(job, new JobParameters());
63+
64+
// check results
65+
List<Person> persons = mongoTemplate.findAll(Person.class, "person_out");
66+
System.out.println("Checking persons in person_out collection");
67+
for (Person person : persons) {
68+
System.out.println(person);
69+
}
70+
}
71+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2020 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.sample.mongodb;
17+
18+
public class Person {
19+
20+
private String name;
21+
22+
public Person() {
23+
}
24+
25+
public Person(String name) {
26+
this.name = name;
27+
}
28+
29+
public String getName() {
30+
return name;
31+
}
32+
33+
public void setName(String name) {
34+
this.name = name;
35+
}
36+
37+
@Override
38+
public String toString() {
39+
return "Person{" +
40+
"name='" + name + '\'' +
41+
'}';
42+
}
43+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mongodb.host=127.0.0.1
2+
mongodb.port=27017
3+
mongodb.database=test

0 commit comments

Comments
 (0)