M
- type of the input messageOM
- type of the transformed messages@InterfaceStability.Unstable @FunctionalInterface public interface AsyncFlatMapFunction<M,OM> extends InitableFunction, ClosableFunction, java.io.Serializable
FlatMapFunction
used in tandem with MessageStream.flatMapAsync(AsyncFlatMapFunction)
to transform a collection of 0 or more messages.
Typically, AsyncFlatMapFunction
is used for describing complex transformations that involve IO operations or remote calls.
The following pseudo code demonstrates a sample implementation of AsyncFlatMapFunction
that sends out an email
and returns the status asynchronously.
AsyncFlatMapFunction<Email, Status> asyncEmailSender = (Email message) -> {
...
Request<Email> emailRequest = buildEmailRequest(message);
Future<EmailResponse> emailResponseFuture = emailClient.sendRequest(emailRequest); // send email asynchronously
...
return new CompletableFuture<>(emailResponseFuture)
.thenApply(response -> fetchStatus(response);
}
The function needs to be thread safe in case of task.max.concurrency>1. It also needs to coordinate any
shared state since happens-before is not guaranteed between the messages delivered to the function. Refer to
MessageStream.flatMapAsync(AsyncFlatMapFunction)
docs for more details on the modes
and guarantees.
For each invocation, the CompletionStage
returned by the function should be completed successfully/exceptionally
within task.callback.timeout.ms; failure to do so will result in SamzaException
bringing down the application.
Modifier and Type | Method and Description |
---|---|
java.util.concurrent.CompletionStage<java.util.Collection<OM>> |
apply(M message)
Transforms the provided message into a collection of 0 or more messages.
|
init
close