Last active
August 30, 2017 12:44
-
-
Save beritou/c0f91e356f7f75629076d6521c2884ac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.lightbend.akka.sample; | |
import akka.actor.AbstractActor; | |
import akka.actor.ActorSystem; | |
import akka.dispatch.OnSuccess; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.concurrent.Callable; | |
import java.util.concurrent.CompletionStage; | |
import java.util.concurrent.Executor; | |
import scala.compat.java8.FutureConverters; | |
import scala.concurrent.Future; | |
import static akka.dispatch.Futures.future; | |
public class CompletionStageExample extends AbstractActor { | |
final ActorSystem system = ActorSystem.create("helloakka"); | |
@Override | |
public Receive createReceive() { | |
Future<List<String>> scalaFuture = future(new Callable<List<String>>() { | |
public List<String> call() { | |
return Arrays.asList("Hello", "World!"); | |
} | |
}, system.dispatcher()); | |
final Executor ex = system.dispatcher(); | |
CompletionStage<List<String>> fromScalaFuture = FutureConverters.toJava(scalaFuture) | |
.thenApplyAsync(s -> { | |
s.contains("Hello"); | |
return s; | |
}, ex) | |
.thenApplyAsync(s -> { | |
s.contains("World"); | |
return s; | |
}, ex) | |
.thenApplyAsync(s -> { | |
new PrintResult<List<String>>(); | |
return s; | |
}, ex); | |
List<String> helloWorldStrings = fromScalaFuture.toCompletableFuture().join(); | |
return AbstractActor.emptyBehavior(); | |
} | |
public final static class PrintResult<T> extends OnSuccess<T> { | |
@Override public final void onSuccess(T t) { | |
System.out.println(t); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment