rename akka-docs dir to docs (#62)
This commit is contained in:
parent
13dce0ec69
commit
708da8caec
1029 changed files with 2033 additions and 2039 deletions
|
|
@ -1,60 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2018-2022 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
package jdocs.cluster;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.time.Duration;
|
||||
|
||||
import jdocs.cluster.StatsMessages.JobFailed;
|
||||
import jdocs.cluster.StatsMessages.StatsResult;
|
||||
import org.apache.pekko.actor.ActorRef;
|
||||
import org.apache.pekko.actor.ReceiveTimeout;
|
||||
import org.apache.pekko.actor.AbstractActor;
|
||||
|
||||
// #aggregator
|
||||
public class StatsAggregator extends AbstractActor {
|
||||
|
||||
final int expectedResults;
|
||||
final ActorRef replyTo;
|
||||
final List<Integer> results = new ArrayList<Integer>();
|
||||
|
||||
public StatsAggregator(int expectedResults, ActorRef replyTo) {
|
||||
this.expectedResults = expectedResults;
|
||||
this.replyTo = replyTo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void preStart() {
|
||||
getContext().setReceiveTimeout(Duration.ofSeconds(3));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.match(
|
||||
Integer.class,
|
||||
wordCount -> {
|
||||
results.add(wordCount);
|
||||
if (results.size() == expectedResults) {
|
||||
int sum = 0;
|
||||
for (int c : results) {
|
||||
sum += c;
|
||||
}
|
||||
double meanWordLength = ((double) sum) / results.size();
|
||||
replyTo.tell(new StatsResult(meanWordLength), getSelf());
|
||||
getContext().stop(getSelf());
|
||||
}
|
||||
})
|
||||
.match(
|
||||
ReceiveTimeout.class,
|
||||
x -> {
|
||||
replyTo.tell(new JobFailed("Service unavailable, try again later"), getSelf());
|
||||
getContext().stop(getSelf());
|
||||
})
|
||||
.build();
|
||||
}
|
||||
}
|
||||
// #aggregator
|
||||
Loading…
Add table
Add a link
Reference in a new issue