Handle akka with scala concurrent - Java Message

Java examples for Message:AKKA

Description

Handle akka with scala concurrent

Demo Code


import java.util.stream.IntStream;

import akka.actor.ActorSystem;
import akka.japi.function.Function2;
import akka.stream.ActorMaterializer;
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 Ex3 {
  public static void main(String[] args) throws Exception {
    ActorSystem system = ActorSystem.create("demo1");
    ActorMaterializer materializer = ActorMaterializer.create(system);

    // Create a source with some integer and print them
    IntStream stream = IntStream.range(1, 11);
    Source<Integer, ?> source = Source.from(() -> stream.iterator());
    Integer zero = 0;/*  w ww  . j ava2 s . c o m*/
    Function2<Integer, Integer, Integer> f = (x, y) -> x + y;
    Sink<Integer, Future<Integer>> foldingSink = Sink.fold(zero, f);
    Future<Integer> foldingResult = source.runWith(foldingSink, materializer);

    System.out.println(Await.result(foldingResult, Duration.Inf()));
  }
}

Related Tutorials