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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue