Adding example on how to roll your own ExecutionContext

This commit is contained in:
Viktor Klang 2012-02-20 15:43:17 +01:00
parent 46fb8c72cd
commit 0f685bdaae
4 changed files with 55 additions and 15 deletions

View file

@ -5,8 +5,6 @@ package akka.docs.future;
//#imports1
import akka.dispatch.*;
import akka.japi.Procedure;
import akka.japi.Procedure2;
import akka.util.Timeout;
//#imports1
@ -41,9 +39,17 @@ import static akka.dispatch.Futures.reduce;
//#imports6
//#imports7
import akka.dispatch.ExecutionContexts;
import akka.dispatch.ExecutionContextExecutorService;
//#imports7
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.junit.After;
import org.junit.Before;
@ -73,6 +79,20 @@ public class FutureDocTestBase {
system.shutdown();
}
@Test public void useCustomExecutionContext() throws Exception {
ExecutorService yourExecutorServiceGoesHere = Executors.newSingleThreadExecutor();
//#diy-execution-context
ExecutionContextExecutorService ec =
ExecutionContexts.fromExecutorService(yourExecutorServiceGoesHere);
//Use ec with your Futures
Future<String> f1 = Futures.successful("foo", ec);
// Then you shut the ec down somewhere at the end of your program/application.
ec.shutdown();
//#diy-execution-context
}
@Test
public void useBlockingFromActor() throws Exception {
ActorRef actor = system.actorOf(new Props(MyActor.class));
@ -324,10 +344,10 @@ public class FutureDocTestBase {
public void useAndThen() {
//#and-then
Future<String> future1 = Futures.successful("value", system.dispatcher()).andThen(new OnComplete<String>() {
public void onComplete(Throwable failure, String result) {
if (failure != null)
sendToIssueTracker(failure);
}
public void onComplete(Throwable failure, String result) {
if (failure != null)
sendToIssueTracker(failure);
}
}).andThen(new OnComplete<String>() {
public void onComplete(Throwable failure, String result) {
if (result != null)
@ -416,13 +436,13 @@ public class FutureDocTestBase {
Future<String> future = Futures.successful("foo", system.dispatcher());
//#onComplete
future.onComplete(new OnComplete<String>() {
public void onComplete(Throwable failure, String result) {
if (failure != null) {
//We got a failure, handle it here
} else {
// We got a result, do something with it
public void onComplete(Throwable failure, String result) {
if (failure != null) {
//We got a failure, handle it here
} else {
// We got a result, do something with it
}
}
}
});
//#onComplete
}