diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/samples/mongodb/DeletionJobConfiguration.java b/spring-batch-samples/src/main/java/org/springframework/batch/samples/mongodb/DeletionJobConfiguration.java index 42dcd42d9f..ca50af5af3 100644 --- a/spring-batch-samples/src/main/java/org/springframework/batch/samples/mongodb/DeletionJobConfiguration.java +++ b/spring-batch-samples/src/main/java/org/springframework/batch/samples/mongodb/DeletionJobConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2020-2022 the original author or authors. + * Copyright 2020-2024 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. @@ -20,14 +20,13 @@ 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.job.builder.JobBuilder; import org.springframework.batch.core.repository.JobRepository; import org.springframework.batch.core.step.builder.StepBuilder; -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.MongoPagingItemReader; import org.springframework.batch.item.data.builder.MongoItemWriterBuilder; +import org.springframework.batch.item.data.builder.MongoPagingItemReaderBuilder; import org.springframework.context.annotation.Bean; import org.springframework.data.domain.Sort; import org.springframework.data.mongodb.core.MongoTemplate; @@ -38,18 +37,17 @@ /** * This job will remove document "foo3" from collection "person_out" using - * {@link MongoItemWriter#setDelete(boolean)}. + * {@link MongoItemWriter#setMode(MongoItemWriter.Mode)}}. * * @author Mahmoud Ben Hassine */ -@EnableBatchProcessing public class DeletionJobConfiguration { @Bean - public MongoItemReader mongoPersonReader(MongoTemplate mongoTemplate) { + public MongoPagingItemReader mongoPersonReader(MongoTemplate mongoTemplate) { Map sortOptions = new HashMap<>(); sortOptions.put("name", Sort.Direction.DESC); - return new MongoItemReaderBuilder().name("personItemReader") + return new MongoPagingItemReaderBuilder().name("personItemReader") .collection("person_out") .targetType(Person.class) .template(mongoTemplate) @@ -61,14 +59,14 @@ public MongoItemReader mongoPersonReader(MongoTemplate mongoTemplate) { @Bean public MongoItemWriter mongoPersonRemover(MongoTemplate mongoTemplate) { return new MongoItemWriterBuilder().template(mongoTemplate) - .delete(true) + .mode(MongoItemWriter.Mode.REMOVE) .collection("person_out") .build(); } @Bean public Step deletionStep(JobRepository jobRepository, PlatformTransactionManager transactionManager, - MongoItemReader mongoPersonReader, MongoItemWriter mongoPersonRemover) { + MongoPagingItemReader mongoPersonReader, MongoItemWriter mongoPersonRemover) { return new StepBuilder("step", jobRepository).chunk(2, transactionManager) .reader(mongoPersonReader) .writer(mongoPersonRemover) diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/samples/mongodb/InsertionJobConfiguration.java b/spring-batch-samples/src/main/java/org/springframework/batch/samples/mongodb/InsertionJobConfiguration.java index 1e1488d50b..8bbf2b0932 100644 --- a/spring-batch-samples/src/main/java/org/springframework/batch/samples/mongodb/InsertionJobConfiguration.java +++ b/spring-batch-samples/src/main/java/org/springframework/batch/samples/mongodb/InsertionJobConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2020-2022 the original author or authors. + * Copyright 2020-2024 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. @@ -20,14 +20,13 @@ 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.job.builder.JobBuilder; import org.springframework.batch.core.repository.JobRepository; import org.springframework.batch.core.step.builder.StepBuilder; -import org.springframework.batch.item.data.MongoItemReader; +import org.springframework.batch.item.data.MongoPagingItemReader; 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.batch.item.data.builder.MongoPagingItemReaderBuilder; import org.springframework.context.annotation.Bean; import org.springframework.data.domain.Sort; import org.springframework.data.mongodb.core.MongoTemplate; @@ -35,18 +34,17 @@ /** * This job will copy documents from collection "person_in" into collection "person_out" - * using {@link MongoItemReader} and {@link MongoItemWriter}. + * using {@link MongoPagingItemReader} and {@link MongoItemWriter}. * * @author Mahmoud Ben Hassine */ -@EnableBatchProcessing public class InsertionJobConfiguration { @Bean - public MongoItemReader mongoItemReader(MongoTemplate mongoTemplate) { + public MongoPagingItemReader mongoItemReader(MongoTemplate mongoTemplate) { Map sortOptions = new HashMap<>(); sortOptions.put("name", Sort.Direction.DESC); - return new MongoItemReaderBuilder().name("personItemReader") + return new MongoPagingItemReaderBuilder().name("personItemReader") .collection("person_in") .targetType(Person.class) .template(mongoTemplate) @@ -62,7 +60,7 @@ public MongoItemWriter mongoItemWriter(MongoTemplate mongoTemplate) { @Bean public Step step(JobRepository jobRepository, PlatformTransactionManager transactionManager, - MongoItemReader mongoItemReader, MongoItemWriter mongoItemWriter) { + MongoPagingItemReader mongoItemReader, MongoItemWriter mongoItemWriter) { return new StepBuilder("step", jobRepository).chunk(2, transactionManager) .reader(mongoItemReader) .writer(mongoItemWriter) diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/samples/mongodb/MongoDBConfiguration.java b/spring-batch-samples/src/main/java/org/springframework/batch/samples/mongodb/MongoDBConfiguration.java index 4695a21fd7..45b2994f3a 100644 --- a/spring-batch-samples/src/main/java/org/springframework/batch/samples/mongodb/MongoDBConfiguration.java +++ b/spring-batch-samples/src/main/java/org/springframework/batch/samples/mongodb/MongoDBConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2020-2022 the original author or authors. + * Copyright 2020-2024 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. @@ -18,6 +18,11 @@ import com.mongodb.client.MongoClient; import com.mongodb.client.MongoClients; +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; +import org.springframework.batch.core.explore.JobExplorer; +import org.springframework.batch.core.explore.support.MongoJobExplorerFactoryBean; +import org.springframework.batch.core.repository.JobRepository; +import org.springframework.batch.core.repository.support.MongoJobRepositoryFactoryBean; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -26,9 +31,11 @@ import org.springframework.data.mongodb.MongoTransactionManager; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory; +import org.springframework.data.mongodb.core.convert.MappingMongoConverter; @Configuration @PropertySource("classpath:/org/springframework/batch/samples/mongodb/mongodb-sample.properties") +@EnableBatchProcessing public class MongoDBConfiguration { @Value("${mongodb.host}") @@ -48,7 +55,10 @@ public MongoClient mongoClient() { @Bean public MongoTemplate mongoTemplate(MongoClient mongoClient) { - return new MongoTemplate(mongoClient, "test"); + MongoTemplate mongoTemplate = new MongoTemplate(mongoClient, "test"); + MappingMongoConverter converter = (MappingMongoConverter) mongoTemplate.getConverter(); + converter.setMapKeyDotReplacement("."); + return mongoTemplate; } @Bean @@ -61,4 +71,24 @@ public MongoTransactionManager transactionManager(MongoDatabaseFactory mongoData return new MongoTransactionManager(mongoDatabaseFactory); } + @Bean + public JobRepository jobRepository(MongoTemplate mongoTemplate, MongoTransactionManager transactionManager) + throws Exception { + MongoJobRepositoryFactoryBean jobRepositoryFactoryBean = new MongoJobRepositoryFactoryBean(); + jobRepositoryFactoryBean.setMongoOperations(mongoTemplate); + jobRepositoryFactoryBean.setTransactionManager(transactionManager); + jobRepositoryFactoryBean.afterPropertiesSet(); + return jobRepositoryFactoryBean.getObject(); + } + + @Bean + public JobExplorer jobExplorer(MongoTemplate mongoTemplate, MongoTransactionManager transactionManager) + throws Exception { + MongoJobExplorerFactoryBean jobExplorerFactoryBean = new MongoJobExplorerFactoryBean(); + jobExplorerFactoryBean.setMongoOperations(mongoTemplate); + jobExplorerFactoryBean.setTransactionManager(transactionManager); + jobExplorerFactoryBean.afterPropertiesSet(); + return jobExplorerFactoryBean.getObject(); + } + } diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/samples/mongodb/MongoDBSampleApp.java b/spring-batch-samples/src/main/java/org/springframework/batch/samples/mongodb/MongoDBSampleApp.java index ae7b53bbb2..7fc8e52f5d 100644 --- a/spring-batch-samples/src/main/java/org/springframework/batch/samples/mongodb/MongoDBSampleApp.java +++ b/spring-batch-samples/src/main/java/org/springframework/batch/samples/mongodb/MongoDBSampleApp.java @@ -1,5 +1,5 @@ /* - * Copyright 2020-2023 the original author or authors. + * Copyright 2020-2024 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. @@ -17,6 +17,7 @@ import java.util.Arrays; import java.util.List; +import java.util.Map; import com.mongodb.client.MongoCollection; import org.bson.Document; @@ -45,6 +46,18 @@ public static void main(String[] args) throws Exception { ApplicationContext context = new AnnotationConfigApplicationContext(configurationClasses); MongoTemplate mongoTemplate = context.getBean(MongoTemplate.class); + // create meta-data collections and sequences + mongoTemplate.createCollection("BATCH_JOB_INSTANCE"); + mongoTemplate.createCollection("BATCH_JOB_EXECUTION"); + mongoTemplate.createCollection("BATCH_STEP_EXECUTION"); + mongoTemplate.createCollection("BATCH_SEQUENCES"); + mongoTemplate.getCollection("BATCH_SEQUENCES") + .insertOne(new Document(Map.of("_id", "BATCH_JOB_INSTANCE_SEQ", "count", 0L))); + mongoTemplate.getCollection("BATCH_SEQUENCES") + .insertOne(new Document(Map.of("_id", "BATCH_JOB_EXECUTION_SEQ", "count", 0L))); + mongoTemplate.getCollection("BATCH_SEQUENCES") + .insertOne(new Document(Map.of("_id", "BATCH_STEP_EXECUTION_SEQ", "count", 0L))); + // clear collections and insert some documents in "person_in" MongoCollection personsIn = mongoTemplate.getCollection("person_in"); MongoCollection personsOut = mongoTemplate.getCollection("person_out");