From f6f0e8652e53118f465f7eb8004fd4314c8cc541 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20St=C3=A4ber?= Date: Mon, 12 Feb 2024 22:50:56 +0100 Subject: [PATCH] Add duration to the example Java service MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabian Stäber --- .../main/java/com/grafana/example/RollController.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/examples/java/src/main/java/com/grafana/example/RollController.java b/examples/java/src/main/java/com/grafana/example/RollController.java index f537a91..4dc3f20 100644 --- a/examples/java/src/main/java/com/grafana/example/RollController.java +++ b/examples/java/src/main/java/com/grafana/example/RollController.java @@ -1,5 +1,6 @@ package com.grafana.example; +import java.util.Random; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.GetMapping; @@ -13,13 +14,14 @@ public class RollController { private static final Logger logger = LoggerFactory.getLogger(RollController.class); + private final Random random = new Random(0); @GetMapping("/rolldice") - public String index(@RequestParam("player") Optional player) { - if (ThreadLocalRandom.current().nextInt(10) < 3) { + public String index(@RequestParam("player") Optional player) throws InterruptedException { + Thread.sleep((long) (Math.abs((random.nextGaussian() + 1.0) * 200.0))); + if (random.nextInt(10) < 3) { throw new RuntimeException("simulating an error"); } - int result = this.getRandomNumber(1, 6); if (player.isPresent()) { logger.info("{} is rolling the dice: {}", player.get(), result); @@ -30,6 +32,6 @@ public String index(@RequestParam("player") Optional player) { } public int getRandomNumber(int min, int max) { - return ThreadLocalRandom.current().nextInt(min, max + 1); + return random.nextInt(min, max + 1); } }