From 20061007c059733b8c07da7d6e715de87c70612e Mon Sep 17 00:00:00 2001 From: Mahmoud Ben Hassine Date: Wed, 8 Jan 2020 16:15:55 +0100 Subject: [PATCH] Add sample app to show MongoDB support --- build.gradle | 1 + spring-batch-samples/README.md | 17 ++++ .../sample/mongodb/JobConfiguration.java | 90 +++++++++++++++++++ .../sample/mongodb/MongoDBConfiguration.java | 48 ++++++++++ .../sample/mongodb/MongoDBSampleApp.java | 71 +++++++++++++++ .../batch/sample/mongodb/Person.java | 43 +++++++++ .../main/resources/mongodb-sample.properties | 3 + 7 files changed, 273 insertions(+) create mode 100644 spring-batch-samples/src/main/java/org/springframework/batch/sample/mongodb/JobConfiguration.java create mode 100644 spring-batch-samples/src/main/java/org/springframework/batch/sample/mongodb/MongoDBConfiguration.java create mode 100644 spring-batch-samples/src/main/java/org/springframework/batch/sample/mongodb/MongoDBSampleApp.java create mode 100644 spring-batch-samples/src/main/java/org/springframework/batch/sample/mongodb/Person.java create mode 100644 spring-batch-samples/src/main/resources/mongodb-sample.properties diff --git a/build.gradle b/build.gradle index e599837e3..e30e43828 100644 --- a/build.gradle +++ b/build.gradle @@ -636,6 +636,7 @@ project('spring-batch-samples') { optional "org.postgresql:postgresql:$postgresqlVersion" optional "org.springframework:spring-web:$springVersion" optional "org.springframework.data:spring-data-commons:$springDataCommonsVersion" + optional "org.springframework.data:spring-data-mongodb:$springDataMongodbVersion" optional "org.springframework.amqp:spring-amqp:$springAmqpVersion" optional ("org.springframework.amqp:spring-rabbit:$springAmqpVersion") { exclude group: "org.springframework", module: "spring-messaging" diff --git a/spring-batch-samples/README.md b/spring-batch-samples/README.md index db8c29524..eaed1194e 100644 --- a/spring-batch-samples/README.md +++ b/spring-batch-samples/README.md @@ -60,6 +60,7 @@ jpa | | | | multiRecordtype | | x | | | | | | | x | | | x multiResource | x | | | | | | | x | | | | x [XML Input Output](#xml-input-output) | | | x | | | | | | | x | | +[MongoDB sample](#mongodb-sample) | | | | | x | | | | x | | | ### Common Sample Source Structures @@ -986,3 +987,19 @@ and import the ready-to-use dashboard in `spring-batch-samples/src/grafana/sprin Finally, run the `org.springframework.batch.sample.metrics.BatchMetricsApplication` class without any argument to start the sample. + +# MongoDB sample + +This sample is a showcase of MongoDB support in Spring Batch. It copies data from +an input collection to an output collection using `MongoItemReader` and `MongoItemWriter`. + +To run the sample, you need to have a MongoDB server up and running on `localhost:27017` +(you can change these defaults in `mongodb-sample.properties`). If you use docker, +you can run the following command to start a MongoDB server: + +``` +$>docker run --name mongodb --rm -d -p 27017:27017 mongo +``` + +Once MongoDB is up and running, run the `org.springframework.batch.sample.mongodb.MongoDBSampleApp` +class without any argument to start the sample. diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/mongodb/JobConfiguration.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/mongodb/JobConfiguration.java new file mode 100644 index 000000000..95240504e --- /dev/null +++ b/spring-batch-samples/src/main/java/org/springframework/batch/sample/mongodb/JobConfiguration.java @@ -0,0 +1,90 @@ +/* + * Copyright 2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.batch.sample.mongodb; + +import java.util.HashMap; +import java.util.Map; + +import org.springframework.batch.core.Job; +import org.springframework.batch.core.Step; +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; +import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; +import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; +import org.springframework.batch.item.data.MongoItemReader; +import org.springframework.batch.item.data.MongoItemWriter; +import org.springframework.batch.item.data.builder.MongoItemReaderBuilder; +import org.springframework.batch.item.data.builder.MongoItemWriterBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.data.domain.Sort; +import org.springframework.data.mongodb.core.MongoTemplate; + +/** + * This sample job will copy data from collection "person_in" into collection "person_out" + * using {@link MongoItemReader} and {@link MongoItemWriter}. + * + * @author Mahmoud Ben Hassine + */ +@EnableBatchProcessing +public class JobConfiguration { + + private JobBuilderFactory jobBuilderFactory; + + private StepBuilderFactory stepBuilderFactory; + + public JobConfiguration(JobBuilderFactory jobBuilderFactory, StepBuilderFactory stepBuilderFactory) { + this.jobBuilderFactory = jobBuilderFactory; + this.stepBuilderFactory = stepBuilderFactory; + } + + @Bean + public MongoItemReader mongoItemReader(MongoTemplate mongoTemplate) { + Map sortOptions = new HashMap<>(); + sortOptions.put("name", Sort.Direction.DESC); + return new MongoItemReaderBuilder() + .name("personItemReader") + .collection("person_in") + .targetType(Person.class) + .template(mongoTemplate) + .jsonQuery("{}") + .sorts(sortOptions) + .build(); + } + + @Bean + public MongoItemWriter mongoItemWriter(MongoTemplate mongoTemplate) { + return new MongoItemWriterBuilder() + .template(mongoTemplate) + .collection("person_out") + .build(); + } + + @Bean + public Step step(MongoItemReader mongoItemReader, MongoItemWriter mongoItemWriter) { + return this.stepBuilderFactory.get("step") + .chunk(2) + .reader(mongoItemReader) + .writer(mongoItemWriter) + .build(); + } + + @Bean + public Job job(Step step) { + return this.jobBuilderFactory.get("job") + .start(step) + .build(); + } + +} diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/mongodb/MongoDBConfiguration.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/mongodb/MongoDBConfiguration.java new file mode 100644 index 000000000..d99dbcb6d --- /dev/null +++ b/spring-batch-samples/src/main/java/org/springframework/batch/sample/mongodb/MongoDBConfiguration.java @@ -0,0 +1,48 @@ +/* + * Copyright 2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.batch.sample.mongodb; + +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.data.mongodb.core.MongoTemplate; + +@Configuration +@PropertySource("classpath:/mongodb-sample.properties") +public class MongoDBConfiguration { + + @Value("${mongodb.host}") + private String mongodbHost; + + @Value("${mongodb.port}") + private int mongodbPort; + + @Value("${mongodb.database}") + private String mongodbDatabase; + + @Bean + public MongoTemplate mongoTemplate() { + String connectionString = "mongodb://" + + this.mongodbHost + ":" + this.mongodbPort + "/" + this.mongodbDatabase; + MongoClient mongoClient = MongoClients.create(connectionString); + return new MongoTemplate(mongoClient, "test"); + } + +} diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/mongodb/MongoDBSampleApp.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/mongodb/MongoDBSampleApp.java new file mode 100644 index 000000000..027e46098 --- /dev/null +++ b/spring-batch-samples/src/main/java/org/springframework/batch/sample/mongodb/MongoDBSampleApp.java @@ -0,0 +1,71 @@ +/* + * Copyright 2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.batch.sample.mongodb; + +import java.util.Arrays; +import java.util.List; + +import com.mongodb.client.MongoCollection; +import org.bson.Document; + +import org.springframework.batch.core.Job; +import org.springframework.batch.core.JobParameters; +import org.springframework.batch.core.launch.JobLauncher; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.data.mongodb.core.MongoTemplate; + +/** + * Ensure a MongoDB instance is running on "localhost:27017", + * otherwise modify mongodb-sample.properties file as needed. + * + * If you use docker, you can run a mongo db server with: + * "docker run --name mongodb --rm -d -p 27017:27017 mongo" + * + * @author Mahmoud Ben Hassine + */ +public class MongoDBSampleApp { + + public static void main(String[] args) throws Exception { + Class[] configurationClasses = {JobConfiguration.class, MongoDBConfiguration.class}; + ApplicationContext context = new AnnotationConfigApplicationContext(configurationClasses); + MongoTemplate mongoTemplate = context.getBean(MongoTemplate.class); + + // clear collections and insert some documents in "person_in" + MongoCollection personsIn = mongoTemplate.getCollection("person_in"); + MongoCollection personsOut = mongoTemplate.getCollection("person_out"); + personsIn.deleteMany(new Document()); + personsOut.deleteMany(new Document()); + personsIn.insertMany(Arrays.asList( + new Document("name", "foo1"), + new Document("name", "foo2"), + new Document("name", "foo3"), + new Document("name", "foo4")) + ); + + // run the job + JobLauncher jobLauncher = context.getBean(JobLauncher.class); + Job job = context.getBean(Job.class); + jobLauncher.run(job, new JobParameters()); + + // check results + List persons = mongoTemplate.findAll(Person.class, "person_out"); + System.out.println("Checking persons in person_out collection"); + for (Person person : persons) { + System.out.println(person); + } + } +} diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/mongodb/Person.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/mongodb/Person.java new file mode 100644 index 000000000..a98bba89b --- /dev/null +++ b/spring-batch-samples/src/main/java/org/springframework/batch/sample/mongodb/Person.java @@ -0,0 +1,43 @@ +/* + * Copyright 2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.batch.sample.mongodb; + +public class Person { + + private String name; + + public Person() { + } + + public Person(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public String toString() { + return "Person{" + + "name='" + name + '\'' + + '}'; + } +} diff --git a/spring-batch-samples/src/main/resources/mongodb-sample.properties b/spring-batch-samples/src/main/resources/mongodb-sample.properties new file mode 100644 index 000000000..5989d4b8a --- /dev/null +++ b/spring-batch-samples/src/main/resources/mongodb-sample.properties @@ -0,0 +1,3 @@ +mongodb.host=127.0.0.1 +mongodb.port=27017 +mongodb.database=test