improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
This commit is contained in:
parent
3617fe8b41
commit
4bd6b7aab1
157 changed files with 3290 additions and 8882 deletions
|
|
@ -67,6 +67,7 @@ public class ActorPublisherDocTest extends AbstractJavaTest {
|
|||
}
|
||||
public static final JobDeniedMessage JobDenied = new JobDeniedMessage();
|
||||
}
|
||||
|
||||
public static class JobManager extends AbstractActorPublisher<JobManagerProtocol.Job> {
|
||||
|
||||
public static Props props() { return Props.create(JobManager.class); }
|
||||
|
|
@ -74,12 +75,13 @@ public class ActorPublisherDocTest extends AbstractJavaTest {
|
|||
private final int MAX_BUFFER_SIZE = 100;
|
||||
private final List<JobManagerProtocol.Job> buf = new ArrayList<>();
|
||||
|
||||
public JobManager() {
|
||||
receive(ReceiveBuilder.
|
||||
match(JobManagerProtocol.Job.class, job -> buf.size() == MAX_BUFFER_SIZE, job -> {
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.match(JobManagerProtocol.Job.class, job -> buf.size() == MAX_BUFFER_SIZE, job -> {
|
||||
sender().tell(JobManagerProtocol.JobDenied, self());
|
||||
}).
|
||||
match(JobManagerProtocol.Job.class, job -> {
|
||||
})
|
||||
.match(JobManagerProtocol.Job.class, job -> {
|
||||
sender().tell(JobManagerProtocol.JobAccepted, self());
|
||||
|
||||
if (buf.isEmpty() && totalDemand() > 0)
|
||||
|
|
@ -88,10 +90,10 @@ public class ActorPublisherDocTest extends AbstractJavaTest {
|
|||
buf.add(job);
|
||||
deliverBuf();
|
||||
}
|
||||
}).
|
||||
match(ActorPublisherMessage.Request.class, request -> deliverBuf()).
|
||||
match(ActorPublisherMessage.Cancel.class, cancel -> context().stop(self())).
|
||||
build());
|
||||
})
|
||||
.match(ActorPublisherMessage.Request.class, request -> deliverBuf())
|
||||
.match(ActorPublisherMessage.Cancel.class, cancel -> getContext().stop(self()))
|
||||
.build();
|
||||
}
|
||||
|
||||
void deliverBuf() {
|
||||
|
|
|
|||
|
|
@ -161,44 +161,49 @@ public class ActorSubscriberDocTest extends AbstractJavaTest {
|
|||
public WorkerPool() {
|
||||
final List<Routee> routees = new ArrayList<>();
|
||||
for (int i = 0; i < 3; i++)
|
||||
routees.add(new ActorRefRoutee(context().actorOf(Props.create(Worker.class))));
|
||||
routees.add(new ActorRefRoutee(getContext().actorOf(Props.create(Worker.class))));
|
||||
router = new Router(new RoundRobinRoutingLogic(), routees);
|
||||
|
||||
receive(ReceiveBuilder.
|
||||
match(ActorSubscriberMessage.OnNext.class, on -> on.element() instanceof WorkerPoolProtocol.Msg,
|
||||
onNext -> {
|
||||
WorkerPoolProtocol.Msg msg = (WorkerPoolProtocol.Msg) onNext.element();
|
||||
queue.put(msg.id, msg.replyTo);
|
||||
|
||||
if (queue.size() > MAX_QUEUE_SIZE)
|
||||
throw new RuntimeException("queued too many: " + queue.size());
|
||||
|
||||
router.route(WorkerPoolProtocol.work(msg.id), self());
|
||||
}).
|
||||
match(ActorSubscriberMessage.onCompleteInstance().getClass(), complete -> {
|
||||
if (queue.isEmpty()) {
|
||||
context().stop(self());
|
||||
}
|
||||
}).
|
||||
match(WorkerPoolProtocol.Reply.class, reply -> {
|
||||
int id = reply.id;
|
||||
queue.get(id).tell(WorkerPoolProtocol.done(id), self());
|
||||
queue.remove(id);
|
||||
if (canceled() && queue.isEmpty()) {
|
||||
context().stop(self());
|
||||
}
|
||||
}).
|
||||
build());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.match(ActorSubscriberMessage.OnNext.class, on -> on.element() instanceof WorkerPoolProtocol.Msg,
|
||||
onNext -> {
|
||||
WorkerPoolProtocol.Msg msg = (WorkerPoolProtocol.Msg) onNext.element();
|
||||
queue.put(msg.id, msg.replyTo);
|
||||
|
||||
if (queue.size() > MAX_QUEUE_SIZE)
|
||||
throw new RuntimeException("queued too many: " + queue.size());
|
||||
|
||||
router.route(WorkerPoolProtocol.work(msg.id), self());
|
||||
})
|
||||
.match(ActorSubscriberMessage.onCompleteInstance().getClass(), complete -> {
|
||||
if (queue.isEmpty()) {
|
||||
getContext().stop(self());
|
||||
}
|
||||
})
|
||||
.match(WorkerPoolProtocol.Reply.class, reply -> {
|
||||
int id = reply.id;
|
||||
queue.get(id).tell(WorkerPoolProtocol.done(id), self());
|
||||
queue.remove(id);
|
||||
if (canceled() && queue.isEmpty()) {
|
||||
getContext().stop(self());
|
||||
}
|
||||
})
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
static class Worker extends AbstractActor {
|
||||
public Worker() {
|
||||
receive(ReceiveBuilder.
|
||||
match(WorkerPoolProtocol.Work.class, work -> {
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.match(WorkerPoolProtocol.Work.class, work -> {
|
||||
// ...
|
||||
sender().tell(WorkerPoolProtocol.reply(work.id), self());
|
||||
}).build());
|
||||
})
|
||||
.build();
|
||||
}
|
||||
}
|
||||
//#worker-pool
|
||||
|
|
|
|||
|
|
@ -255,13 +255,18 @@ public class IntegrationDocTest extends AbstractJavaTest {
|
|||
static class DatabaseService extends AbstractActor {
|
||||
public final ActorRef probe;
|
||||
|
||||
DatabaseService(ActorRef probe) {
|
||||
public DatabaseService(ActorRef probe) {
|
||||
this.probe = probe;
|
||||
|
||||
receive(ReceiveBuilder.match(Save.class, s -> {
|
||||
probe.tell(s.tweet.author.handle, ActorRef.noSender());
|
||||
sender().tell(SaveDone.INSTANCE, self());
|
||||
}).build());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.match(Save.class, s -> {
|
||||
probe.tell(s.tweet.author.handle, ActorRef.noSender());
|
||||
sender().tell(SaveDone.INSTANCE, self());
|
||||
})
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -298,7 +303,7 @@ public class IntegrationDocTest extends AbstractJavaTest {
|
|||
// ... process message
|
||||
String reply = word.toUpperCase();
|
||||
// reply to the ask
|
||||
getSender().tell(reply, getSelf());
|
||||
sender().tell(reply, self());
|
||||
} else {
|
||||
unhandled(message);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -49,7 +49,7 @@ public class TwitterStreamQuickstartDocTest extends AbstractJavaTest {
|
|||
|
||||
@BeforeClass
|
||||
public static void setup() {
|
||||
system = ActorSystem.create("SampleActorTest");
|
||||
system = ActorSystem.create("TwitterStreamQuickstartDocTest");
|
||||
mat = ActorMaterializer.create(system);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -87,14 +87,17 @@ public class RecipeGlobalRateLimit extends RecipeTest {
|
|||
this.tokenRefreshPeriod,
|
||||
self(),
|
||||
REPLENISH_TOKENS,
|
||||
context().system().dispatcher(),
|
||||
getContext().system().dispatcher(),
|
||||
self());
|
||||
|
||||
receive(open());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return open();
|
||||
}
|
||||
|
||||
PartialFunction<Object, BoxedUnit> open() {
|
||||
return ReceiveBuilder
|
||||
private Receive open() {
|
||||
return receiveBuilder()
|
||||
.match(ReplenishTokens.class, rt -> {
|
||||
permitTokens = Math.min(permitTokens + tokenRefreshAmount, maxAvailableTokens);
|
||||
})
|
||||
|
|
@ -102,13 +105,14 @@ public class RecipeGlobalRateLimit extends RecipeTest {
|
|||
permitTokens -= 1;
|
||||
sender().tell(MAY_PASS, self());
|
||||
if (permitTokens == 0) {
|
||||
context().become(closed());
|
||||
getContext().become(closed());
|
||||
}
|
||||
}).build();
|
||||
})
|
||||
.build();
|
||||
}
|
||||
|
||||
PartialFunction<Object, BoxedUnit> closed() {
|
||||
return ReceiveBuilder
|
||||
private Receive closed() {
|
||||
return receiveBuilder()
|
||||
.match(ReplenishTokens.class, rt -> {
|
||||
permitTokens = Math.min(permitTokens + tokenRefreshAmount, maxAvailableTokens);
|
||||
releaseWaiting();
|
||||
|
|
@ -128,7 +132,7 @@ public class RecipeGlobalRateLimit extends RecipeTest {
|
|||
permitTokens -= toBeReleased.size();
|
||||
toBeReleased.stream().forEach(ref -> ref.tell(MAY_PASS, self()));
|
||||
if (permitTokens > 0) {
|
||||
context().become(open());
|
||||
getContext().become(open());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue