Search code examples
javaakkaakka-stream

Akka Stream callbacks


I'm new at Akka Streams and I have a question.

So I have some Client (source code from official documentation below) which can handle and process data from Server.

private static final ActorSystem system = ActorSystem.create("Client");
private static final Materializer materializer = ActorMaterializer.create(system);

final Flow<ByteString, ByteString, CompletionStage<Tcp.OutgoingConnection>> connection =
    Tcp.get(system).outgoingConnection("127.0.0.1", 8888);

final Flow<ByteString, ByteString, NotUsed> repl =
    Flow.of(ByteString.class)
        .map(ByteString::utf8String)
        .map(text -> {
            System.out.println("Server message ->: " + text);
            return ByteString.fromString(text);
        });

connection.join(repl).run(materializer);

How can I add callback methods (onComplete(), onError())?


Solution

  • Take a look at watchTermination combinator which will make the stream materialize a Future, which will be completed or failed depending if the stream completes or errors out.