Formatting java codes with sbt-java-formatter.

This commit is contained in:
hepin1989 2019-01-12 04:00:53 +08:00
parent 27500001ea
commit 998c5a9285
401 changed files with 19750 additions and 17450 deletions

View file

@ -8,41 +8,43 @@ import akka.actor.*;
import akka.pattern.Backoff;
import akka.pattern.BackoffSupervisor;
import akka.testkit.TestActors.EchoActor;
//#backoff-imports
// #backoff-imports
import java.time.Duration;
//#backoff-imports
// #backoff-imports
public class BackoffSupervisorDocTest {
void exampleStop (ActorSystem system) {
//#backoff-stop
void exampleStop(ActorSystem system) {
// #backoff-stop
final Props childProps = Props.create(EchoActor.class);
final Props supervisorProps = BackoffSupervisor.props(
Backoff.onStop(
childProps,
"myEcho",
Duration.ofSeconds(3),
Duration.ofSeconds(30),
0.2)); // adds 20% "noise" to vary the intervals slightly
final Props supervisorProps =
BackoffSupervisor.props(
Backoff.onStop(
childProps,
"myEcho",
Duration.ofSeconds(3),
Duration.ofSeconds(30),
0.2)); // adds 20% "noise" to vary the intervals slightly
system.actorOf(supervisorProps, "echoSupervisor");
//#backoff-stop
// #backoff-stop
}
void exampleFailure (ActorSystem system) {
//#backoff-fail
void exampleFailure(ActorSystem system) {
// #backoff-fail
final Props childProps = Props.create(EchoActor.class);
final Props supervisorProps = BackoffSupervisor.props(
Backoff.onFailure(
childProps,
"myEcho",
Duration.ofSeconds(3),
Duration.ofSeconds(30),
0.2)); // adds 20% "noise" to vary the intervals slightly
final Props supervisorProps =
BackoffSupervisor.props(
Backoff.onFailure(
childProps,
"myEcho",
Duration.ofSeconds(3),
Duration.ofSeconds(30),
0.2)); // adds 20% "noise" to vary the intervals slightly
system.actorOf(supervisorProps, "echoSupervisor");
//#backoff-fail
// #backoff-fail
}
}

View file

@ -35,20 +35,20 @@ public class SupervisedAsk {
}
}
private static class AskTimeout {
}
private static class AskTimeout {}
public static class AskSupervisorCreator extends AbstractActor {
@Override
public Receive createReceive() {
return receiveBuilder()
.match(AskParam.class, message -> {
ActorRef supervisor = getContext().actorOf(
Props.create(AskSupervisor.class));
supervisor.forward(message, getContext());
})
.build();
.match(
AskParam.class,
message -> {
ActorRef supervisor = getContext().actorOf(Props.create(AskSupervisor.class));
supervisor.forward(message, getContext());
})
.build();
}
}
@ -60,49 +60,63 @@ public class SupervisedAsk {
@Override
public SupervisorStrategy supervisorStrategy() {
return new OneForOneStrategy(0, Duration.ZERO, cause -> {
caller.tell(new Status.Failure(cause), getSelf());
return SupervisorStrategy.stop();
});
return new OneForOneStrategy(
0,
Duration.ZERO,
cause -> {
caller.tell(new Status.Failure(cause), getSelf());
return SupervisorStrategy.stop();
});
}
@Override
public Receive createReceive() {
return receiveBuilder()
.match(AskParam.class, message -> {
askParam = message;
caller = getSender();
targetActor = getContext().actorOf(askParam.props);
getContext().watch(targetActor);
targetActor.forward(askParam.message, getContext());
Scheduler scheduler = getContext().getSystem().scheduler();
timeoutMessage = scheduler.scheduleOnce(askParam.timeout,
getSelf(), new AskTimeout(), getContext().getDispatcher(), null);
})
.match(Terminated.class, message -> {
Throwable ex = new ActorKilledException("Target actor terminated.");
caller.tell(new Status.Failure(ex), getSelf());
timeoutMessage.cancel();
getContext().stop(getSelf());
})
.match(AskTimeout.class, message -> {
Throwable ex = new TimeoutException("Target actor timed out after "
+ askParam.timeout.toString());
caller.tell(new Status.Failure(ex), getSelf());
getContext().stop(getSelf());
})
.build();
.match(
AskParam.class,
message -> {
askParam = message;
caller = getSender();
targetActor = getContext().actorOf(askParam.props);
getContext().watch(targetActor);
targetActor.forward(askParam.message, getContext());
Scheduler scheduler = getContext().getSystem().scheduler();
timeoutMessage =
scheduler.scheduleOnce(
askParam.timeout,
getSelf(),
new AskTimeout(),
getContext().getDispatcher(),
null);
})
.match(
Terminated.class,
message -> {
Throwable ex = new ActorKilledException("Target actor terminated.");
caller.tell(new Status.Failure(ex), getSelf());
timeoutMessage.cancel();
getContext().stop(getSelf());
})
.match(
AskTimeout.class,
message -> {
Throwable ex =
new TimeoutException(
"Target actor timed out after " + askParam.timeout.toString());
caller.tell(new Status.Failure(ex), getSelf());
getContext().stop(getSelf());
})
.build();
}
}
public static CompletionStage<Object> askOf(ActorRef supervisorCreator, Props props,
Object message, Duration timeout) {
public static CompletionStage<Object> askOf(
ActorRef supervisorCreator, Props props, Object message, Duration timeout) {
AskParam param = new AskParam(props, message, timeout);
return Patterns.ask(supervisorCreator, param, timeout);
}
synchronized public static ActorRef createSupervisorCreator(
ActorRefFactory factory) {
public static synchronized ActorRef createSupervisorCreator(ActorRefFactory factory) {
return factory.actorOf(Props.create(AskSupervisorCreator.class));
}
}

View file

@ -17,15 +17,17 @@ import java.util.concurrent.TimeUnit;
public class SupervisedAskSpec {
public Object execute(Class<? extends AbstractActor> someActor,
Object message, Duration timeout, ActorRefFactory actorSystem)
public Object execute(
Class<? extends AbstractActor> someActor,
Object message,
Duration timeout,
ActorRefFactory actorSystem)
throws Exception {
// example usage
try {
ActorRef supervisorCreator = SupervisedAsk
.createSupervisorCreator(actorSystem);
CompletionStage<Object> finished = SupervisedAsk.askOf(supervisorCreator,
Props.create(someActor), message, timeout);
ActorRef supervisorCreator = SupervisedAsk.createSupervisorCreator(actorSystem);
CompletionStage<Object> finished =
SupervisedAsk.askOf(supervisorCreator, Props.create(someActor), message, timeout);
return finished.toCompletableFuture().get(timeout.toMillis(), TimeUnit.MILLISECONDS);
} catch (Exception e) {
// exception propagated by supervision