2023-01-08 17:13:31 +08:00
|
|
|
/*
|
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
|
* license agreements; and to You under the Apache License, version 2.0:
|
|
|
|
|
*
|
|
|
|
|
* https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* This file is part of the Apache Pekko project, derived from Akka.
|
|
|
|
|
*/
|
|
|
|
|
|
2018-10-29 17:19:37 +08:00
|
|
|
/*
|
2022-02-04 12:36:44 +01:00
|
|
|
* Copyright (C) 2009-2022 Lightbend Inc. <https://www.lightbend.com>
|
2014-02-21 12:43:30 +01:00
|
|
|
*/
|
|
|
|
|
|
2017-03-16 09:30:00 +01:00
|
|
|
package jdocs.actor;
|
2013-12-11 14:53:41 +01:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #sample-actor
|
2022-11-12 10:21:24 +01:00
|
|
|
import org.apache.pekko.actor.AbstractActor;
|
2013-12-11 14:53:41 +01:00
|
|
|
|
|
|
|
|
public class SampleActor extends AbstractActor {
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
private Receive guarded =
|
|
|
|
|
receiveBuilder()
|
|
|
|
|
.match(
|
|
|
|
|
String.class,
|
|
|
|
|
s -> s.contains("guard"),
|
|
|
|
|
s -> {
|
|
|
|
|
getSender().tell("contains(guard): " + s, getSelf());
|
|
|
|
|
getContext().unbecome();
|
|
|
|
|
})
|
|
|
|
|
.build();
|
2013-12-11 14:53:41 +01:00
|
|
|
|
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
2016-12-13 10:59:29 +01:00
|
|
|
@Override
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
return receiveBuilder()
|
2019-01-12 04:00:53 +08:00
|
|
|
.match(
|
|
|
|
|
Double.class,
|
|
|
|
|
d -> {
|
|
|
|
|
getSender().tell(d.isNaN() ? 0 : d, getSelf());
|
|
|
|
|
})
|
|
|
|
|
.match(
|
|
|
|
|
Integer.class,
|
|
|
|
|
i -> {
|
|
|
|
|
getSender().tell(i * 10, getSelf());
|
|
|
|
|
})
|
|
|
|
|
.match(
|
|
|
|
|
String.class,
|
|
|
|
|
s -> s.startsWith("guard"),
|
|
|
|
|
s -> {
|
|
|
|
|
getSender().tell("startsWith(guard): " + s.toUpperCase(), getSelf());
|
|
|
|
|
getContext().become(guarded, false);
|
|
|
|
|
})
|
|
|
|
|
.build();
|
2013-12-11 14:53:41 +01:00
|
|
|
}
|
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
// #sample-actor
|