Add latency to simulate long database queries.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user