Convert UntypedActor to AbstractActor #22308

This commit is contained in:
ortigali 2017-02-23 00:50:07 +05:00
parent 94afbee179
commit a175180b44
13 changed files with 217 additions and 264 deletions

View file

@ -9,14 +9,14 @@ import akka.routing.ConsistentHashingPool;
import docs.cluster.StatsMessages.StatsJob;
import akka.actor.ActorRef;
import akka.actor.Props;
import akka.actor.UntypedActor;
import akka.actor.AbstractActor;
import akka.routing.ConsistentHashingRouter.ConsistentHashableEnvelope;
import akka.routing.FromConfig;
import java.util.Collections;
//#service
public class StatsService extends UntypedActor {
public class StatsService extends AbstractActor {
// This router is used both with lookup and deploy of routees. If you
// have a router with only lookup of routees you can use Props.empty()
@ -26,35 +26,30 @@ public class StatsService extends UntypedActor {
"workerRouter");
@Override
public void onReceive(Object message) {
if (message instanceof StatsJob) {
StatsJob job = (StatsJob) message;
if (job.getText().equals("")) {
unhandled(message);
} else {
final String[] words = job.getText().split(" ");
final ActorRef replyTo = getSender();
public Receive createReceive() {
return receiveBuilder()
.match(StatsJob.class, job -> !job.getText().isEmpty(), job -> {
String[] words = job.getText().split(" ");
ActorRef replyTo = sender();
// create actor that collects replies from workers
ActorRef aggregator = getContext().actorOf(
Props.create(StatsAggregator.class, words.length, replyTo));
Props.create(StatsAggregator.class, words.length, replyTo));
// send each word to a worker
for (String word : words) {
workerRouter.tell(new ConsistentHashableEnvelope(word, word),
aggregator);
aggregator);
}
}
} else {
unhandled(message);
}
})
.build();
}
}
//#service
//not used, only for documentation
abstract class StatsService2 extends UntypedActor {
abstract class StatsService2 extends AbstractActor {
//#router-lookup-in-code
int totalInstances = 100;
Iterable<String> routeesPaths = Collections
@ -69,7 +64,7 @@ abstract class StatsService2 extends UntypedActor {
}
//not used, only for documentation
abstract class StatsService3 extends UntypedActor {
abstract class StatsService3 extends AbstractActor {
//#router-deploy-in-code
int totalInstances = 100;
int maxInstancesPerNode = 3;