in java/showcase/src/main/java/org/apache/flink/statefun/playground/java/showcase/part2/messaging/MessagingPrimitivesShowcaseFn.java [52:85]
public CompletableFuture<Void> apply(Context context, Message message) {
// You send messages to functions simply by specifying the target function's typename
// and the target instance id for that for function; StateFun handles the routing for you,
// without the need of any means for service discovery.
final Address targetAddress = new Address(TARGET_FN_TYPENAME, "target-instance-id");
// you can directly send primitive type values as messages, ...
context.send(MessageBuilder.forAddress(targetAddress).withValue(123).build());
context.send(MessageBuilder.forAddress(targetAddress).withValue(true).build());
context.send(MessageBuilder.forAddress(targetAddress).withValue("hello world!").build());
context.send(MessageBuilder.forAddress(targetAddress).withValue(3.14159e+11f).build());
context.send(MessageBuilder.forAddress(targetAddress).withValue(3.14159).build());
context.send(MessageBuilder.forAddress(targetAddress).withValue(-19911108123046639L).build());
// ... or, in general, a value of any custom defined type.
context.send(
MessageBuilder.forAddress(targetAddress)
.withCustomType(USER_PROFILE_PROTOBUF_TYPE, UserProfile.getDefaultInstance())
.build());
// You can send messages to any function, including yourself!
context.send(MessageBuilder.forAddress(context.self()).withValue("hello world!").build());
// Additionally, you may ask StateFun to send out a message after a specified delay.
// A common usage pattern is to send delayed messages to yourself to model timer triggers.
context.sendAfter(
Duration.ofMinutes(10),
MessageBuilder.forAddress(context.self()).withValue("hello world!").build());
// None of the above sends is a blocking operation.
// All side-effects (such as messaging other functions) are collected and happen after this
// method returns.
return context.done();
}