Contenu connexe
Similaire à JJUG CCC 2018 Spring - I-7 (俺が)はじめての Netty (20)
Plus de Shinya Mochida (20)
JJUG CCC 2018 Spring - I-7 (俺が)はじめての Netty
- 11. Norman Maurer in Netty in Action 2016
Our primary goal has been to make Netty
accessible to the broadest range of
developers.
- 13. Java API I/O(1)
•JDK 1.0 java.net
API
•accept / read / write
API
•
(block)
• OIO (Old blocking I/O)
- 14. Java API I/O(2)
•Java 1.4 java.nio.channels
API
•select( epoll/kqueue)
API
•
• NIO(New Non-blocking I/O)
- 17. OIO Echo
class EchoServer {
public static void main(String[] args) {
var serverSocket = new ServerSocket(8000); //(1)
while(true) {
Socket socket = serverSocket.accept(); // (2)
var reader = toBufferedReader(socket.getInputStream());
var writer = toPrintWriter(socket.getOutputStream());
String line = reader.readLine(); // (3)
System.out.println(line); // (4)
writer.println(line); // (5)
socket.close(); // (6)
}
}
}
- 26. read/write
ExecutorService exec = Executors.newFixedThreadPool(THREAD_NUM);
var serverSocket = new ServerSocket(8000); //(1)
while(true) {
Socket socket = serverSocket.accept(); // (2)
exec.submit(() -> {
var reader = toBufferedReader(socket.getInputStream());
var writer = toPrintWriter(socket.getOutputStream());
String line = reader.readLine(); // (3)
System.out.println(line); //(3)
writer.println(line); // (4)
socket.close();
});
}
- 32. OIO
• ( )
• read/write/close
•
read/write
( )
• /CPU
- 35. NIO Echo
1. ( ) I/O (OP_ACCEPT/OP_READ/
OP_WRITE)
2.
3. OP_ACCEPT
(OP_READ)
4. OP_READ
(OP_WRITE)
5. OP_WRITE
(OP_WRITE)
- 36. I/O
•Selector 4
•OP_READ - Channel
•OP_WRITE - Channel
•OP_CONNECT - Channel
(
)
•OP_ACCEPT - ServerSocketChannel (
) (
listen )
- 37. class EchoServer {
public static void main(String[] args) {
var channel = ServerSocketChannel.open();
channel.setOption(SO_REUSEADDR, true);
channel.bind(new InetSocketAddress(PORT));
channel.configureBlocking(false);
var selector = Selector.open();
channel.register(selector, SelectionKey.OP_ACCEPT);
runLoop(selector);
}
- 38. void runLoop(Selector selector) {
while(true) {
selector.select();
Set<SelectionKey> selectionKeys =
selector.selectedKeys();
for (var key: selectionKeys) {
if (key.isAcceptable()) accept(key, selector);
if (key.isReadable()) read(key);
if (key.isWritable()) write(key);
selectionKeys.remove(key);
}
}
- 39. OP_ACCEPT
void access(SelectionKey key, Selector selector) {
var serverSocketChannel =
(ServerSocketChannel) key.channel();
SocketChannel channel = serverSocketChannel.accept();
channel.configureBlocking(false);
channel.register(
selector,
SelectionKey.OP_READ,
ByteBuffer.allocate(1024));
}
OP_READ
- 40. OP_READ
void read(SelectionKey key) {
SocketChannel channel =
(SocketChannel) key.channel;
var buf = (ByteBuffer) key.attachment();
int size = channel.read(buf);
var bytes = new byte[size];
buf.flip().get(bytes);
buf.clear();
buf.put(handleMessage(bytes));
// read 1
key.interestOps(SelectionKey.OP_WRITE);
}
OP_WRITE
- 41. OP_WRITE
void write(SelectionKey key) {
SocketChannel channel = (SocketChannel) key.channel;
var buf = ((ByteBuffer) key.attachment()).flip();
channel.write(buf);
buf.compact();
if (buf.position() > 0) { // NW 1
key.interestOps(SelectionKey.OP_WRITE);
} else {
buf.clear();
channel.close();
}
}
- 61. 1. ChannelInboundHandlerAdapter
class EchoHandler extends ChannelInboundHandlerAdapter {
@Override public void channelRead(
ChannelContext ctx, Object msg) {
var buf = (ByteBuf) msg;
String message = buf.toString(UTF_8);
ByteBuf res = ctx.alloc().buffer();
res.writeCharSequence(message, UTF_8);
ctx.writeAndFlush(res);
}
}
channelRead OP_READ
byte[] String
ByteBuf
Netty
- 63. 3.
ServerBootstrap
class EchoServer {
public static void main(String[] args) {
var parent = new NioEventLoopGroup();
var child = new NioEventLoopGroup();
var bootstrap = new ServerBootstrap();
bootstrap.group(parent, child)
.channel(NioServerSocketChannel.class)
.localAddress(8000)
.childHandler(new InitHandler());
ChannelFuture future = bootstrap.bind().sync();
future.channel().closeFuture().sync();
}
}
ServerBootstrap
blocking I/O
NIO
- 64. 3.
ServerBootstrap
class EchoServer {
public static void main(String[] args) {
var parent = new NioEventLoopGroup();
var child = new NioEventLoopGroup();
var bootstrap = new ServerBootstrap();
bootstrap.group(parent, child)
.channel(NioServerSocketChannel.class)
.localAddress(8000)
.childHandler(new InitHandler());
ChannelFuture future = bootstrap.bind().sync();
future.channel().closeFuture().sync();
}
}
Netty …
- 107. ByteBuffer
ByteBuffer first = …
ByteBuffer second = …
var byteBuffer = ByteBuffer.allocate(
first.remaining(), second.remaining());
byteBuffer.put(first);
byteBuffer.put(second);
byteBuffer.flip();
- 109. ByteBuf
ByteBuf first = …
ByteBuf second = …
var byteBuf = Unpooled.compositeBuffer();
byteBuf.addComponents(true, first, second);
- 115. ( )
• / Channel EventLoop
•
•TCP -
•UDP -
•SCTP -
•
•OIO - (Java I/O )
•NIO - (select epoll kqueue)
•epoll - (edge trigger/ Linux OS )
•kqueue - BSD OS
- 116. ( )
•Channel
• /TCP/NIO : NioServerSocketChannel
• /TCP/NIO : NioSocketChannel
• /TCP/epoll : EpollServerSocketChannel
•EventLoop
• /NIO : NioEventLoopGroup x 2
• /OIO : OioEventLoopGroup x 1
• /epoll : EpollEventLoopGroup x 2
- 117. ServerBootstrap ( )
class EchoServer {
public static void main(String[] args) {
var parent = new NioEventLoopGroup();
var child = new NioEventLoopGroup();
var bootstrap = new ServerBootstrap();
bootstrap.group(parent, child)
.channel(NioServerSocketChannel.class)
.localAddress(8000)
.childHandler(new InitHandler());
ChannelFuture future = bootstrap.bind().sync();
future.channel().closeFuture().sync();
}
}
- 125. • Netty OIO NIO
• Netty NIO
• Netty
/ /
(
)
• Spring WebFlux/Play-Akka
- 126. •Netty In Action
•Norman Maurer/Marvin Allen Wolfthal
•Manning 2016
•Java
•Scott Oaks
•Acroquest Technology/
•
• 2015