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

View File

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

View File

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

View File

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