feat(新增): 🚀 NettyFuture先导
This commit is contained in:
parent
7a7030a0e4
commit
41a4c7f388
|
@ -0,0 +1,62 @@
|
|||
package cn.bunny;
|
||||
|
||||
import io.netty.bootstrap.Bootstrap;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelFuture;
|
||||
import io.netty.channel.ChannelFutureListener;
|
||||
import io.netty.channel.ChannelInitializer;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.channel.socket.nio.NioSocketChannel;
|
||||
import io.netty.handler.codec.string.StringEncoder;
|
||||
import io.netty.handler.logging.LogLevel;
|
||||
import io.netty.handler.logging.LoggingHandler;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.Scanner;
|
||||
|
||||
@Slf4j
|
||||
public class InputClientTest {
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
NioEventLoopGroup group = new NioEventLoopGroup();
|
||||
ChannelFuture future = new Bootstrap()
|
||||
// .group(new NioEventLoopGroup())
|
||||
.group(group)
|
||||
.channel(NioSocketChannel.class)
|
||||
.handler(new ChannelInitializer<NioSocketChannel>() {
|
||||
@Override
|
||||
protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
|
||||
nioSocketChannel.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));
|
||||
nioSocketChannel.pipeline().addLast(new StringEncoder());
|
||||
}
|
||||
}).connect(new InetSocketAddress("localhost", 8080));
|
||||
|
||||
Channel channel = future.sync().channel();
|
||||
|
||||
new Thread(() -> {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
while (true) {
|
||||
String line = scanner.nextLine();
|
||||
if ("q".equals(line)) {
|
||||
channel.close();
|
||||
break;
|
||||
}
|
||||
channel.writeAndFlush(line);
|
||||
}
|
||||
}, "input").start();
|
||||
|
||||
// 获取CloseFuture 对象,同步处理处理关闭 异步处理要关闭
|
||||
ChannelFuture channelFuture = channel.closeFuture();
|
||||
System.out.println("等待关闭......");
|
||||
|
||||
// 第一种方式关闭
|
||||
// channelFuture.sync();
|
||||
// log.debug("处理之后的关闭");
|
||||
|
||||
// 第二种方式关闭
|
||||
channelFuture.addListener((ChannelFutureListener) future1 -> {
|
||||
log.debug("处理之后的关闭");
|
||||
group.shutdownGracefully();
|
||||
});
|
||||
}
|
||||
}
|
|
@ -15,8 +15,10 @@ import java.nio.charset.Charset;
|
|||
@Slf4j
|
||||
public class EventLoopServerTest {
|
||||
public static void main(String[] args) {
|
||||
// 创建服务器
|
||||
new ServerBootstrap()
|
||||
.group(new NioEventLoopGroup())
|
||||
// BOOS只负责accept事件,worker 只负责 SocketChannel 上的读写
|
||||
.group(new NioEventLoopGroup(), new NioEventLoopGroup(2))// 设置工作线程为2个
|
||||
.channel(NioServerSocketChannel.class)
|
||||
.childHandler(new ChannelInitializer<NioSocketChannel>() {
|
||||
@Override
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
package cn.bunny.demo4;
|
||||
|
||||
import io.netty.bootstrap.ServerBootstrap;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelInboundHandlerAdapter;
|
||||
import io.netty.channel.ChannelInitializer;
|
||||
import io.netty.channel.DefaultEventLoopGroup;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
||||
import io.netty.channel.socket.nio.NioSocketChannel;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
@Slf4j
|
||||
public class TestDefaultServerLoopGroup {
|
||||
public static void main(String[] args) {
|
||||
DefaultEventLoopGroup group = new DefaultEventLoopGroup();
|
||||
new ServerBootstrap()
|
||||
.group(new NioEventLoopGroup(), new NioEventLoopGroup(2))
|
||||
.channel(NioServerSocketChannel.class)
|
||||
.childHandler(new ChannelInitializer<NioSocketChannel>() {
|
||||
@Override
|
||||
protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
|
||||
nioSocketChannel.pipeline().addLast("handler", new ChannelInboundHandlerAdapter() {
|
||||
@Override
|
||||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
||||
ByteBuf buf = (ByteBuf) msg;
|
||||
log.debug(buf.toString((Charset.defaultCharset())));
|
||||
// 让消息继续传递下去
|
||||
ctx.fireChannelRead(msg);
|
||||
}
|
||||
}).addLast(group, "hander2", new ChannelInboundHandlerAdapter() {
|
||||
@Override
|
||||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
||||
ByteBuf buf = (ByteBuf) msg;
|
||||
log.debug(buf.toString(Charset.defaultCharset()));
|
||||
super.channelRead(ctx, msg);
|
||||
}
|
||||
});
|
||||
}
|
||||
}).bind(8080);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue