akka Error Handling - Java Message

Java examples for Message:AKKA

Description

akka Error Handling

Demo Code



import java.util.stream.IntStream;

import akka.actor.ActorSystem;
import akka.japi.function.Function;
import akka.stream.ActorAttributes;
import akka.stream.ActorMaterializer;
import akka.stream.Supervision;
import akka.stream.javadsl.Sink;
import akka.stream.javadsl.Source;
import scala.concurrent.Await;
import scala.concurrent.Future;
import scala.concurrent.duration.Duration;

public class Main {
  public static void main(String[] args) throws Exception {
    ActorSystem system = ActorSystem.create("demo5");
    ActorMaterializer materializer = ActorMaterializer.create(system);

    Function<Throwable, Supervision.Directive> decider = e -> {
      if (e instanceof ArithmeticException)
        return Supervision.resume();
      else//from   w w w.ja v a2s.c  o m
        return Supervision.stop();
    };

    IntStream stream = IntStream.range(0, 6);
    Source<Integer, ?> source = Source.from(() -> stream.iterator()).map(x -> 100 / x)
        .withAttributes(ActorAttributes.withSupervisionStrategy(decider));
    Future<Integer> result = source.runWith(Sink.fold(0, (x, y) -> x + y), materializer);
    Integer res = Await.result(result, Duration.Inf());

    System.out.println(res); // this will print 228
  }
}

Related Tutorials