Merge remote-tracking branch 'origin/master' into wip-1644-programmatic-deploy-∂π
This commit is contained in:
commit
45140b465e
306 changed files with 14713 additions and 11320 deletions
|
|
@ -40,9 +40,9 @@ public class FaultHandlingTestBase {
|
|||
|
||||
//#strategy
|
||||
private static SupervisorStrategy strategy = new OneForOneStrategy(10, Duration.parse("1 minute"),
|
||||
new Function<Throwable, Action>() {
|
||||
new Function<Throwable, Directive>() {
|
||||
@Override
|
||||
public Action apply(Throwable t) {
|
||||
public Directive apply(Throwable t) {
|
||||
if (t instanceof ArithmeticException) {
|
||||
return resume();
|
||||
} else if (t instanceof NullPointerException) {
|
||||
|
|
@ -78,9 +78,9 @@ public class FaultHandlingTestBase {
|
|||
|
||||
//#strategy2
|
||||
private static SupervisorStrategy strategy = new OneForOneStrategy(10, Duration.parse("1 minute"),
|
||||
new Function<Throwable, Action>() {
|
||||
new Function<Throwable, Directive>() {
|
||||
@Override
|
||||
public Action apply(Throwable t) {
|
||||
public Directive apply(Throwable t) {
|
||||
if (t instanceof ArithmeticException) {
|
||||
return resume();
|
||||
} else if (t instanceof NullPointerException) {
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ import akka.actor.Props;
|
|||
//#import-future
|
||||
import akka.dispatch.Future;
|
||||
import akka.dispatch.Futures;
|
||||
import akka.dispatch.Mapper;
|
||||
import akka.dispatch.Await;
|
||||
import akka.util.Duration;
|
||||
import akka.util.Timeout;
|
||||
|
|
@ -37,16 +38,16 @@ import akka.util.Duration;
|
|||
import akka.actor.ActorTimeoutException;
|
||||
//#import-gracefulStop
|
||||
|
||||
//#import-askPipeTo
|
||||
//#import-askPipe
|
||||
import static akka.pattern.Patterns.ask;
|
||||
import static akka.pattern.Patterns.pipeTo;
|
||||
import static akka.pattern.Patterns.pipe;
|
||||
import akka.dispatch.Future;
|
||||
import akka.dispatch.Futures;
|
||||
import akka.util.Duration;
|
||||
import akka.util.Timeout;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.ArrayList;
|
||||
//#import-askPipeTo
|
||||
//#import-askPipe
|
||||
|
||||
import akka.actor.Props;
|
||||
import akka.actor.UntypedActor;
|
||||
|
|
@ -223,12 +224,12 @@ public class UntypedActorDocTestBase {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void usePatternsAskPipeTo() {
|
||||
public void usePatternsAskPipe() {
|
||||
ActorSystem system = ActorSystem.create("MySystem");
|
||||
ActorRef actorA = system.actorOf(new Props(MyUntypedActor.class));
|
||||
ActorRef actorB = system.actorOf(new Props(MyUntypedActor.class));
|
||||
ActorRef actorC = system.actorOf(new Props(MyUntypedActor.class));
|
||||
//#ask-pipeTo
|
||||
//#ask-pipe
|
||||
final Timeout t = new Timeout(Duration.create(5, TimeUnit.SECONDS));
|
||||
|
||||
final ArrayList<Future<Object>> futures = new ArrayList<Future<Object>>();
|
||||
|
|
@ -236,8 +237,8 @@ public class UntypedActorDocTestBase {
|
|||
futures.add(ask(actorB, "reqeest", t)); // using timeout from above
|
||||
|
||||
final Future<Iterable<Object>> aggregate = Futures.sequence(futures, system.dispatcher());
|
||||
|
||||
final Future<Result> transformed = aggregate.map(new akka.japi.Function<Iterable<Object>, Result>() {
|
||||
|
||||
final Future<Result> transformed = aggregate.map(new Mapper<Iterable<Object>, Result>() {
|
||||
public Result apply(Iterable<Object> coll) {
|
||||
final Iterator<Object> it = coll.iterator();
|
||||
final String s = (String) it.next();
|
||||
|
|
@ -246,8 +247,8 @@ public class UntypedActorDocTestBase {
|
|||
}
|
||||
});
|
||||
|
||||
pipeTo(transformed, actorC);
|
||||
//#ask-pipeTo
|
||||
pipe(transformed).to(actorC);
|
||||
//#ask-pipe
|
||||
system.shutdown();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ import java.util.List;
|
|||
import java.util.Map;
|
||||
|
||||
import akka.actor.*;
|
||||
import akka.dispatch.Mapper;
|
||||
import akka.japi.Function;
|
||||
import akka.util.Duration;
|
||||
import akka.util.Timeout;
|
||||
|
|
@ -19,9 +20,11 @@ import akka.event.LoggingAdapter;
|
|||
import com.typesafe.config.Config;
|
||||
import com.typesafe.config.ConfigFactory;
|
||||
|
||||
import static akka.japi.Util.manifest;
|
||||
|
||||
import static akka.actor.SupervisorStrategy.*;
|
||||
import static akka.pattern.Patterns.ask;
|
||||
import static akka.pattern.Patterns.pipeTo;
|
||||
import static akka.pattern.Patterns.pipe;
|
||||
|
||||
import static akka.docs.actor.japi.FaultHandlingDocSample.WorkerApi.*;
|
||||
import static akka.docs.actor.japi.FaultHandlingDocSample.CounterServiceApi.*;
|
||||
|
|
@ -115,9 +118,9 @@ public class FaultHandlingDocSample {
|
|||
|
||||
// Stop the CounterService child if it throws ServiceUnavailable
|
||||
private static SupervisorStrategy strategy = new OneForOneStrategy(-1, Duration.Inf(),
|
||||
new Function<Throwable, Action>() {
|
||||
new Function<Throwable, Directive>() {
|
||||
@Override
|
||||
public Action apply(Throwable t) {
|
||||
public Directive apply(Throwable t) {
|
||||
if (t instanceof ServiceUnavailable) {
|
||||
return stop();
|
||||
} else {
|
||||
|
|
@ -142,11 +145,14 @@ public class FaultHandlingDocSample {
|
|||
counterService.tell(new Increment(1), getSelf());
|
||||
|
||||
// Send current progress to the initial sender
|
||||
pipeTo(ask(counterService, GetCurrentCount, askTimeout).map(new Function<CurrentCount, Progress>() {
|
||||
public Progress apply(CurrentCount c) {
|
||||
return new Progress(100.0 * c.count / totalCount);
|
||||
}
|
||||
}), progressListener);
|
||||
pipe(ask(counterService, GetCurrentCount, askTimeout)
|
||||
.mapTo(manifest(CurrentCount.class))
|
||||
.map(new Mapper<CurrentCount, Progress>() {
|
||||
public Progress apply(CurrentCount c) {
|
||||
return new Progress(100.0 * c.count / totalCount);
|
||||
}
|
||||
}))
|
||||
.to(progressListener);
|
||||
} else {
|
||||
unhandled(msg);
|
||||
}
|
||||
|
|
@ -224,9 +230,9 @@ public class FaultHandlingDocSample {
|
|||
// Restart the storage child when StorageException is thrown.
|
||||
// After 3 restarts within 5 seconds it will be stopped.
|
||||
private static SupervisorStrategy strategy = new OneForOneStrategy(3, Duration.parse("5 seconds"),
|
||||
new Function<Throwable, Action>() {
|
||||
new Function<Throwable, Directive>() {
|
||||
@Override
|
||||
public Action apply(Throwable t) {
|
||||
public Directive apply(Throwable t) {
|
||||
if (t instanceof StorageException) {
|
||||
return restart();
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@ public class AgentDocTest {
|
|||
|
||||
@Test
|
||||
public void createAndClose() {
|
||||
//#create
|
||||
//#create
|
||||
ActorSystem system = ActorSystem.create("app");
|
||||
|
||||
Agent<Integer> agent = new Agent<Integer>(5, system);
|
||||
|
|
|
|||
|
|
@ -4,12 +4,10 @@
|
|||
package akka.docs.future;
|
||||
|
||||
//#imports1
|
||||
import akka.dispatch.Promise;
|
||||
import akka.dispatch.*;
|
||||
import akka.japi.Procedure;
|
||||
import akka.japi.Procedure2;
|
||||
import akka.util.Timeout;
|
||||
import akka.dispatch.Await;
|
||||
import akka.dispatch.Future;
|
||||
|
||||
//#imports1
|
||||
|
||||
|
|
@ -57,7 +55,6 @@ import akka.actor.ActorSystem;
|
|||
import akka.actor.UntypedActor;
|
||||
import akka.actor.ActorRef;
|
||||
import akka.actor.Props;
|
||||
import akka.dispatch.Futures;
|
||||
import akka.pattern.Patterns;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
|
@ -110,7 +107,7 @@ public class FutureDocTestBase {
|
|||
}
|
||||
}, system.dispatcher());
|
||||
|
||||
Future<Integer> f2 = f1.map(new Function<String, Integer>() {
|
||||
Future<Integer> f2 = f1.map(new Mapper<String, Integer>() {
|
||||
public Integer apply(String s) {
|
||||
return s.length();
|
||||
}
|
||||
|
|
@ -131,7 +128,7 @@ public class FutureDocTestBase {
|
|||
}
|
||||
}, system.dispatcher());
|
||||
|
||||
Future<Integer> f2 = f1.map(new Function<String, Integer>() {
|
||||
Future<Integer> f2 = f1.map(new Mapper<String, Integer>() {
|
||||
public Integer apply(String s) {
|
||||
return s.length();
|
||||
}
|
||||
|
|
@ -153,7 +150,7 @@ public class FutureDocTestBase {
|
|||
|
||||
Thread.sleep(100);
|
||||
|
||||
Future<Integer> f2 = f1.map(new Function<String, Integer>() {
|
||||
Future<Integer> f2 = f1.map(new Mapper<String, Integer>() {
|
||||
public Integer apply(String s) {
|
||||
return s.length();
|
||||
}
|
||||
|
|
@ -173,7 +170,7 @@ public class FutureDocTestBase {
|
|||
}
|
||||
}, system.dispatcher());
|
||||
|
||||
Future<Integer> f2 = f1.flatMap(new Function<String, Future<Integer>>() {
|
||||
Future<Integer> f2 = f1.flatMap(new Mapper<String, Future<Integer>>() {
|
||||
public Future<Integer> apply(final String s) {
|
||||
return future(new Callable<Integer>() {
|
||||
public Integer call() {
|
||||
|
|
@ -204,7 +201,7 @@ public class FutureDocTestBase {
|
|||
|
||||
// Find the sum of the odd numbers
|
||||
Future<Long> futureSum = futureListOfInts.map(
|
||||
new Function<Iterable<Integer>, Long>() {
|
||||
new Mapper<Iterable<Integer>, Long>() {
|
||||
public Long apply(Iterable<Integer> ints) {
|
||||
long sum = 0;
|
||||
for (Integer i : ints)
|
||||
|
|
@ -306,24 +303,87 @@ public class FutureDocTestBase {
|
|||
//#filter
|
||||
Future<Integer> future1 = Futures.successful(4, system.dispatcher());
|
||||
Future<Integer> successfulFilter =
|
||||
future1.filter(new Function<Integer, Boolean>() {
|
||||
public Boolean apply(Integer i) { return i % 2 == 0; }
|
||||
future1.filter(new Filter<Integer>() {
|
||||
public boolean filter(Integer i) { return i % 2 == 0; }
|
||||
});
|
||||
|
||||
Future<Integer> failedFilter =
|
||||
future1.filter(new Function<Integer, Boolean>() {
|
||||
public Boolean apply(Integer i) { return i % 2 != 0; }
|
||||
future1.filter(new Filter<Integer>() {
|
||||
public boolean filter(Integer i) { return i % 2 != 0; }
|
||||
});
|
||||
//When filter fails, the returned Future will be failed with a scala.MatchError
|
||||
//#filter
|
||||
}
|
||||
|
||||
public void sendToTheInternetz(String s) {
|
||||
|
||||
}
|
||||
|
||||
public void sendToIssueTracker(Throwable t) {
|
||||
|
||||
}
|
||||
|
||||
@Test 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);
|
||||
}
|
||||
}).andThen(new OnComplete<String>() {
|
||||
public void onComplete(Throwable failure, String result) {
|
||||
if (result != null) sendToTheInternetz(result);
|
||||
}
|
||||
});
|
||||
//#and-then
|
||||
}
|
||||
|
||||
@Test public void useRecover() {
|
||||
//#recover
|
||||
Future<Integer> future = future(new Callable<Integer>() {
|
||||
public Integer call() {
|
||||
return 1 / 0;
|
||||
}
|
||||
}, system.dispatcher()).recover(new Recover<Integer>() {
|
||||
public Integer recover(Throwable problem) throws Throwable {
|
||||
if (problem instanceof ArithmeticException) return 0;
|
||||
else throw problem;
|
||||
}
|
||||
});
|
||||
int result = Await.result(future, Duration.create(1, SECONDS));
|
||||
assertEquals(result, 0);
|
||||
//#recover
|
||||
}
|
||||
|
||||
@Test public void useTryRecover() {
|
||||
//#try-recover
|
||||
Future<Integer> future = future(new Callable<Integer>() {
|
||||
public Integer call() {
|
||||
return 1 / 0;
|
||||
}
|
||||
}, system.dispatcher()).recoverWith(new Recover<Future<Integer>>() {
|
||||
public Future<Integer> recover(Throwable problem) throws Throwable {
|
||||
if (problem instanceof ArithmeticException) {
|
||||
return future(new Callable<Integer>() {
|
||||
public Integer call() {
|
||||
return 0;
|
||||
}
|
||||
}, system.dispatcher());
|
||||
}
|
||||
else throw problem;
|
||||
}
|
||||
});
|
||||
int result = Await.result(future, Duration.create(1, SECONDS));
|
||||
assertEquals(result, 0);
|
||||
//#try-recover
|
||||
}
|
||||
|
||||
@Test public void useOnSuccessOnFailureAndOnComplete() {
|
||||
{
|
||||
Future<String> future = Futures.successful("foo", system.dispatcher());
|
||||
//#onSuccess
|
||||
future.onSuccess(new Procedure<String>() {
|
||||
public void apply(String result) {
|
||||
future.onSuccess(new OnSuccess<String>() {
|
||||
public void onSuccess(String result) {
|
||||
if ("bar" == result) {
|
||||
//Do something if it resulted in "bar"
|
||||
} else {
|
||||
|
|
@ -337,8 +397,8 @@ public class FutureDocTestBase {
|
|||
Future<String> future =
|
||||
Futures.failed(new IllegalStateException("OHNOES"), system.dispatcher());
|
||||
//#onFailure
|
||||
future.onFailure( new Procedure<Throwable>() {
|
||||
public void apply(Throwable failure) {
|
||||
future.onFailure( new OnFailure() {
|
||||
public void onFailure(Throwable failure) {
|
||||
if (failure instanceof IllegalStateException) {
|
||||
//Do something if it was this particular failure
|
||||
} else {
|
||||
|
|
@ -351,8 +411,8 @@ public class FutureDocTestBase {
|
|||
{
|
||||
Future<String> future = Futures.successful("foo", system.dispatcher());
|
||||
//#onComplete
|
||||
future.onComplete(new Procedure2<Throwable, String>() {
|
||||
public void apply(Throwable failure, String result) {
|
||||
future.onComplete(new OnComplete<String>() {
|
||||
public void onComplete(Throwable failure, String result) {
|
||||
if (failure != null) {
|
||||
//We got a failure, handle it here
|
||||
} else {
|
||||
|
|
@ -370,7 +430,7 @@ public class FutureDocTestBase {
|
|||
Future<String> future1 = Futures.successful("foo", system.dispatcher());
|
||||
Future<String> future2 = Futures.successful("bar", system.dispatcher());
|
||||
Future<String> future3 =
|
||||
future1.zip(future2).map(new Function<scala.Tuple2<String,String>, String>() {
|
||||
future1.zip(future2).map(new Mapper<scala.Tuple2<String,String>, String>() {
|
||||
public String apply(scala.Tuple2<String,String> zipped) {
|
||||
return zipped._1() + " " + zipped._2();
|
||||
}
|
||||
|
|
@ -382,7 +442,7 @@ public class FutureDocTestBase {
|
|||
}
|
||||
|
||||
{
|
||||
//#or
|
||||
//#fallback-to
|
||||
Future<String> future1 =
|
||||
Futures.failed(new IllegalStateException("OHNOES1"), system.dispatcher());
|
||||
Future<String> future2 =
|
||||
|
|
@ -390,10 +450,10 @@ public class FutureDocTestBase {
|
|||
Future<String> future3 =
|
||||
Futures.successful("bar", system.dispatcher());
|
||||
Future<String> future4 =
|
||||
future1.or(future2).or(future3); // Will have "bar" in this case
|
||||
future1.fallbackTo(future2).fallbackTo(future3); // Will have "bar" in this case
|
||||
String result = Await.result(future4, Duration.create(1, SECONDS));
|
||||
assertEquals("bar", result);
|
||||
//#or
|
||||
//#fallback-to
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue