Convert remaining UntypedActor in docs #22182
This commit is contained in:
parent
432b53c509
commit
760de5c6d4
39 changed files with 701 additions and 717 deletions
|
|
@ -2,7 +2,7 @@ package docs.io;
|
|||
|
||||
import akka.actor.ActorRef;
|
||||
import akka.actor.Props;
|
||||
import akka.actor.UntypedActor;
|
||||
import akka.actor.AbstractActor;
|
||||
import akka.io.Inet;
|
||||
import akka.io.Tcp;
|
||||
import akka.io.TcpMessage;
|
||||
|
|
@ -17,23 +17,26 @@ import java.util.List;
|
|||
*/
|
||||
public class JavaReadBackPressure {
|
||||
|
||||
static public class Listener extends UntypedActor {
|
||||
static public class Listener extends AbstractActor {
|
||||
ActorRef tcp;
|
||||
ActorRef listener;
|
||||
|
||||
@Override
|
||||
//#pull-accepting
|
||||
public void onReceive(Object message) throws Exception {
|
||||
if (message instanceof Tcp.Bound) {
|
||||
listener = sender();
|
||||
// Accept connections one by one
|
||||
listener.tell(TcpMessage.resumeAccepting(1), self());
|
||||
} else if (message instanceof Tcp.Connected) {
|
||||
ActorRef handler = getContext().actorOf(Props.create(PullEcho.class, sender()));
|
||||
sender().tell(TcpMessage.register(handler), self());
|
||||
// Resume accepting connections
|
||||
listener.tell(TcpMessage.resumeAccepting(1), self());
|
||||
}
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.match(Tcp.Bound.class, x -> {
|
||||
listener = sender();
|
||||
// Accept connections one by one
|
||||
listener.tell(TcpMessage.resumeAccepting(1), self());
|
||||
})
|
||||
.match(Tcp.Connected.class, x -> {
|
||||
ActorRef handler = getContext().actorOf(Props.create(PullEcho.class, sender()));
|
||||
sender().tell(TcpMessage.register(handler), self());
|
||||
// Resume accepting connections
|
||||
listener.tell(TcpMessage.resumeAccepting(1), self());
|
||||
})
|
||||
.build();
|
||||
}
|
||||
//#pull-accepting
|
||||
|
||||
|
|
@ -63,7 +66,7 @@ public class JavaReadBackPressure {
|
|||
static public class Ack implements Tcp.Event {
|
||||
}
|
||||
|
||||
static public class PullEcho extends UntypedActor {
|
||||
static public class PullEcho extends AbstractActor {
|
||||
final ActorRef connection;
|
||||
|
||||
public PullEcho(ActorRef connection) {
|
||||
|
|
@ -77,17 +80,18 @@ public class JavaReadBackPressure {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void onReceive(Object message) throws Exception {
|
||||
if (message instanceof Tcp.Received) {
|
||||
ByteString data = ((Tcp.Received) message).data();
|
||||
connection.tell(TcpMessage.write(data, new Ack()), self());
|
||||
} else if (message instanceof Ack) {
|
||||
connection.tell(TcpMessage.resumeReading(), self());
|
||||
}
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.match(Tcp.Received.class, message -> {
|
||||
ByteString data = message.data();
|
||||
connection.tell(TcpMessage.write(data, new Ack()), self());
|
||||
})
|
||||
.match(Ack.class, message -> {
|
||||
connection.tell(TcpMessage.resumeReading(), self());
|
||||
})
|
||||
.build();
|
||||
}
|
||||
//#pull-reading-echo
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ package docs.io;
|
|||
|
||||
//#imports
|
||||
import akka.actor.ActorRef;
|
||||
import akka.actor.UntypedActor;
|
||||
import akka.actor.AbstractActor;
|
||||
import akka.event.Logging;
|
||||
import akka.event.LoggingAdapter;
|
||||
import akka.io.Inet;
|
||||
|
|
@ -57,7 +57,7 @@ public class JavaUdpMulticast {
|
|||
}
|
||||
//#multicast-group
|
||||
|
||||
public static class Listener extends UntypedActor {
|
||||
public static class Listener extends AbstractActor {
|
||||
LoggingAdapter log = Logging.getLogger(getContext().system(), this);
|
||||
|
||||
ActorRef sink;
|
||||
|
|
@ -78,21 +78,22 @@ public class JavaUdpMulticast {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void onReceive(Object msg) {
|
||||
if (msg instanceof Udp.Bound) {
|
||||
final Udp.Bound b = (Udp.Bound) msg;
|
||||
log.info("Bound to {}", b.localAddress());
|
||||
sink.tell(b, self());
|
||||
} else if (msg instanceof Udp.Received) {
|
||||
final Udp.Received r = (Udp.Received) msg;
|
||||
final String txt = r.data().decodeString("utf-8");
|
||||
log.info("Received '{}' from {}", txt, r.sender());
|
||||
sink.tell(txt, self());
|
||||
} else unhandled(msg);
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.match(Udp.Bound.class, bound -> {
|
||||
log.info("Bound to {}", bound.localAddress());
|
||||
sink.tell(bound, self());
|
||||
})
|
||||
.match(Udp.Received.class, received -> {
|
||||
final String txt = received.data().decodeString("utf-8");
|
||||
log.info("Received '{}' from {}", txt, received.sender());
|
||||
sink.tell(txt, self());
|
||||
})
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
public static class Sender extends UntypedActor {
|
||||
public static class Sender extends AbstractActor {
|
||||
LoggingAdapter log = Logging.getLogger(getContext().system(), this);
|
||||
|
||||
String iface;
|
||||
|
|
@ -114,12 +115,14 @@ public class JavaUdpMulticast {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void onReceive(Object msg) {
|
||||
if (msg instanceof Udp.SimpleSenderReady) {
|
||||
InetSocketAddress remote = new InetSocketAddress(group + "%" + iface, port);
|
||||
log.info("Sending message to " + remote);
|
||||
sender().tell(UdpMessage.send(ByteString.fromString(message), remote), self());
|
||||
} else unhandled(msg);
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.match(Udp.SimpleSenderReady.class, x -> {
|
||||
InetSocketAddress remote = new InetSocketAddress(group + "%" + iface, port);
|
||||
log.info("Sending message to " + remote);
|
||||
sender().tell(UdpMessage.send(ByteString.fromString(message), remote), self());
|
||||
})
|
||||
.build();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,12 +4,11 @@
|
|||
|
||||
package docs.io;
|
||||
|
||||
import akka.testkit.AkkaJUnitActorSystemResource;
|
||||
import org.junit.ClassRule;
|
||||
import akka.japi.pf.ReceiveBuilder;
|
||||
import org.junit.Test;
|
||||
|
||||
import akka.actor.ActorSystem;
|
||||
import akka.actor.UntypedActor;
|
||||
import akka.actor.AbstractActor;
|
||||
//#imports
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.ArrayList;
|
||||
|
|
@ -24,14 +23,15 @@ import akka.util.ByteString;
|
|||
|
||||
public class UdpConnectedDocTest {
|
||||
|
||||
static public class Demo extends UntypedActor {
|
||||
static public class Demo extends AbstractActor {
|
||||
ActorRef connectionActor = null;
|
||||
ActorRef handler = self();
|
||||
ActorSystem system = getContext().system();
|
||||
|
||||
@Override
|
||||
public void onReceive(Object msg) {
|
||||
if ("connect".equals(msg)) {
|
||||
public Receive createReceive() {
|
||||
ReceiveBuilder builder = receiveBuilder();
|
||||
builder.matchEquals("connect", message -> {
|
||||
//#manager
|
||||
final ActorRef udp = UdpConnected.get(system).manager();
|
||||
//#manager
|
||||
|
|
@ -48,34 +48,33 @@ public class UdpConnectedDocTest {
|
|||
options.add(UdpSO.broadcast(true));
|
||||
udp.tell(UdpConnectedMessage.connect(handler, remoteAddr, localAddr, options), self());
|
||||
//#connect-with-options
|
||||
} else
|
||||
//#connected
|
||||
if (msg instanceof UdpConnected.Connected) {
|
||||
final UdpConnected.Connected conn = (UdpConnected.Connected) msg;
|
||||
connectionActor = sender(); // Save the worker ref for later use
|
||||
}
|
||||
//#connected
|
||||
else
|
||||
//#received
|
||||
if (msg instanceof UdpConnected.Received) {
|
||||
final UdpConnected.Received recv = (UdpConnected.Received) msg;
|
||||
final ByteString data = recv.data();
|
||||
// and do something with the received data ...
|
||||
} else if (msg instanceof UdpConnected.CommandFailed) {
|
||||
final UdpConnected.CommandFailed failed = (UdpConnected.CommandFailed) msg;
|
||||
final UdpConnected.Command command = failed.cmd();
|
||||
// react to failed connect, etc.
|
||||
} else if (msg instanceof UdpConnected.Disconnected) {
|
||||
// do something on disconnect
|
||||
}
|
||||
//#received
|
||||
else
|
||||
if ("send".equals(msg)) {
|
||||
ByteString data = ByteString.empty();
|
||||
//#send
|
||||
connectionActor.tell(UdpConnectedMessage.send(data), self());
|
||||
//#send
|
||||
}
|
||||
});
|
||||
//#connected
|
||||
builder.match(UdpConnected.Connected.class, conn -> {
|
||||
connectionActor = sender(); // Save the worker ref for later use
|
||||
});
|
||||
//#connected
|
||||
//#received
|
||||
builder
|
||||
.match(UdpConnected.Received.class, recv -> {
|
||||
final ByteString data = recv.data();
|
||||
// and do something with the received data ...
|
||||
})
|
||||
.match(UdpConnected.CommandFailed.class, failed -> {
|
||||
final UdpConnected.Command command = failed.cmd();
|
||||
// react to failed connect, etc.
|
||||
})
|
||||
.match(UdpConnected.Disconnected.class, x -> {
|
||||
// do something on disconnect
|
||||
});
|
||||
//#received
|
||||
builder.matchEquals("send", x -> {
|
||||
ByteString data = ByteString.empty();
|
||||
//#send
|
||||
connectionActor.tell(UdpConnectedMessage.send(data), self());
|
||||
//#send
|
||||
});
|
||||
return builder.build();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ package docs.io;
|
|||
//#imports
|
||||
import akka.actor.ActorRef;
|
||||
import akka.actor.PoisonPill;
|
||||
import akka.actor.UntypedActor;
|
||||
import akka.actor.AbstractActor;
|
||||
import akka.io.Udp;
|
||||
import akka.io.UdpConnected;
|
||||
import akka.io.UdpConnectedMessage;
|
||||
|
|
@ -21,7 +21,7 @@ import java.net.InetSocketAddress;
|
|||
public class UdpDocTest {
|
||||
|
||||
//#sender
|
||||
public static class SimpleSender extends UntypedActor {
|
||||
public static class SimpleSender extends AbstractActor {
|
||||
final InetSocketAddress remote;
|
||||
|
||||
public SimpleSender(InetSocketAddress remote) {
|
||||
|
|
@ -33,37 +33,34 @@ public class UdpDocTest {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void onReceive(Object msg) {
|
||||
if (msg instanceof Udp.SimpleSenderReady) {
|
||||
getContext().become(ready(sender()));
|
||||
//#sender
|
||||
sender().tell(UdpMessage.send(ByteString.fromString("hello"), remote), self());
|
||||
//#sender
|
||||
} else unhandled(msg);
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.match(Udp.SimpleSenderReady.class, message -> {
|
||||
getContext().become(ready(sender()));
|
||||
//#sender
|
||||
sender().tell(UdpMessage.send(ByteString.fromString("hello"), remote), self());
|
||||
//#sender
|
||||
})
|
||||
.build();
|
||||
}
|
||||
|
||||
private Procedure<Object> ready(final ActorRef send) {
|
||||
return new Procedure<Object>() {
|
||||
@Override
|
||||
public void apply(Object msg) throws Exception {
|
||||
if (msg instanceof String) {
|
||||
final String str = (String) msg;
|
||||
send.tell(UdpMessage.send(ByteString.fromString(str), remote), self());
|
||||
//#sender
|
||||
if (str.equals("world")) {
|
||||
send.tell(PoisonPill.getInstance(), self());
|
||||
}
|
||||
//#sender
|
||||
|
||||
} else unhandled(msg);
|
||||
}
|
||||
};
|
||||
private Receive ready(final ActorRef send) {
|
||||
return receiveBuilder()
|
||||
.match(String.class, message -> {
|
||||
send.tell(UdpMessage.send(ByteString.fromString(message), remote), self());
|
||||
//#sender
|
||||
if (message.equals("world")) {
|
||||
send.tell(PoisonPill.getInstance(), self());
|
||||
}
|
||||
//#sender
|
||||
})
|
||||
.build();
|
||||
}
|
||||
}
|
||||
//#sender
|
||||
|
||||
//#listener
|
||||
public static class Listener extends UntypedActor {
|
||||
public static class Listener extends AbstractActor {
|
||||
final ActorRef nextActor;
|
||||
|
||||
public Listener(ActorRef nextActor) {
|
||||
|
|
@ -77,46 +74,42 @@ public class UdpDocTest {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void onReceive(Object msg) {
|
||||
if (msg instanceof Udp.Bound) {
|
||||
final Udp.Bound b = (Udp.Bound) msg;
|
||||
//#listener
|
||||
nextActor.tell(b.localAddress(), sender());
|
||||
//#listener
|
||||
getContext().become(ready(sender()));
|
||||
} else unhandled(msg);
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.match(Udp.Bound.class, bound -> {
|
||||
//#listener
|
||||
nextActor.tell(bound.localAddress(), sender());
|
||||
//#listener
|
||||
getContext().become(ready(sender()));
|
||||
})
|
||||
.build();
|
||||
}
|
||||
|
||||
private Procedure<Object> ready(final ActorRef socket) {
|
||||
return new Procedure<Object>() {
|
||||
@Override
|
||||
public void apply(Object msg) throws Exception {
|
||||
if (msg instanceof Udp.Received) {
|
||||
final Udp.Received r = (Udp.Received) msg;
|
||||
// echo server example: send back the data
|
||||
socket.tell(UdpMessage.send(r.data(), r.sender()), self());
|
||||
// or do some processing and forward it on
|
||||
final Object processed = // parse data etc., e.g. using PipelineStage
|
||||
//#listener
|
||||
r.data().utf8String();
|
||||
//#listener
|
||||
nextActor.tell(processed, self());
|
||||
|
||||
} else if (msg.equals(UdpMessage.unbind())) {
|
||||
socket.tell(msg, self());
|
||||
|
||||
} else if (msg instanceof Udp.Unbound) {
|
||||
getContext().stop(self());
|
||||
|
||||
} else unhandled(msg);
|
||||
}
|
||||
};
|
||||
|
||||
private Receive ready(final ActorRef socket) {
|
||||
return receiveBuilder()
|
||||
.match(Udp.Received.class, r -> {
|
||||
// echo server example: send back the data
|
||||
socket.tell(UdpMessage.send(r.data(), r.sender()), self());
|
||||
// or do some processing and forward it on
|
||||
final Object processed = // parse data etc., e.g. using PipelineStage
|
||||
// #listener
|
||||
r.data().utf8String();
|
||||
//#listener
|
||||
nextActor.tell(processed, self());
|
||||
})
|
||||
.matchEquals(UdpMessage.unbind(), message -> {
|
||||
socket.tell(message, self());
|
||||
})
|
||||
.match(Udp.Unbound.class, message -> {
|
||||
getContext().stop(self());
|
||||
})
|
||||
.build();
|
||||
}
|
||||
}
|
||||
//#listener
|
||||
|
||||
//#connected
|
||||
public static class Connected extends UntypedActor {
|
||||
public static class Connected extends AbstractActor {
|
||||
final InetSocketAddress remote;
|
||||
|
||||
public Connected(InetSocketAddress remote) {
|
||||
|
|
@ -126,49 +119,45 @@ public class UdpDocTest {
|
|||
final ActorRef mgr = UdpConnected.get(getContext().system()).getManager();
|
||||
mgr.tell(UdpConnectedMessage.connect(self(), remote), self());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onReceive(Object msg) {
|
||||
if (msg instanceof UdpConnected.Connected) {
|
||||
getContext().become(ready(sender()));
|
||||
//#connected
|
||||
sender()
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.match(UdpConnected.Connected.class, message -> {
|
||||
getContext().become(ready(sender()));
|
||||
//#connected
|
||||
sender()
|
||||
.tell(UdpConnectedMessage.send(ByteString.fromString("hello")),
|
||||
self());
|
||||
//#connected
|
||||
} else unhandled(msg);
|
||||
self());
|
||||
//#connected
|
||||
})
|
||||
.build();
|
||||
}
|
||||
|
||||
private Procedure<Object> ready(final ActorRef connection) {
|
||||
return new Procedure<Object>() {
|
||||
@Override
|
||||
public void apply(Object msg) throws Exception {
|
||||
if (msg instanceof UdpConnected.Received) {
|
||||
final UdpConnected.Received r = (UdpConnected.Received) msg;
|
||||
// process data, send it on, etc.
|
||||
// #connected
|
||||
if (r.data().utf8String().equals("hello")) {
|
||||
connection.tell(
|
||||
UdpConnectedMessage.send(ByteString.fromString("world")),
|
||||
self());
|
||||
}
|
||||
// #connected
|
||||
|
||||
} else if (msg instanceof String) {
|
||||
final String str = (String) msg;
|
||||
connection
|
||||
.tell(UdpConnectedMessage.send(ByteString.fromString(str)),
|
||||
self());
|
||||
|
||||
} else if (msg.equals(UdpConnectedMessage.disconnect())) {
|
||||
connection.tell(msg, self());
|
||||
|
||||
} else if (msg instanceof UdpConnected.Disconnected) {
|
||||
getContext().stop(self());
|
||||
|
||||
} else unhandled(msg);
|
||||
}
|
||||
};
|
||||
|
||||
private Receive ready(final ActorRef connection) {
|
||||
return receiveBuilder()
|
||||
.match(UdpConnected.Received.class, r -> {
|
||||
// process data, send it on, etc.
|
||||
// #connected
|
||||
if (r.data().utf8String().equals("hello")) {
|
||||
connection.tell(
|
||||
UdpConnectedMessage.send(ByteString.fromString("world")),
|
||||
self());
|
||||
}
|
||||
// #connected
|
||||
})
|
||||
.match(String.class, str -> {
|
||||
connection
|
||||
.tell(UdpConnectedMessage.send(ByteString.fromString(str)),
|
||||
self());
|
||||
})
|
||||
.matchEquals(UdpConnectedMessage.disconnect(), message -> {
|
||||
connection.tell(message, self());
|
||||
})
|
||||
.match(UdpConnected.Disconnected.class, x -> {
|
||||
getContext().stop(self());
|
||||
})
|
||||
.build();
|
||||
}
|
||||
}
|
||||
//#connected
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ import akka.testkit.JavaTestKit;
|
|||
import akka.actor.ActorSystem;
|
||||
|
||||
//#imports1
|
||||
import akka.actor.UntypedActor;
|
||||
import akka.actor.AbstractActor;
|
||||
import akka.routing.ConsistentHashingRouter.ConsistentHashable;
|
||||
|
||||
import java.util.Map;
|
||||
|
|
@ -41,24 +41,24 @@ public class ConsistentHashingRouterDocTest extends AbstractJavaTest {
|
|||
|
||||
static
|
||||
//#cache-actor
|
||||
public class Cache extends UntypedActor {
|
||||
public class Cache extends AbstractActor {
|
||||
Map<String, String> cache = new HashMap<String, String>();
|
||||
|
||||
public void onReceive(Object msg) {
|
||||
if (msg instanceof Entry) {
|
||||
Entry entry = (Entry) msg;
|
||||
cache.put(entry.key, entry.value);
|
||||
} else if (msg instanceof Get) {
|
||||
Get get = (Get) msg;
|
||||
Object value = cache.get(get.key);
|
||||
sender().tell(value == null ? NOT_FOUND : value,
|
||||
getContext().self());
|
||||
} else if (msg instanceof Evict) {
|
||||
Evict evict = (Evict) msg;
|
||||
cache.remove(evict.key);
|
||||
} else {
|
||||
unhandled(msg);
|
||||
}
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.match(Entry.class, entry -> {
|
||||
cache.put(entry.key, entry.value);
|
||||
})
|
||||
.match(Get.class, get -> {
|
||||
Object value = cache.get(get.key);
|
||||
sender().tell(value == null ? NOT_FOUND : value,
|
||||
getContext().self());
|
||||
})
|
||||
.match(Evict.class, evict -> {
|
||||
cache.remove(evict.key);
|
||||
})
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ import akka.actor.ActorRef;
|
|||
import akka.actor.ActorSystem;
|
||||
import akka.actor.Props;
|
||||
//#imports1
|
||||
import akka.actor.UntypedActor;
|
||||
import akka.actor.AbstractActor;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
|
|
@ -94,9 +94,14 @@ public class CustomRouterDocTest extends AbstractJavaTest {
|
|||
|
||||
//#unit-test-logic
|
||||
|
||||
static public class Storage extends UntypedActor {
|
||||
public void onReceive(Object msg) {
|
||||
sender().tell(msg, self());
|
||||
static public class Storage extends AbstractActor {
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.matchAny(message -> {
|
||||
sender().tell(message, self());
|
||||
})
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ import akka.actor.ActorSystem;
|
|||
import akka.actor.ActorRef;
|
||||
import akka.actor.Props;
|
||||
import akka.actor.Terminated;
|
||||
import akka.actor.UntypedActor;
|
||||
import akka.actor.AbstractActor;
|
||||
import akka.routing.ActorRefRoutee;
|
||||
import akka.routing.Routee;
|
||||
import akka.routing.Router;
|
||||
|
|
@ -90,7 +90,7 @@ public class RouterDocTest extends AbstractJavaTest {
|
|||
//#router-in-actor
|
||||
static
|
||||
//#router-in-actor
|
||||
public class Master extends UntypedActor {
|
||||
public class Master extends AbstractActor {
|
||||
|
||||
Router router;
|
||||
{
|
||||
|
|
@ -103,39 +103,54 @@ public class RouterDocTest extends AbstractJavaTest {
|
|||
router = new Router(new RoundRobinRoutingLogic(), routees);
|
||||
}
|
||||
|
||||
public void onReceive(Object msg) {
|
||||
if (msg instanceof Work) {
|
||||
router.route(msg, sender());
|
||||
} else if (msg instanceof Terminated) {
|
||||
router = router.removeRoutee(((Terminated) msg).actor());
|
||||
ActorRef r = getContext().actorOf(Props.create(Worker.class));
|
||||
getContext().watch(r);
|
||||
router = router.addRoutee(new ActorRefRoutee(r));
|
||||
}
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.match(Work.class, message -> {
|
||||
router.route(message, sender());
|
||||
})
|
||||
.match(Terminated.class, message -> {
|
||||
router = router.removeRoutee(message.actor());
|
||||
ActorRef r = getContext().actorOf(Props.create(Worker.class));
|
||||
getContext().watch(r);
|
||||
router = router.addRoutee(new ActorRefRoutee(r));
|
||||
})
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
//#router-in-actor
|
||||
|
||||
static public class Worker extends UntypedActor {
|
||||
public void onReceive(Object msg) {}
|
||||
}
|
||||
|
||||
static public class Echo extends UntypedActor {
|
||||
public void onReceive(Object msg) {
|
||||
sender().tell(msg, self());
|
||||
static public class Worker extends AbstractActor {
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder().build();
|
||||
}
|
||||
}
|
||||
|
||||
static public class Replier extends UntypedActor {
|
||||
public void onReceive(Object msg) {
|
||||
//#reply-with-self
|
||||
sender().tell("reply", self());
|
||||
//#reply-with-self
|
||||
|
||||
//#reply-with-parent
|
||||
sender().tell("reply", getContext().parent());
|
||||
//#reply-with-parent
|
||||
static public class Echo extends AbstractActor {
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.matchAny(message -> sender().tell(message, self()))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
static public class Replier extends AbstractActor {
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.matchAny(message -> {
|
||||
//#reply-with-self
|
||||
sender().tell("reply", self());
|
||||
//#reply-with-self
|
||||
|
||||
//#reply-with-parent
|
||||
sender().tell("reply", getContext().parent());
|
||||
//#reply-with-parent
|
||||
})
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -143,7 +158,7 @@ public class RouterDocTest extends AbstractJavaTest {
|
|||
|
||||
static
|
||||
//#create-worker-actors
|
||||
public class Workers extends UntypedActor {
|
||||
public class Workers extends AbstractActor {
|
||||
@Override public void preStart() {
|
||||
getContext().actorOf(Props.create(Worker.class), "w1");
|
||||
getContext().actorOf(Props.create(Worker.class), "w2");
|
||||
|
|
@ -151,12 +166,14 @@ public class RouterDocTest extends AbstractJavaTest {
|
|||
}
|
||||
// ...
|
||||
//#create-worker-actors
|
||||
|
||||
|
||||
public void onReceive(Object msg) {}
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder().build();
|
||||
}
|
||||
}
|
||||
|
||||
static public class Parent extends UntypedActor {
|
||||
static public class Parent extends AbstractActor {
|
||||
|
||||
//#paths
|
||||
List<String> paths = Arrays.asList("/user/workers/w1", "/user/workers/w2",
|
||||
|
|
@ -346,11 +363,12 @@ public class RouterDocTest extends AbstractJavaTest {
|
|||
Props.create(Worker.class)), "router31");
|
||||
//#optimal-size-exploring-resize-pool
|
||||
|
||||
public void onReceive(Object msg) {}
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder().build();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void createActors() {
|
||||
//#create-workers
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -19,13 +19,16 @@ import akka.actor.ActorSystem;
|
|||
import akka.remote.RemoteScope;
|
||||
//#import
|
||||
|
||||
import akka.actor.UntypedActor;
|
||||
import akka.actor.AbstractActor;
|
||||
|
||||
public class RemoteDeploymentDocTest {
|
||||
|
||||
public static class SampleActor extends UntypedActor {
|
||||
public void onReceive(Object message) {
|
||||
sender().tell(self(), self());
|
||||
public static class SampleActor extends AbstractActor {
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.matchAny(message -> sender().tell(self(), self()))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -295,18 +295,17 @@ public class IntegrationDocTest extends AbstractJavaTest {
|
|||
//#sometimes-slow-service
|
||||
|
||||
//#ask-actor
|
||||
static class Translator extends UntypedActor {
|
||||
static class Translator extends AbstractActor {
|
||||
@Override
|
||||
public void onReceive(Object message) {
|
||||
if (message instanceof String) {
|
||||
String word = (String) message;
|
||||
// ... process message
|
||||
String reply = word.toUpperCase();
|
||||
// reply to the ask
|
||||
sender().tell(reply, self());
|
||||
} else {
|
||||
unhandled(message);
|
||||
}
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.match(String.class, word -> {
|
||||
// ... process message
|
||||
String reply = word.toUpperCase();
|
||||
// reply to the ask
|
||||
sender().tell(reply, self());
|
||||
})
|
||||
.build();
|
||||
}
|
||||
}
|
||||
//#ask-actor
|
||||
|
|
|
|||
|
|
@ -27,54 +27,52 @@ public class ParentChildTest {
|
|||
private final ActorSystem system = actorSystemResource.getSystem();
|
||||
|
||||
//#test-example
|
||||
static class Parent extends UntypedActor {
|
||||
static class Parent extends AbstractActor {
|
||||
final ActorRef child = getContext().actorOf(Props.create(Child.class), "child");
|
||||
boolean ponged = false;
|
||||
|
||||
@Override public void onReceive(Object message) throws Exception {
|
||||
if ("pingit".equals(message)) {
|
||||
child.tell("ping", self());
|
||||
} else if ("pong".equals(message)) {
|
||||
ponged = true;
|
||||
} else {
|
||||
unhandled(message);
|
||||
}
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.matchEquals("pingit", message -> child.tell("ping", self()))
|
||||
.matchEquals("pong", message -> ponged = true)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
static class Child extends UntypedActor {
|
||||
@Override public void onReceive(Object message) throws Exception {
|
||||
if ("ping".equals(message)) {
|
||||
getContext().parent().tell("pong", self());
|
||||
} else {
|
||||
unhandled(message);
|
||||
}
|
||||
static class Child extends AbstractActor {
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.matchEquals("ping", message -> {
|
||||
getContext().parent().tell("pong", self());
|
||||
})
|
||||
.build();
|
||||
}
|
||||
}
|
||||
//#test-example
|
||||
|
||||
static
|
||||
//#test-dependentchild
|
||||
class DependentChild extends UntypedActor {
|
||||
class DependentChild extends AbstractActor {
|
||||
private final ActorRef parent;
|
||||
|
||||
public DependentChild(ActorRef parent) {
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
@Override public void onReceive(Object message) throws Exception {
|
||||
if ("ping".equals(message)) {
|
||||
parent.tell("pong", self());
|
||||
} else {
|
||||
unhandled(message);
|
||||
}
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.matchEquals("ping", message -> parent.tell("pong", self()))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
//#test-dependentchild
|
||||
|
||||
static
|
||||
//#test-dependentparent
|
||||
class DependentParent extends UntypedActor {
|
||||
class DependentParent extends AbstractActor {
|
||||
final ActorRef child;
|
||||
boolean ponged = false;
|
||||
|
||||
|
|
@ -82,21 +80,19 @@ public class ParentChildTest {
|
|||
child = getContext().actorOf(childProps, "child");
|
||||
}
|
||||
|
||||
@Override public void onReceive(Object message) throws Exception {
|
||||
if ("pingit".equals(message)) {
|
||||
child.tell("ping", self());
|
||||
} else if ("pong".equals(message)) {
|
||||
ponged = true;
|
||||
} else {
|
||||
unhandled(message);
|
||||
}
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.matchEquals("pingit", message -> child.tell("ping", self()))
|
||||
.matchEquals("pong", message -> ponged = true)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
//#test-dependentparent
|
||||
|
||||
static
|
||||
//#test-dependentparent-generic
|
||||
class GenericDependentParent extends UntypedActor {
|
||||
class GenericDependentParent extends AbstractActor {
|
||||
final ActorRef child;
|
||||
boolean ponged = false;
|
||||
|
||||
|
|
@ -105,14 +101,12 @@ public class ParentChildTest {
|
|||
child = childMaker.apply(getContext());
|
||||
}
|
||||
|
||||
@Override public void onReceive(Object message) throws Exception {
|
||||
if ("pingit".equals(message)) {
|
||||
child.tell("ping", self());
|
||||
} else if ("pong".equals(message)) {
|
||||
ponged = true;
|
||||
} else {
|
||||
unhandled(message);
|
||||
}
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.matchEquals("pingit", message -> child.tell("ping", self()))
|
||||
.matchEquals("pong", message -> ponged = true)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
//#test-dependentparent-generic
|
||||
|
|
@ -174,15 +168,21 @@ public class ParentChildTest {
|
|||
}
|
||||
|
||||
@Override public Actor create() throws Exception {
|
||||
return new UntypedActor() {
|
||||
return new AbstractActor() {
|
||||
final ActorRef child = getContext().actorOf(Props.create(Child.class), "child");
|
||||
|
||||
@Override public void onReceive(Object x) throws Exception {
|
||||
if (sender().equals(child)) {
|
||||
proxy.ref().forward(x, getContext());
|
||||
} else {
|
||||
child.forward(x, getContext());
|
||||
}
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.matchAny(message -> {
|
||||
if (sender().equals(child)) {
|
||||
proxy.ref().forward(message, getContext());
|
||||
} else {
|
||||
child.forward(message, getContext());
|
||||
}
|
||||
})
|
||||
.build();
|
||||
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ import akka.actor.Kill;
|
|||
import akka.actor.PoisonPill;
|
||||
import akka.actor.Props;
|
||||
import akka.actor.Terminated;
|
||||
import akka.actor.UntypedActor;
|
||||
import akka.actor.AbstractActor;
|
||||
import scala.concurrent.Await;
|
||||
import scala.concurrent.Future;
|
||||
import akka.testkit.TestActor.AutoPilot;
|
||||
|
|
@ -34,13 +34,17 @@ public class TestKitDocTest {
|
|||
private final ActorSystem system = actorSystemResource.getSystem();
|
||||
|
||||
//#test-actor-ref
|
||||
static class MyActor extends UntypedActor {
|
||||
public void onReceive(Object o) throws Exception {
|
||||
if (o.equals("say42")) {
|
||||
sender().tell(42, self());
|
||||
} else if (o instanceof Exception) {
|
||||
throw (Exception) o;
|
||||
}
|
||||
static class MyActor extends AbstractActor {
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.matchEquals("say42", message -> {
|
||||
sender().tell(42, self());
|
||||
})
|
||||
.match(Exception.class, (Exception ex) -> {
|
||||
throw ex;
|
||||
})
|
||||
.build();
|
||||
}
|
||||
public boolean testMe() { return true; }
|
||||
}
|
||||
|
|
@ -259,14 +263,17 @@ public class TestKitDocTest {
|
|||
//#test-probe
|
||||
new JavaTestKit(system) {{
|
||||
// simple actor which just forwards messages
|
||||
class Forwarder extends UntypedActor {
|
||||
class Forwarder extends AbstractActor {
|
||||
final ActorRef target;
|
||||
@SuppressWarnings("unused")
|
||||
public Forwarder(ActorRef target) {
|
||||
this.target = target;
|
||||
}
|
||||
public void onReceive(Object msg) {
|
||||
target.forward(msg, getContext());
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.matchAny(message -> target.forward(message, getContext()))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -12,25 +12,27 @@ import org.junit.Test;
|
|||
import akka.actor.ActorRef;
|
||||
import akka.actor.ActorSystem;
|
||||
import akka.actor.Props;
|
||||
import akka.actor.UntypedActor;
|
||||
import akka.actor.AbstractActor;
|
||||
import akka.testkit.JavaTestKit;
|
||||
import scala.concurrent.duration.Duration;
|
||||
|
||||
public class TestKitSampleTest {
|
||||
|
||||
public static class SomeActor extends UntypedActor {
|
||||
public static class SomeActor extends AbstractActor {
|
||||
ActorRef target = null;
|
||||
|
||||
public void onReceive(Object msg) {
|
||||
|
||||
if (msg.equals("hello")) {
|
||||
sender().tell("world", self());
|
||||
if (target != null) target.forward(msg, getContext());
|
||||
|
||||
} else if (msg instanceof ActorRef) {
|
||||
target = (ActorRef) msg;
|
||||
sender().tell("done", self());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.matchEquals("hello", message -> {
|
||||
sender().tell("world", self());
|
||||
if (target != null) target.forward(message, getContext());
|
||||
})
|
||||
.match(ActorRef.class, actorRef -> {
|
||||
target = actorRef;
|
||||
sender().tell("done", self());
|
||||
})
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue