Skip to content

Commit

Permalink
Update MongoDB sample
Browse files Browse the repository at this point in the history
  • Loading branch information
fmbenhassine committed Nov 21, 2024
1 parent 50a41a3 commit 6931060
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand All @@ -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<Person> mongoPersonReader(MongoTemplate mongoTemplate) {
public MongoPagingItemReader<Person> mongoPersonReader(MongoTemplate mongoTemplate) {
Map<String, Sort.Direction> sortOptions = new HashMap<>();
sortOptions.put("name", Sort.Direction.DESC);
return new MongoItemReaderBuilder<Person>().name("personItemReader")
return new MongoPagingItemReaderBuilder<Person>().name("personItemReader")
.collection("person_out")
.targetType(Person.class)
.template(mongoTemplate)
Expand All @@ -61,14 +59,14 @@ public MongoItemReader<Person> mongoPersonReader(MongoTemplate mongoTemplate) {
@Bean
public MongoItemWriter<Person> mongoPersonRemover(MongoTemplate mongoTemplate) {
return new MongoItemWriterBuilder<Person>().template(mongoTemplate)
.delete(true)
.mode(MongoItemWriter.Mode.REMOVE)
.collection("person_out")
.build();
}

@Bean
public Step deletionStep(JobRepository jobRepository, PlatformTransactionManager transactionManager,
MongoItemReader<Person> mongoPersonReader, MongoItemWriter<Person> mongoPersonRemover) {
MongoPagingItemReader<Person> mongoPersonReader, MongoItemWriter<Person> mongoPersonRemover) {
return new StepBuilder("step", jobRepository).<Person, Person>chunk(2, transactionManager)
.reader(mongoPersonReader)
.writer(mongoPersonRemover)
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -20,33 +20,31 @@

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;
import org.springframework.transaction.PlatformTransactionManager;

/**
* 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<Person> mongoItemReader(MongoTemplate mongoTemplate) {
public MongoPagingItemReader<Person> mongoItemReader(MongoTemplate mongoTemplate) {
Map<String, Sort.Direction> sortOptions = new HashMap<>();
sortOptions.put("name", Sort.Direction.DESC);
return new MongoItemReaderBuilder<Person>().name("personItemReader")
return new MongoPagingItemReaderBuilder<Person>().name("personItemReader")
.collection("person_in")
.targetType(Person.class)
.template(mongoTemplate)
Expand All @@ -62,7 +60,7 @@ public MongoItemWriter<Person> mongoItemWriter(MongoTemplate mongoTemplate) {

@Bean
public Step step(JobRepository jobRepository, PlatformTransactionManager transactionManager,
MongoItemReader<Person> mongoItemReader, MongoItemWriter<Person> mongoItemWriter) {
MongoPagingItemReader<Person> mongoItemReader, MongoItemWriter<Person> mongoItemWriter) {
return new StepBuilder("step", jobRepository).<Person, Person>chunk(2, transactionManager)
.reader(mongoItemReader)
.writer(mongoItemWriter)
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand All @@ -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}")
Expand All @@ -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
Expand All @@ -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();
}

}
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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<Document> personsIn = mongoTemplate.getCollection("person_in");
MongoCollection<Document> personsOut = mongoTemplate.getCollection("person_out");
Expand Down

0 comments on commit 6931060

Please sign in to comment.