parent
537840bd2a
commit
487083a9c3
13 changed files with 467 additions and 74 deletions
|
|
@ -44,7 +44,7 @@ public class IODocTest {
|
|||
1234);
|
||||
final List<Inet.SocketOption> options = new ArrayList<Inet.SocketOption>();
|
||||
options.add(TcpSO.keepAlive(true));
|
||||
tcp.tell(TcpMessage.connect(remoteAddr, localAddr, options), getSelf());
|
||||
tcp.tell(TcpMessage.connect(remoteAddr, localAddr, options, null, false), getSelf());
|
||||
//#connect-with-options
|
||||
} else
|
||||
//#connected
|
||||
|
|
@ -80,7 +80,7 @@ public class IODocTest {
|
|||
1234);
|
||||
final List<Inet.SocketOption> options = new ArrayList<Inet.SocketOption>();
|
||||
options.add(TcpSO.reuseAddress(true));
|
||||
tcp.tell(TcpMessage.bind(handler, localAddr, 10, options), getSelf());
|
||||
tcp.tell(TcpMessage.bind(handler, localAddr, 10, options, false), getSelf());
|
||||
//#bind
|
||||
}
|
||||
}
|
||||
|
|
|
|||
93
akka-docs/rst/java/code/docs/io/JavaReadBackPressure.java
Normal file
93
akka-docs/rst/java/code/docs/io/JavaReadBackPressure.java
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
package docs.io;
|
||||
|
||||
import akka.actor.ActorRef;
|
||||
import akka.actor.Props;
|
||||
import akka.actor.UntypedActor;
|
||||
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;
|
||||
|
||||
/**
|
||||
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
public class JavaReadBackPressure {
|
||||
|
||||
static public class Listener extends UntypedActor {
|
||||
ActorRef tcp;
|
||||
ActorRef listener;
|
||||
|
||||
@Override
|
||||
//#pull-accepting
|
||||
public void onReceive(Object message) throws Exception {
|
||||
if (message instanceof Tcp.Bound) {
|
||||
listener = getSender();
|
||||
// Accept connections one by one
|
||||
listener.tell(TcpMessage.resumeAccepting(1), getSelf());
|
||||
} else if (message instanceof Tcp.Connected) {
|
||||
ActorRef handler = getContext().actorOf(Props.create(PullEcho.class, getSender()));
|
||||
getSender().tell(TcpMessage.register(handler), getSelf());
|
||||
// Resume accepting connections
|
||||
listener.tell(TcpMessage.resumeAccepting(1), getSelf());
|
||||
}
|
||||
}
|
||||
//#pull-accepting
|
||||
|
||||
@Override
|
||||
public void preStart() throws Exception {
|
||||
//#pull-mode-bind
|
||||
tcp = Tcp.get(getContext().system()).manager();
|
||||
final List<Inet.SocketOption> options = new ArrayList<Inet.SocketOption>();
|
||||
tcp.tell(
|
||||
TcpMessage.bind(getSelf(), new InetSocketAddress("localhost", 0), 100, options, true),
|
||||
getSelf()
|
||||
);
|
||||
//#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),
|
||||
getSelf()
|
||||
);
|
||||
//#pull-mode-connect
|
||||
}
|
||||
}
|
||||
|
||||
static public class Ack implements Tcp.Event {
|
||||
}
|
||||
|
||||
static public class PullEcho extends UntypedActor {
|
||||
final ActorRef connection;
|
||||
|
||||
public PullEcho(ActorRef connection) {
|
||||
this.connection = connection;
|
||||
}
|
||||
|
||||
//#pull-reading-echo
|
||||
@Override
|
||||
public void preStart() throws Exception {
|
||||
connection.tell(TcpMessage.resumeReading(), getSelf());
|
||||
}
|
||||
|
||||
@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()), getSelf());
|
||||
} else if (message instanceof Ack) {
|
||||
connection.tell(TcpMessage.resumeReading(), getSelf());
|
||||
}
|
||||
}
|
||||
//#pull-reading-echo
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue