|
4 | 4 | import de.wuespace.telestion.api.verticle.GenericConfiguration; |
5 | 5 | import de.wuespace.telestion.api.verticle.TelestionConfiguration; |
6 | 6 | import de.wuespace.telestion.api.verticle.TelestionVerticle; |
| 7 | +import de.wuespace.telestion.api.verticle.trait.WithTiming; |
7 | 8 | import io.vertx.core.DeploymentOptions; |
| 9 | +import io.vertx.core.Handler; |
8 | 10 | import io.vertx.core.Promise; |
9 | 11 | import io.vertx.core.Vertx; |
10 | 12 |
|
|
15 | 17 | * |
16 | 18 | * @author Pablo Klaschka, Jan von Pichowski, Ludwig Richter |
17 | 19 | */ |
18 | | -public final class SayHello extends TelestionVerticle<GenericConfiguration> { |
| 20 | +public final class SayHello extends TelestionVerticle<SayHello.Configuration> implements WithTiming { |
19 | 21 | public static void main(String[] args) { |
20 | 22 | var vertx = Vertx.vertx(); |
21 | | - vertx.deployVerticle(SayHello.class, |
22 | | - new DeploymentOptions().setConfig(new Configuration(1, "hello world").json())); |
| 23 | + var configuration = new Configuration(1, 10, "hello world"); |
| 24 | + vertx.deployVerticle(SayHello.class, new DeploymentOptions().setConfig(configuration.json())); |
| 25 | + } |
| 26 | + |
| 27 | + public record Configuration( |
| 28 | + @JsonProperty long period, |
| 29 | + @JsonProperty long duration, |
| 30 | + @JsonProperty String message |
| 31 | + ) implements TelestionConfiguration { |
23 | 32 | } |
24 | 33 |
|
25 | 34 | @Override |
26 | 35 | public void onStart(Promise<Void> startPromise) { |
27 | | - vertx.setPeriodic(Duration.ofSeconds(getGenericConfig().getInteger("period")).toMillis(), |
28 | | - timerId -> logger.info( |
29 | | - "{} from {}", |
30 | | - getGenericConfig().getString("message"), |
31 | | - deploymentID() |
32 | | - ) |
33 | | - ); |
| 36 | + var delay = Duration.ofSeconds(getConfig().period()); |
| 37 | + var duration = Duration.ofSeconds(getConfig().duration()); |
| 38 | + // setup interval |
| 39 | + var timing = interval(delay, id -> logger.info( |
| 40 | + "{} from {}", |
| 41 | + getConfig().message(), |
| 42 | + deploymentID() |
| 43 | + )); |
| 44 | + |
| 45 | + // cancel after of some time |
| 46 | + timeout(duration, id -> timing.cancel()); |
| 47 | + |
34 | 48 | startPromise.complete(); |
35 | 49 | logger.info("Started {} with config {}", SayHello.class.getSimpleName(), getConfig()); |
36 | 50 | } |
37 | | - |
38 | | - public static record Configuration( |
39 | | - @JsonProperty long period, |
40 | | - @JsonProperty String message |
41 | | - ) implements TelestionConfiguration { |
42 | | - } |
43 | 51 | } |
0 commit comments