From ce28bf6f97bf45d0fb0433334d11e70ef96237e3 Mon Sep 17 00:00:00 2001 From: Moncef AOUDIA Date: Fri, 9 Feb 2024 01:04:33 +0100 Subject: [PATCH] refactor: switch to Java Records configuration enable Virtual Threads --- .../.run/Application.run.xml | 14 ++ .../.run/CollectionServiceTest.run.xml | 23 +++ .../pom.xml | 139 +++++++++++------- .../com/maoudia/tutorial/AppProperties.java | 92 ++++++------ .../com/maoudia/tutorial/Application.java | 3 +- .../maoudia/tutorial/CollectionService.java | 21 ++- .../tutorial/CollectionServiceTest.java | 13 +- 7 files changed, 190 insertions(+), 115 deletions(-) create mode 100644 bulk-update-with-spring-data-mongodb-reactive/.run/Application.run.xml create mode 100644 bulk-update-with-spring-data-mongodb-reactive/.run/CollectionServiceTest.run.xml diff --git a/bulk-update-with-spring-data-mongodb-reactive/.run/Application.run.xml b/bulk-update-with-spring-data-mongodb-reactive/.run/Application.run.xml new file mode 100644 index 0000000..da7ac69 --- /dev/null +++ b/bulk-update-with-spring-data-mongodb-reactive/.run/Application.run.xml @@ -0,0 +1,14 @@ + + + + \ No newline at end of file diff --git a/bulk-update-with-spring-data-mongodb-reactive/.run/CollectionServiceTest.run.xml b/bulk-update-with-spring-data-mongodb-reactive/.run/CollectionServiceTest.run.xml new file mode 100644 index 0000000..b12f535 --- /dev/null +++ b/bulk-update-with-spring-data-mongodb-reactive/.run/CollectionServiceTest.run.xml @@ -0,0 +1,23 @@ + + + + + + + + + \ No newline at end of file diff --git a/bulk-update-with-spring-data-mongodb-reactive/pom.xml b/bulk-update-with-spring-data-mongodb-reactive/pom.xml index 6677b1d..a093b11 100644 --- a/bulk-update-with-spring-data-mongodb-reactive/pom.xml +++ b/bulk-update-with-spring-data-mongodb-reactive/pom.xml @@ -1,58 +1,87 @@ - - 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 3.1.5 - - - com.maoudia - bulk-update-with-spring-data-mongodb-reactive - 0.0.1-SNAPSHOT - Bulk Update with Spring data MongoDB Reactive - Bulk Update with Spring data MongoDB Reactive - - 21 - - - - org.springframework.boot - spring-boot-starter-data-mongodb-reactive - - - org.springframework.boot - spring-boot-starter-webflux - + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.2.2 + + + com.maoudia + bulk-update-with-spring-data-mongodb-reactive + 0.0.1-SNAPSHOT + Bulk Update with Spring data MongoDB Reactive + Bulk Update with Spring data MongoDB Reactive + + 21 + 4.1.76.Final + + + + org.springframework.boot + spring-boot-starter-webflux + + + org.springframework.boot + spring-boot-starter-data-mongodb-reactive + + + org.springframework.boot + spring-boot-starter-validation + + + org.springframework.boot + spring-boot-configuration-processor + true + - - org.springframework.boot - spring-boot-starter-test - test - - - io.projectreactor - reactor-test - test - - - org.testcontainers - junit-jupiter - test - - - org.testcontainers - mongodb - test - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - + + org.springframework.boot + spring-boot-starter-test + test + + + io.projectreactor + reactor-test + test + + + org.testcontainers + junit-jupiter + test + + + org.testcontainers + mongodb + test + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + macos-arm + + + mac + aarch64 + + + + + io.netty + netty-resolver-dns-native-macos + ${netty-resolver-dns-native-macos.version} + osx-aarch_64 + + + + diff --git a/bulk-update-with-spring-data-mongodb-reactive/src/main/java/com/maoudia/tutorial/AppProperties.java b/bulk-update-with-spring-data-mongodb-reactive/src/main/java/com/maoudia/tutorial/AppProperties.java index 43c13f8..5b94a9d 100644 --- a/bulk-update-with-spring-data-mongodb-reactive/src/main/java/com/maoudia/tutorial/AppProperties.java +++ b/bulk-update-with-spring-data-mongodb-reactive/src/main/java/com/maoudia/tutorial/AppProperties.java @@ -1,52 +1,52 @@ package com.maoudia.tutorial; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Positive; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.bind.DefaultValue; +import org.springframework.validation.annotation.Validated; +import java.net.URI; + +/** + * Configuration properties for the application. + * These properties are bound from the application.yml (or application.properties) file using the specified prefix. + */ @ConfigurationProperties(prefix = "app") -public class AppProperties{ - private int bulkSize; - private int bufferMaxSize; - private String collectionName; - private String enrichingKey; - private String enrichingUri; - - public int getBulkSize() { - return bulkSize; - } - - public void setBulkSize(final int bulkSize) { - this.bulkSize = bulkSize; - } - - public int getBufferMaxSize() { - return bufferMaxSize; - } - - public void setBufferMaxSize(final int bufferMaxSize) { - this.bufferMaxSize = bufferMaxSize; - } - - public String getCollectionName() { - return collectionName; - } - - public void setCollectionName(final String collectionName) { - this.collectionName = collectionName; - } - - public String getEnrichingKey() { - return enrichingKey; - } - - public void setEnrichingKey(final String enrichingKey) { - this.enrichingKey = enrichingKey; - } - - public String getEnrichingUri() { - return enrichingUri; - } - - public void setEnrichingUri(final String enrichingUri) { - this.enrichingUri = enrichingUri; - } +@Validated +public record AppProperties( + + /* + * The bulk size configuration. + */ + @DefaultValue("128") + @Positive + int bulkSize, + + /* + * The maximum buffer size configuration. + */ + @DefaultValue("1024") + @Positive + int bufferMaxSize, + + /* + * The name of the collection. + */ + @NotBlank + String collectionName, + + /* + * The key used for enriching. + */ + @NotBlank + String enrichingKey, + + /* + * The URI used for enriching. + */ + @NotNull + URI enrichingUri +) { } \ No newline at end of file diff --git a/bulk-update-with-spring-data-mongodb-reactive/src/main/java/com/maoudia/tutorial/Application.java b/bulk-update-with-spring-data-mongodb-reactive/src/main/java/com/maoudia/tutorial/Application.java index 1565716..0c81cf6 100644 --- a/bulk-update-with-spring-data-mongodb-reactive/src/main/java/com/maoudia/tutorial/Application.java +++ b/bulk-update-with-spring-data-mongodb-reactive/src/main/java/com/maoudia/tutorial/Application.java @@ -8,6 +8,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.data.mongo.MongoReactiveRepositoriesAutoConfiguration; import org.springframework.boot.context.properties.ConfigurationPropertiesScan; +import reactor.core.scheduler.Schedulers; @SpringBootApplication(exclude = MongoReactiveRepositoriesAutoConfiguration.class) @ConfigurationPropertiesScan("com.maoudia.tutorial") @@ -28,7 +29,7 @@ public Application(AppProperties properties, CollectionService service) { @Override public void run(final String... args) { - service.enrichAll(properties.getCollectionName(), properties.getEnrichingKey(), properties.getEnrichingUri()) + service.enrichAll(properties.collectionName(), properties.enrichingKey(), properties.enrichingUri()) .doOnSubscribe(unused -> LOGGER.info("------------------< Staring Collection Enriching Command >-------------------")) .doOnNext(bulkWriteResult -> LOGGER.info("Bulk write result with {} modified document(s)", bulkWriteResult.getModifiedCount())) .doOnError(throwable -> { diff --git a/bulk-update-with-spring-data-mongodb-reactive/src/main/java/com/maoudia/tutorial/CollectionService.java b/bulk-update-with-spring-data-mongodb-reactive/src/main/java/com/maoudia/tutorial/CollectionService.java index 79fa287..30db42a 100644 --- a/bulk-update-with-spring-data-mongodb-reactive/src/main/java/com/maoudia/tutorial/CollectionService.java +++ b/bulk-update-with-spring-data-mongodb-reactive/src/main/java/com/maoudia/tutorial/CollectionService.java @@ -42,15 +42,20 @@ public CollectionService(AppProperties properties, this.client = client; } - public Flux enrichAll(String collectionName, String enrichingKey, String enrichingUri) { + public Flux enrichAll(String collectionName, + String enrichingKey, + URI enrichingUri) { return template.findAll(Document.class, collectionName) - .onBackpressureBuffer(properties.getBufferMaxSize()) + .onBackpressureBuffer(properties.bufferMaxSize()) .flatMap(document -> enrich(document, enrichingKey, enrichingUri)) .map(CollectionService::toReplaceOneModel) - .window(properties.getBulkSize()) + .window(properties.bulkSize()) .flatMap(replaceOneModelFlux -> bulkWrite(replaceOneModelFlux, collectionName)); } - private Publisher enrich(Document document, String enrichingKey, String enrichingUri) { + + private Publisher enrich(Document document, + String enrichingKey, + URI enrichingUri) { return getEnrichingDocument(enrichingUri) .map(enrichingDocument -> { document.put(enrichingKey, enrichingDocument); @@ -59,13 +64,15 @@ private Publisher enrich(Document document, String enrichingKey, Strin }); } - private Mono getEnrichingDocument(String enrichingUri) { + private Mono getEnrichingDocument(URI enrichingUri) { return client.get() - .uri(URI.create(enrichingUri)) + .uri(enrichingUri) .retrieve() .bodyToMono(Document.class); } - private Flux bulkWrite(Flux> updateOneModelFlux, String collectionName) { + + private Flux bulkWrite(Flux> updateOneModelFlux, + String collectionName) { return updateOneModelFlux .collectList() .flatMapMany(updateOneModels -> template.getCollection(collectionName) diff --git a/bulk-update-with-spring-data-mongodb-reactive/src/test/java/com/maoudia/tutorial/CollectionServiceTest.java b/bulk-update-with-spring-data-mongodb-reactive/src/test/java/com/maoudia/tutorial/CollectionServiceTest.java index 5b6182d..da66eb6 100644 --- a/bulk-update-with-spring-data-mongodb-reactive/src/test/java/com/maoudia/tutorial/CollectionServiceTest.java +++ b/bulk-update-with-spring-data-mongodb-reactive/src/test/java/com/maoudia/tutorial/CollectionServiceTest.java @@ -3,18 +3,18 @@ import com.mongodb.bulk.BulkWriteResult; import com.mongodb.internal.bulk.WriteRequest; import org.bson.Document; -import org.junit.ClassRule; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.mongodb.core.ReactiveMongoTemplate; import org.springframework.test.context.DynamicPropertyRegistry; import org.springframework.test.context.DynamicPropertySource; +import org.testcontainers.containers.BindMode; import org.testcontainers.containers.GenericContainer; import org.testcontainers.containers.MongoDBContainer; +import org.testcontainers.containers.wait.strategy.Wait; import org.testcontainers.junit.jupiter.Container; import org.testcontainers.junit.jupiter.Testcontainers; -import org.testcontainers.utility.DockerImageName; import reactor.test.StepVerifier; import java.util.Arrays; @@ -24,11 +24,12 @@ @Testcontainers class CollectionServiceTest { - @Container public static GenericContainer jsonServerContainer = new GenericContainer<>("clue/json-server:latest") .withExposedPorts(80) - .withFileSystemBind("./data/product/db.json", "/data/db.json"); + .withFileSystemBind("./data/product/db.json", "/data/db.json", BindMode.READ_ONLY) + .waitingFor(Wait.forHttp("/").forStatusCode(200).forPort(80)) + .withReuse(true); @Container private static final MongoDBContainer mongoDBContainer = new MongoDBContainer("mongo:7.0.2"); @@ -57,14 +58,14 @@ void multipleBulkWriteResultsAreReturned() { Document givenDocument3 = new Document(); givenDocument3.put("_id", "628ea3edb5110304e5e814f8"); givenDocument3.put("type", "housenumber"); - template.insert(Arrays.asList(givenDocument1, givenDocument2, givenDocument3), properties.getCollectionName()).blockLast(); + template.insert(Arrays.asList(givenDocument1, givenDocument2, givenDocument3), properties.collectionName()).blockLast(); BulkWriteResult expectedBulkWriteResult1 = BulkWriteResult.acknowledged(WriteRequest.Type.REPLACE, 2, 2, Collections.emptyList(), Collections.emptyList()); BulkWriteResult expectedBulkWriteResult2 = BulkWriteResult.acknowledged(WriteRequest.Type.REPLACE, 1, 1, Collections.emptyList(), Collections.emptyList()); - command.enrichAll(properties.getCollectionName(), properties.getEnrichingKey() , properties.getEnrichingUri()) + command.enrichAll(properties.collectionName(), properties.enrichingKey() , properties.enrichingUri()) .as(StepVerifier::create) .expectNext(expectedBulkWriteResult1) .expectNext(expectedBulkWriteResult2)