Polish 6c5cb2b13d
* Move class Item as inner class of surrounding test * Update mongodb sample for item deletion use case
This commit is contained in:
@@ -350,15 +350,15 @@ public class MongoItemWriterTests {
|
||||
assertEquals(String.valueOf(i), results[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Item {
|
||||
Integer id;
|
||||
String name;
|
||||
public Item(Integer id){
|
||||
this.id = id;
|
||||
}
|
||||
public Item(String name) {
|
||||
this.name = name;
|
||||
static class Item {
|
||||
Integer id;
|
||||
String name;
|
||||
public Item(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
public Item(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -127,15 +127,15 @@ public class MongoItemWriterBuilderTests {
|
||||
iae.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Item {
|
||||
Integer id;
|
||||
String name;
|
||||
public Item(Integer id){
|
||||
this.id = id;
|
||||
}
|
||||
public Item(String name) {
|
||||
this.name = name;
|
||||
static class Item {
|
||||
Integer id;
|
||||
String name;
|
||||
public Item(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
public Item(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* 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;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
|
||||
import static org.springframework.data.mongodb.core.query.Criteria.where;
|
||||
|
||||
/**
|
||||
* This job will remove document "foo3" from collection "person_out"
|
||||
* using {@link MongoItemWriter#setDelete(boolean)}.
|
||||
*
|
||||
* @author Mahmoud Ben Hassine
|
||||
*/
|
||||
@EnableBatchProcessing
|
||||
public class DeletionJobConfiguration {
|
||||
|
||||
private JobBuilderFactory jobBuilderFactory;
|
||||
private StepBuilderFactory stepBuilderFactory;
|
||||
|
||||
public DeletionJobConfiguration(JobBuilderFactory jobBuilderFactory, StepBuilderFactory stepBuilderFactory) {
|
||||
this.jobBuilderFactory = jobBuilderFactory;
|
||||
this.stepBuilderFactory = stepBuilderFactory;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MongoItemReader<Person> mongoPersonReader(MongoTemplate mongoTemplate) {
|
||||
Map<String, Sort.Direction> sortOptions = new HashMap<>();
|
||||
sortOptions.put("name", Sort.Direction.DESC);
|
||||
return new MongoItemReaderBuilder<Person>()
|
||||
.name("personItemReader")
|
||||
.collection("person_out")
|
||||
.targetType(Person.class)
|
||||
.template(mongoTemplate)
|
||||
.query(new Query().addCriteria(where("name").is("foo3")))
|
||||
.sorts(sortOptions)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MongoItemWriter<Person> mongoPersonRemover(MongoTemplate mongoTemplate) {
|
||||
return new MongoItemWriterBuilder<Person>()
|
||||
.template(mongoTemplate)
|
||||
.delete(true)
|
||||
.collection("person_out")
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Step deletionStep(MongoItemReader<Person> mongoPersonReader, MongoItemWriter<Person> mongoPersonRemover) {
|
||||
return this.stepBuilderFactory.get("step")
|
||||
.<Person, Person>chunk(2)
|
||||
.reader(mongoPersonReader)
|
||||
.writer(mongoPersonRemover)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Job deletionJob(Step deletionStep) {
|
||||
return this.jobBuilderFactory.get("deletionJob")
|
||||
.start(deletionStep)
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -32,19 +32,18 @@ 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}.
|
||||
* This job will copy documents from collection "person_in" into collection
|
||||
* "person_out" using {@link MongoItemReader} and {@link MongoItemWriter}.
|
||||
*
|
||||
* @author Mahmoud Ben Hassine
|
||||
*/
|
||||
@EnableBatchProcessing
|
||||
public class JobConfiguration {
|
||||
public class InsertionJobConfiguration {
|
||||
|
||||
private JobBuilderFactory jobBuilderFactory;
|
||||
|
||||
private StepBuilderFactory stepBuilderFactory;
|
||||
|
||||
public JobConfiguration(JobBuilderFactory jobBuilderFactory, StepBuilderFactory stepBuilderFactory) {
|
||||
public InsertionJobConfiguration(JobBuilderFactory jobBuilderFactory, StepBuilderFactory stepBuilderFactory) {
|
||||
this.jobBuilderFactory = jobBuilderFactory;
|
||||
this.stepBuilderFactory = stepBuilderFactory;
|
||||
}
|
||||
@@ -81,8 +80,8 @@ public class JobConfiguration {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Job job(Step step) {
|
||||
return this.jobBuilderFactory.get("job")
|
||||
public Job insertionJob(Step step) {
|
||||
return this.jobBuilderFactory.get("insertionJob")
|
||||
.start(step)
|
||||
.build();
|
||||
}
|
||||
@@ -40,7 +40,10 @@ import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
public class MongoDBSampleApp {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
Class<?>[] configurationClasses = {JobConfiguration.class, MongoDBConfiguration.class};
|
||||
Class<?>[] configurationClasses = {
|
||||
InsertionJobConfiguration.class,
|
||||
DeletionJobConfiguration.class,
|
||||
MongoDBConfiguration.class};
|
||||
ApplicationContext context = new AnnotationConfigApplicationContext(configurationClasses);
|
||||
MongoTemplate mongoTemplate = context.getBean(MongoTemplate.class);
|
||||
|
||||
@@ -56,10 +59,10 @@ public class MongoDBSampleApp {
|
||||
new Document("name", "foo4"))
|
||||
);
|
||||
|
||||
// run the job
|
||||
// run the insertion job
|
||||
JobLauncher jobLauncher = context.getBean(JobLauncher.class);
|
||||
Job job = context.getBean(Job.class);
|
||||
jobLauncher.run(job, new JobParameters());
|
||||
Job insertionJob = context.getBean("insertionJob", Job.class);
|
||||
jobLauncher.run(insertionJob, new JobParameters());
|
||||
|
||||
// check results
|
||||
List<Person> persons = mongoTemplate.findAll(Person.class, "person_out");
|
||||
@@ -67,5 +70,16 @@ public class MongoDBSampleApp {
|
||||
for (Person person : persons) {
|
||||
System.out.println(person);
|
||||
}
|
||||
|
||||
// run the deletion job
|
||||
Job deletionJob = context.getBean("deletionJob", Job.class);
|
||||
jobLauncher.run(deletionJob, new JobParameters());
|
||||
|
||||
// check results (foo3 should have been removed)
|
||||
persons = mongoTemplate.findAll(Person.class, "person_out");
|
||||
System.out.println("Checking persons in person_out collection after deleting foo3");
|
||||
for (Person person : persons) {
|
||||
System.out.println(person);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,8 @@
|
||||
package org.springframework.batch.sample.mongodb;
|
||||
|
||||
public class Person {
|
||||
|
||||
|
||||
private String id;
|
||||
private String name;
|
||||
|
||||
public Person() {
|
||||
@@ -26,6 +27,16 @@ public class Person {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
// setter used for data binding if items to delete (with known IDs)
|
||||
// are read from a flat file for example
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
@@ -37,7 +48,8 @@ public class Person {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Person{" +
|
||||
"name='" + name + '\'' +
|
||||
"id='" + id + '\'' +
|
||||
", name='" + name + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user