Add latency to simulate long database queries.

This commit is contained in:
Florian THIERRY
2025-09-18 17:51:55 +02:00
parent 9dce18d00f
commit 2fe14de357
5 changed files with 27 additions and 0 deletions

View File

@@ -9,6 +9,7 @@ import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers; import reactor.core.scheduler.Schedulers;
import java.time.Duration;
import java.util.UUID; import java.util.UUID;
@Component @Component
@@ -31,6 +32,7 @@ public class CatalogJpaAdapter implements CatalogPort {
public Flux<Catalog> getAll() { public Flux<Catalog> getAll() {
return Mono.fromCallable(catalogJpaRepository::findAll) return Mono.fromCallable(catalogJpaRepository::findAll)
.subscribeOn(Schedulers.boundedElastic()) .subscribeOn(Schedulers.boundedElastic())
.delayElement(Duration.ofSeconds(1))
.flatMapMany(Flux::fromIterable) .flatMapMany(Flux::fromIterable)
.map(CatalogJpaEntity::toDomain); .map(CatalogJpaEntity::toDomain);
} }

View File

@@ -9,6 +9,9 @@ import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers; import reactor.core.scheduler.Schedulers;
import java.time.Duration;
@Component @Component
public class ItemJpaAdapter implements ItemPort { public class ItemJpaAdapter implements ItemPort {
private final ItemJpaRepository itemJpaRepository; private final ItemJpaRepository itemJpaRepository;
@@ -21,6 +24,7 @@ public class ItemJpaAdapter implements ItemPort {
public Flux<Item> getAll() { public Flux<Item> getAll() {
return Mono.fromCallable(itemJpaRepository::findAll) return Mono.fromCallable(itemJpaRepository::findAll)
.subscribeOn(Schedulers.boundedElastic()) .subscribeOn(Schedulers.boundedElastic())
.delayElement(Duration.ofSeconds(1))
.flatMapMany(Flux::fromIterable) .flatMapMany(Flux::fromIterable)
.map(ItemJpaEntity::toDomain); .map(ItemJpaEntity::toDomain);
} }

View File

@@ -10,6 +10,8 @@ import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import static com.zeenea.experiments.virtualthreads.virtualthreadsapp.utils.LatencyUtils.sleepOneSec;
@Component @Component
public class CatalogJpaAdapter implements CatalogPort { public class CatalogJpaAdapter implements CatalogPort {
private final CatalogJpaRepository catalogJpaRepository; private final CatalogJpaRepository catalogJpaRepository;
@@ -26,6 +28,7 @@ public class CatalogJpaAdapter implements CatalogPort {
@Override @Override
public List<Catalog> getAll() { public List<Catalog> getAll() {
sleepOneSec();
return catalogJpaRepository.findAll() return catalogJpaRepository.findAll()
.stream() .stream()
.map(CatalogJpaEntity::toDomain) .map(CatalogJpaEntity::toDomain)

View File

@@ -8,6 +8,8 @@ import org.springframework.stereotype.Component;
import java.util.List; import java.util.List;
import static com.zeenea.experiments.virtualthreads.virtualthreadsapp.utils.LatencyUtils.sleepOneSec;
@Component @Component
public class ItemJpaAdapter implements ItemPort { public class ItemJpaAdapter implements ItemPort {
private final ItemJpaRepository itemJpaRepository; private final ItemJpaRepository itemJpaRepository;
@@ -18,6 +20,7 @@ public class ItemJpaAdapter implements ItemPort {
@Override @Override
public List<Item> getAll() { public List<Item> getAll() {
sleepOneSec();
return itemJpaRepository.findAll() return itemJpaRepository.findAll()
.stream() .stream()
.map(ItemJpaEntity::toDomain) .map(ItemJpaEntity::toDomain)

View File

@@ -0,0 +1,15 @@
package com.zeenea.experiments.virtualthreads.virtualthreadsapp.utils;
public class LatencyUtils {
public static void sleep(int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
public static void sleepOneSec() {
sleep(1000);
}
}