2017-03-16 09:30:00 +01:00
|
|
|
package jdocs.io;
|
2014-01-07 15:50:36 +01:00
|
|
|
|
|
|
|
|
import akka.actor.ActorRef;
|
|
|
|
|
import akka.actor.Props;
|
2017-02-04 11:51:30 +05:00
|
|
|
import akka.actor.AbstractActor;
|
2014-01-07 15:50:36 +01:00
|
|
|
import akka.io.Inet;
|
|
|
|
|
import akka.io.Tcp;
|
|
|
|
|
import akka.io.TcpMessage;
|
|
|
|
|
import akka.util.ByteString;
|
|
|
|
|
|
|
|
|
|
import java.net.InetSocketAddress;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
/**
|
2017-01-04 17:37:10 +01:00
|
|
|
* Copyright (C) 2009-2017 Lightbend Inc. <http://www.lightbend.com>
|
2014-01-07 15:50:36 +01:00
|
|
|
*/
|
|
|
|
|
public class JavaReadBackPressure {
|
|
|
|
|
|
2017-02-04 11:51:30 +05:00
|
|
|
static public class Listener extends AbstractActor {
|
2014-01-07 15:50:36 +01:00
|
|
|
ActorRef tcp;
|
|
|
|
|
ActorRef listener;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
//#pull-accepting
|
2017-02-04 11:51:30 +05:00
|
|
|
public Receive createReceive() {
|
|
|
|
|
return receiveBuilder()
|
|
|
|
|
.match(Tcp.Bound.class, x -> {
|
2017-03-16 09:30:00 +01:00
|
|
|
listener = getSender();
|
2017-02-04 11:51:30 +05:00
|
|
|
// Accept connections one by one
|
2017-03-16 09:30:00 +01:00
|
|
|
listener.tell(TcpMessage.resumeAccepting(1), getSelf());
|
2017-02-04 11:51:30 +05:00
|
|
|
})
|
|
|
|
|
.match(Tcp.Connected.class, x -> {
|
2017-03-16 09:30:00 +01:00
|
|
|
ActorRef handler = getContext().actorOf(Props.create(PullEcho.class, getSender()));
|
|
|
|
|
getSender().tell(TcpMessage.register(handler), getSelf());
|
2017-02-04 11:51:30 +05:00
|
|
|
// Resume accepting connections
|
2017-03-16 09:30:00 +01:00
|
|
|
listener.tell(TcpMessage.resumeAccepting(1), getSelf());
|
2017-02-04 11:51:30 +05:00
|
|
|
})
|
|
|
|
|
.build();
|
2014-01-07 15:50:36 +01:00
|
|
|
}
|
|
|
|
|
//#pull-accepting
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void preStart() throws Exception {
|
|
|
|
|
//#pull-mode-bind
|
2017-03-16 09:30:00 +01:00
|
|
|
tcp = Tcp.get(getContext().getSystem()).manager();
|
2014-01-07 15:50:36 +01:00
|
|
|
final List<Inet.SocketOption> options = new ArrayList<Inet.SocketOption>();
|
|
|
|
|
tcp.tell(
|
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
|
|
|
TcpMessage.bind(self(), new InetSocketAddress("localhost", 0), 100, options, true),
|
2017-03-16 09:30:00 +01:00
|
|
|
getSelf()
|
2014-01-07 15:50:36 +01:00
|
|
|
);
|
|
|
|
|
//#pull-mode-bind
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void demonstrateConnect() {
|
|
|
|
|
//#pull-mode-connect
|
|
|
|
|
final List<Inet.SocketOption> options = new ArrayList<Inet.SocketOption>();
|
|
|
|
|
tcp.tell(
|
|
|
|
|
TcpMessage.connect(new InetSocketAddress("localhost", 3000), null, options, null, true),
|
2017-03-16 09:30:00 +01:00
|
|
|
getSelf()
|
2014-01-07 15:50:36 +01:00
|
|
|
);
|
|
|
|
|
//#pull-mode-connect
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static public class Ack implements Tcp.Event {
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-04 11:51:30 +05:00
|
|
|
static public class PullEcho extends AbstractActor {
|
2014-01-07 15:50:36 +01:00
|
|
|
final ActorRef connection;
|
|
|
|
|
|
|
|
|
|
public PullEcho(ActorRef connection) {
|
|
|
|
|
this.connection = connection;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//#pull-reading-echo
|
|
|
|
|
@Override
|
|
|
|
|
public void preStart() throws Exception {
|
2017-03-16 09:30:00 +01:00
|
|
|
connection.tell(TcpMessage.resumeReading(), getSelf());
|
2014-01-07 15:50:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2017-02-04 11:51:30 +05:00
|
|
|
public Receive createReceive() {
|
|
|
|
|
return receiveBuilder()
|
|
|
|
|
.match(Tcp.Received.class, message -> {
|
|
|
|
|
ByteString data = message.data();
|
2017-03-16 09:30:00 +01:00
|
|
|
connection.tell(TcpMessage.write(data, new Ack()), getSelf());
|
2017-02-04 11:51:30 +05:00
|
|
|
})
|
|
|
|
|
.match(Ack.class, message -> {
|
2017-03-16 09:30:00 +01:00
|
|
|
connection.tell(TcpMessage.resumeReading(), getSelf());
|
2017-02-04 11:51:30 +05:00
|
|
|
})
|
|
|
|
|
.build();
|
2014-01-07 15:50:36 +01:00
|
|
|
}
|
|
|
|
|
//#pull-reading-echo
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|