Skip to content

Commit 6931060

Browse files
committed
Update MongoDB sample
1 parent 50a41a3 commit 6931060

File tree

4 files changed

+61
-22
lines changed

4 files changed

+61
-22
lines changed

spring-batch-samples/src/main/java/org/springframework/batch/samples/mongodb/DeletionJobConfiguration.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2022 the original author or authors.
2+
* Copyright 2020-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,14 +20,13 @@
2020

2121
import org.springframework.batch.core.Job;
2222
import org.springframework.batch.core.Step;
23-
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
2423
import org.springframework.batch.core.job.builder.JobBuilder;
2524
import org.springframework.batch.core.repository.JobRepository;
2625
import org.springframework.batch.core.step.builder.StepBuilder;
27-
import org.springframework.batch.item.data.MongoItemReader;
2826
import org.springframework.batch.item.data.MongoItemWriter;
29-
import org.springframework.batch.item.data.builder.MongoItemReaderBuilder;
27+
import org.springframework.batch.item.data.MongoPagingItemReader;
3028
import org.springframework.batch.item.data.builder.MongoItemWriterBuilder;
29+
import org.springframework.batch.item.data.builder.MongoPagingItemReaderBuilder;
3130
import org.springframework.context.annotation.Bean;
3231
import org.springframework.data.domain.Sort;
3332
import org.springframework.data.mongodb.core.MongoTemplate;
@@ -38,18 +37,17 @@
3837

3938
/**
4039
* This job will remove document "foo3" from collection "person_out" using
41-
* {@link MongoItemWriter#setDelete(boolean)}.
40+
* {@link MongoItemWriter#setMode(MongoItemWriter.Mode)}}.
4241
*
4342
* @author Mahmoud Ben Hassine
4443
*/
45-
@EnableBatchProcessing
4644
public class DeletionJobConfiguration {
4745

4846
@Bean
49-
public MongoItemReader<Person> mongoPersonReader(MongoTemplate mongoTemplate) {
47+
public MongoPagingItemReader<Person> mongoPersonReader(MongoTemplate mongoTemplate) {
5048
Map<String, Sort.Direction> sortOptions = new HashMap<>();
5149
sortOptions.put("name", Sort.Direction.DESC);
52-
return new MongoItemReaderBuilder<Person>().name("personItemReader")
50+
return new MongoPagingItemReaderBuilder<Person>().name("personItemReader")
5351
.collection("person_out")
5452
.targetType(Person.class)
5553
.template(mongoTemplate)
@@ -61,14 +59,14 @@ public MongoItemReader<Person> mongoPersonReader(MongoTemplate mongoTemplate) {
6159
@Bean
6260
public MongoItemWriter<Person> mongoPersonRemover(MongoTemplate mongoTemplate) {
6361
return new MongoItemWriterBuilder<Person>().template(mongoTemplate)
64-
.delete(true)
62+
.mode(MongoItemWriter.Mode.REMOVE)
6563
.collection("person_out")
6664
.build();
6765
}
6866

6967
@Bean
7068
public Step deletionStep(JobRepository jobRepository, PlatformTransactionManager transactionManager,
71-
MongoItemReader<Person> mongoPersonReader, MongoItemWriter<Person> mongoPersonRemover) {
69+
MongoPagingItemReader<Person> mongoPersonReader, MongoItemWriter<Person> mongoPersonRemover) {
7270
return new StepBuilder("step", jobRepository).<Person, Person>chunk(2, transactionManager)
7371
.reader(mongoPersonReader)
7472
.writer(mongoPersonRemover)

spring-batch-samples/src/main/java/org/springframework/batch/samples/mongodb/InsertionJobConfiguration.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2022 the original author or authors.
2+
* Copyright 2020-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,33 +20,31 @@
2020

2121
import org.springframework.batch.core.Job;
2222
import org.springframework.batch.core.Step;
23-
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
2423
import org.springframework.batch.core.job.builder.JobBuilder;
2524
import org.springframework.batch.core.repository.JobRepository;
2625
import org.springframework.batch.core.step.builder.StepBuilder;
27-
import org.springframework.batch.item.data.MongoItemReader;
26+
import org.springframework.batch.item.data.MongoPagingItemReader;
2827
import org.springframework.batch.item.data.MongoItemWriter;
29-
import org.springframework.batch.item.data.builder.MongoItemReaderBuilder;
3028
import org.springframework.batch.item.data.builder.MongoItemWriterBuilder;
29+
import org.springframework.batch.item.data.builder.MongoPagingItemReaderBuilder;
3130
import org.springframework.context.annotation.Bean;
3231
import org.springframework.data.domain.Sort;
3332
import org.springframework.data.mongodb.core.MongoTemplate;
3433
import org.springframework.transaction.PlatformTransactionManager;
3534

3635
/**
3736
* This job will copy documents from collection "person_in" into collection "person_out"
38-
* using {@link MongoItemReader} and {@link MongoItemWriter}.
37+
* using {@link MongoPagingItemReader} and {@link MongoItemWriter}.
3938
*
4039
* @author Mahmoud Ben Hassine
4140
*/
42-
@EnableBatchProcessing
4341
public class InsertionJobConfiguration {
4442

4543
@Bean
46-
public MongoItemReader<Person> mongoItemReader(MongoTemplate mongoTemplate) {
44+
public MongoPagingItemReader<Person> mongoItemReader(MongoTemplate mongoTemplate) {
4745
Map<String, Sort.Direction> sortOptions = new HashMap<>();
4846
sortOptions.put("name", Sort.Direction.DESC);
49-
return new MongoItemReaderBuilder<Person>().name("personItemReader")
47+
return new MongoPagingItemReaderBuilder<Person>().name("personItemReader")
5048
.collection("person_in")
5149
.targetType(Person.class)
5250
.template(mongoTemplate)
@@ -62,7 +60,7 @@ public MongoItemWriter<Person> mongoItemWriter(MongoTemplate mongoTemplate) {
6260

6361
@Bean
6462
public Step step(JobRepository jobRepository, PlatformTransactionManager transactionManager,
65-
MongoItemReader<Person> mongoItemReader, MongoItemWriter<Person> mongoItemWriter) {
63+
MongoPagingItemReader<Person> mongoItemReader, MongoItemWriter<Person> mongoItemWriter) {
6664
return new StepBuilder("step", jobRepository).<Person, Person>chunk(2, transactionManager)
6765
.reader(mongoItemReader)
6866
.writer(mongoItemWriter)

spring-batch-samples/src/main/java/org/springframework/batch/samples/mongodb/MongoDBConfiguration.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2022 the original author or authors.
2+
* Copyright 2020-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,11 @@
1818
import com.mongodb.client.MongoClient;
1919
import com.mongodb.client.MongoClients;
2020

21+
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
22+
import org.springframework.batch.core.explore.JobExplorer;
23+
import org.springframework.batch.core.explore.support.MongoJobExplorerFactoryBean;
24+
import org.springframework.batch.core.repository.JobRepository;
25+
import org.springframework.batch.core.repository.support.MongoJobRepositoryFactoryBean;
2126
import org.springframework.beans.factory.annotation.Value;
2227
import org.springframework.context.annotation.Bean;
2328
import org.springframework.context.annotation.Configuration;
@@ -26,9 +31,11 @@
2631
import org.springframework.data.mongodb.MongoTransactionManager;
2732
import org.springframework.data.mongodb.core.MongoTemplate;
2833
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory;
34+
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
2935

3036
@Configuration
3137
@PropertySource("classpath:/org/springframework/batch/samples/mongodb/mongodb-sample.properties")
38+
@EnableBatchProcessing
3239
public class MongoDBConfiguration {
3340

3441
@Value("${mongodb.host}")
@@ -48,7 +55,10 @@ public MongoClient mongoClient() {
4855

4956
@Bean
5057
public MongoTemplate mongoTemplate(MongoClient mongoClient) {
51-
return new MongoTemplate(mongoClient, "test");
58+
MongoTemplate mongoTemplate = new MongoTemplate(mongoClient, "test");
59+
MappingMongoConverter converter = (MappingMongoConverter) mongoTemplate.getConverter();
60+
converter.setMapKeyDotReplacement(".");
61+
return mongoTemplate;
5262
}
5363

5464
@Bean
@@ -61,4 +71,24 @@ public MongoTransactionManager transactionManager(MongoDatabaseFactory mongoData
6171
return new MongoTransactionManager(mongoDatabaseFactory);
6272
}
6373

74+
@Bean
75+
public JobRepository jobRepository(MongoTemplate mongoTemplate, MongoTransactionManager transactionManager)
76+
throws Exception {
77+
MongoJobRepositoryFactoryBean jobRepositoryFactoryBean = new MongoJobRepositoryFactoryBean();
78+
jobRepositoryFactoryBean.setMongoOperations(mongoTemplate);
79+
jobRepositoryFactoryBean.setTransactionManager(transactionManager);
80+
jobRepositoryFactoryBean.afterPropertiesSet();
81+
return jobRepositoryFactoryBean.getObject();
82+
}
83+
84+
@Bean
85+
public JobExplorer jobExplorer(MongoTemplate mongoTemplate, MongoTransactionManager transactionManager)
86+
throws Exception {
87+
MongoJobExplorerFactoryBean jobExplorerFactoryBean = new MongoJobExplorerFactoryBean();
88+
jobExplorerFactoryBean.setMongoOperations(mongoTemplate);
89+
jobExplorerFactoryBean.setTransactionManager(transactionManager);
90+
jobExplorerFactoryBean.afterPropertiesSet();
91+
return jobExplorerFactoryBean.getObject();
92+
}
93+
6494
}

spring-batch-samples/src/main/java/org/springframework/batch/samples/mongodb/MongoDBSampleApp.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2023 the original author or authors.
2+
* Copyright 2020-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717

1818
import java.util.Arrays;
1919
import java.util.List;
20+
import java.util.Map;
2021

2122
import com.mongodb.client.MongoCollection;
2223
import org.bson.Document;
@@ -45,6 +46,18 @@ public static void main(String[] args) throws Exception {
4546
ApplicationContext context = new AnnotationConfigApplicationContext(configurationClasses);
4647
MongoTemplate mongoTemplate = context.getBean(MongoTemplate.class);
4748

49+
// create meta-data collections and sequences
50+
mongoTemplate.createCollection("BATCH_JOB_INSTANCE");
51+
mongoTemplate.createCollection("BATCH_JOB_EXECUTION");
52+
mongoTemplate.createCollection("BATCH_STEP_EXECUTION");
53+
mongoTemplate.createCollection("BATCH_SEQUENCES");
54+
mongoTemplate.getCollection("BATCH_SEQUENCES")
55+
.insertOne(new Document(Map.of("_id", "BATCH_JOB_INSTANCE_SEQ", "count", 0L)));
56+
mongoTemplate.getCollection("BATCH_SEQUENCES")
57+
.insertOne(new Document(Map.of("_id", "BATCH_JOB_EXECUTION_SEQ", "count", 0L)));
58+
mongoTemplate.getCollection("BATCH_SEQUENCES")
59+
.insertOne(new Document(Map.of("_id", "BATCH_STEP_EXECUTION_SEQ", "count", 0L)));
60+
4861
// clear collections and insert some documents in "person_in"
4962
MongoCollection<Document> personsIn = mongoTemplate.getCollection("person_in");
5063
MongoCollection<Document> personsOut = mongoTemplate.getCollection("person_out");

0 commit comments

Comments
 (0)