Skip to content
This repository was archived by the owner on Oct 26, 2025. It is now read-only.

Commit 2d28b89

Browse files
fussel178Ludwig Richter
authored andcommitted
feat(examples): Add sample which shows the usage of the WithTiming trait
1 parent c98c6c1 commit 2d28b89

File tree

1 file changed

+24
-16
lines changed
  • modules/telestion-examples/src/main/java/de/wuespace/telestion/examples

1 file changed

+24
-16
lines changed

modules/telestion-examples/src/main/java/de/wuespace/telestion/examples/SayHello.java

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import de.wuespace.telestion.api.verticle.GenericConfiguration;
55
import de.wuespace.telestion.api.verticle.TelestionConfiguration;
66
import de.wuespace.telestion.api.verticle.TelestionVerticle;
7+
import de.wuespace.telestion.api.verticle.trait.WithTiming;
78
import io.vertx.core.DeploymentOptions;
9+
import io.vertx.core.Handler;
810
import io.vertx.core.Promise;
911
import io.vertx.core.Vertx;
1012

@@ -15,29 +17,35 @@
1517
*
1618
* @author Pablo Klaschka, Jan von Pichowski, Ludwig Richter
1719
*/
18-
public final class SayHello extends TelestionVerticle<GenericConfiguration> {
20+
public final class SayHello extends TelestionVerticle<SayHello.Configuration> implements WithTiming {
1921
public static void main(String[] args) {
2022
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 {
2332
}
2433

2534
@Override
2635
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+
3448
startPromise.complete();
3549
logger.info("Started {} with config {}", SayHello.class.getSimpleName(), getConfig());
3650
}
37-
38-
public static record Configuration(
39-
@JsonProperty long period,
40-
@JsonProperty String message
41-
) implements TelestionConfiguration {
42-
}
4351
}

0 commit comments

Comments
 (0)