Convert remaining UntypedActor in docs #22182

This commit is contained in:
ortigali 2017-02-04 11:51:30 +05:00
parent 432b53c509
commit 760de5c6d4
39 changed files with 701 additions and 717 deletions

View file

@ -25,7 +25,7 @@ public class SchedulerPatternTest extends AbstractJavaTest {
static
//#schedule-constructor
public class ScheduleInConstructor extends UntypedActor {
public class ScheduleInConstructor extends AbstractActor {
private final Cancellable tick = getContext().system().scheduler().schedule(
Duration.create(500, TimeUnit.MILLISECONDS),
@ -45,28 +45,25 @@ public class SchedulerPatternTest extends AbstractJavaTest {
}
@Override
public void onReceive(Object message) throws Exception {
if (message.equals("tick")) {
// do something useful here
//#schedule-constructor
target.tell(message, self());
//#schedule-constructor
}
//#schedule-constructor
else if (message.equals("restart")) {
throw new ArithmeticException();
}
//#schedule-constructor
else {
unhandled(message);
}
public Receive createReceive() {
return receiveBuilder()
.matchEquals("tick", message -> {
// do something useful here
//#schedule-constructor
target.tell(message, self());
//#schedule-constructor
})
.matchEquals("restart", message -> {
throw new ArithmeticException();
})
.build();
}
}
//#schedule-constructor
static
//#schedule-receive
public class ScheduleInReceive extends UntypedActor {
public class ScheduleInReceive extends AbstractActor {
//#schedule-receive
// this variable and constructor is declared here to not show up in the docs
final ActorRef target;
@ -88,25 +85,22 @@ public class SchedulerPatternTest extends AbstractJavaTest {
}
@Override
public void onReceive(Object message) throws Exception {
if (message.equals("tick")) {
// send another periodic tick after the specified delay
getContext().system().scheduler().scheduleOnce(
Duration.create(1, TimeUnit.SECONDS),
self(), "tick", getContext().dispatcher(), null);
// do something useful here
//#schedule-receive
target.tell(message, self());
//#schedule-receive
}
//#schedule-receive
else if (message.equals("restart")) {
throw new ArithmeticException();
}
//#schedule-receive
else {
unhandled(message);
}
public Receive createReceive() {
return receiveBuilder()
.matchEquals("tick", message -> {
// send another periodic tick after the specified delay
getContext().system().scheduler().scheduleOnce(
Duration.create(1, TimeUnit.SECONDS),
self(), "tick", getContext().dispatcher(), null);
// do something useful here
//#schedule-receive
target.tell(message, self());
//#schedule-receive
})
.matchEquals("restart", message -> {
throw new ArithmeticException();
})
.build();
}
}
//#schedule-receive

View file

@ -17,7 +17,7 @@ import akka.actor.Status;
import akka.actor.SupervisorStrategy;
import akka.actor.SupervisorStrategy.Directive;
import akka.actor.Terminated;
import akka.actor.UntypedActor;
import akka.actor.AbstractActor;
import akka.japi.Function;
import akka.pattern.Patterns;
import akka.util.Timeout;
@ -39,21 +39,21 @@ public class SupervisedAsk {
private static class AskTimeout {
}
public static class AskSupervisorCreator extends UntypedActor {
public static class AskSupervisorCreator extends AbstractActor {
@Override
public void onReceive(Object message) throws Exception {
if (message instanceof AskParam) {
ActorRef supervisor = getContext().actorOf(
public Receive createReceive() {
return receiveBuilder()
.match(AskParam.class, message -> {
ActorRef supervisor = getContext().actorOf(
Props.create(AskSupervisor.class));
supervisor.forward(message, getContext());
} else {
unhandled(message);
}
supervisor.forward(message, getContext());
})
.build();
}
}
public static class AskSupervisor extends UntypedActor {
public static class AskSupervisor extends AbstractActor {
private ActorRef targetActor;
private ActorRef caller;
private AskParam askParam;
@ -71,28 +71,31 @@ public class SupervisedAsk {
}
@Override
public void onReceive(Object message) throws Exception {
if (message instanceof AskParam) {
askParam = (AskParam) message;
caller = sender();
targetActor = getContext().actorOf(askParam.props);
getContext().watch(targetActor);
targetActor.forward(askParam.message, getContext());
Scheduler scheduler = getContext().system().scheduler();
timeoutMessage = scheduler.scheduleOnce(askParam.timeout.duration(),
public Receive createReceive() {
return receiveBuilder()
.match(AskParam.class, message -> {
askParam = message;
caller = sender();
targetActor = getContext().actorOf(askParam.props);
getContext().watch(targetActor);
targetActor.forward(askParam.message, getContext());
Scheduler scheduler = getContext().system().scheduler();
timeoutMessage = scheduler.scheduleOnce(askParam.timeout.duration(),
self(), new AskTimeout(), getContext().dispatcher(), null);
} else if (message instanceof Terminated) {
Throwable ex = new ActorKilledException("Target actor terminated.");
caller.tell(new Status.Failure(ex), self());
timeoutMessage.cancel();
getContext().stop(self());
} else if (message instanceof AskTimeout) {
Throwable ex = new TimeoutException("Target actor timed out after "
})
.match(Terminated.class, message -> {
Throwable ex = new ActorKilledException("Target actor terminated.");
caller.tell(new Status.Failure(ex), self());
timeoutMessage.cancel();
getContext().stop(self());
})
.match(AskTimeout.class, message -> {
Throwable ex = new TimeoutException("Target actor timed out after "
+ askParam.timeout.toString());
caller.tell(new Status.Failure(ex), self());
getContext().stop(self());
} else
unhandled(message);
caller.tell(new Status.Failure(ex), self());
getContext().stop(self());
})
.build();
}
}

View file

@ -5,12 +5,12 @@ import scala.concurrent.Future;
import akka.actor.ActorRef;
import akka.actor.ActorRefFactory;
import akka.actor.Props;
import akka.actor.UntypedActor;
import akka.actor.AbstractActor;
import akka.util.Timeout;
public class SupervisedAskSpec {
public Object execute(Class<? extends UntypedActor> someActor,
public Object execute(Class<? extends AbstractActor> someActor,
Object message, Timeout timeout, ActorRefFactory actorSystem)
throws Exception {
// example usage