fix:详细分类

上级 7ac95a63
package com.kwan.shuyu.heima.netty_04_netty.c10_listener;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import lombok.extern.slf4j.Slf4j;
import java.nio.charset.Charset;
/**
* 服务端3个handler
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/23 10:51
*/
@Slf4j
public class TestEventLoop_01_server {
public static void main(String[] args) {
//普通任务
final EventLoopGroup group = new DefaultEventLoopGroup();
new ServerBootstrap()
//boss 只负责ServerSocketChannel 上 accept 事件 worker只负责socketChannel上的读写
.group(new NioEventLoopGroup(), new NioEventLoopGroup(2))
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<NioSocketChannel>() {
@Override
protected void initChannel(NioSocketChannel ch) throws Exception {
//支持链式调用
ch.pipeline().addLast("handler1", new ChannelInboundHandlerAdapter() {
// ByteBuf
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
log.info("我是handler1,{}", buf.toString(Charset.defaultCharset()));
ctx.fireChannelRead(msg);
}
}).addLast(group, "handler2", new ChannelInboundHandlerAdapter() {
// handler2 是一个普通任务,第一个参数是EventExecutorGroup,指定group执行
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
log.info("我是handler1,{}", buf.toString(Charset.defaultCharset()));
}
});
//添加解码器
ch.pipeline().addLast("handler0", new StringDecoder());
}
})
.bind(8080);
}
}
\ No newline at end of file
package com.kwan.shuyu.heima.netty_04_netty.c3_channel;
package com.kwan.shuyu.heima.netty_04_netty.c10_listener;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
......@@ -13,14 +13,14 @@ import lombok.extern.slf4j.Slf4j;
import java.net.InetSocketAddress;
/**
* ChannelFuture
* 客户端使用addListener异步接受
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/23 16:43
*/
@Slf4j
public class TestEventLoop_04_ChannelFuture {
public class TestEventLoop_02_client {
public static void main(String[] args) throws InterruptedException {
//1.启动类
final ChannelFuture channelFuture = new Bootstrap()
......@@ -37,6 +37,7 @@ public class TestEventLoop_04_ChannelFuture {
})
//连接到服务器
.connect(new InetSocketAddress("localhost", 8080));
//连接上服务器后回调
channelFuture.addListener((ChannelFutureListener) future -> {
//获取Channel
final Channel channel = future.channel();
......
package com.kwan.shuyu.heima.netty_04_netty.c11_Scanner;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import lombok.extern.slf4j.Slf4j;
import java.nio.charset.Charset;
/**
* 服务端3个Handler
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/23 10:51
*/
@Slf4j
public class TestEventLoop_01_server {
public static void main(String[] args) {
//普通任务
final EventLoopGroup group = new DefaultEventLoopGroup();
new ServerBootstrap()
//boss 只负责ServerSocketChannel 上 accept 事件 worker只负责socketChannel上的读写
.group(new NioEventLoopGroup(), new NioEventLoopGroup(2))
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<NioSocketChannel>() {
@Override
protected void initChannel(NioSocketChannel ch) throws Exception {
//支持链式调用
ch.pipeline().addLast("handler1", new ChannelInboundHandlerAdapter() {
// ByteBuf
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
log.info("我是handler1,{}", buf.toString(Charset.defaultCharset()));
ctx.fireChannelRead(msg);
}
}).addLast(group, "handler2", new ChannelInboundHandlerAdapter() {
// handler2 是一个普通任务,第一个参数是EventExecutorGroup,指定group执行
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
log.info("我是handler1,{}", buf.toString(Charset.defaultCharset()));
}
});
//添加解码器
ch.pipeline().addLast("handler0", new StringDecoder());
}
})
.bind(8080);
}
}
\ No newline at end of file
package com.kwan.shuyu.heima.netty_04_netty.c3_channel;
package com.kwan.shuyu.heima.netty_04_netty.c11_Scanner;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
......@@ -15,14 +15,14 @@ import java.net.InetSocketAddress;
import java.util.Scanner;
/**
* ChannelFuture和LoggingHandler使用
* 客户端添加Scanner输入
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/23 16:43
*/
@Slf4j
public class TestEventLoop_05_ChannelFuture {
public class TestEventLoop_02_client {
public static void main(String[] args) throws InterruptedException {
//1.启动类
final ChannelFuture channelFuture = new Bootstrap()
......
package com.kwan.shuyu.heima.netty_04_netty.c1_基础;
package com.kwan.shuyu.heima.netty_04_netty.c1_basic;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelInitializer;
......
package com.kwan.shuyu.heima.netty_04_netty.c1_基础;
package com.kwan.shuyu.heima.netty_04_netty.c1_basic;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelHandlerContext;
......@@ -11,7 +11,7 @@ import io.netty.handler.codec.string.StringDecoder;
/**
* 服务端
* 服务端ChannelInboundHandlerAdapter自定义handler
*
* @author : qinyingjie
* @version : 2.2.0
......
package com.kwan.shuyu.heima.netty_04_netty.c2_组件;
package com.kwan.shuyu.heima.netty_04_netty.c2_ByteBuf;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelInitializer;
......@@ -16,7 +16,7 @@ import java.net.InetSocketAddress;
* @version : 2.2.0
* @date : 2023/4/23 16:43
*/
public class TestEventLoop_04_Client {
public class TestEventLoop_01_Client {
public static void main(String[] args) throws InterruptedException {
//1.启动类
new Bootstrap()
......
package com.kwan.shuyu.heima.netty_04_netty.c2_组件;
package com.kwan.shuyu.heima.netty_04_netty.c2_ByteBuf;
import io.netty.bootstrap.ServerBootstrap;
......@@ -14,14 +14,14 @@ import lombok.extern.slf4j.Slf4j;
import java.nio.charset.Charset;
/**
* 服务端
* 服务端接受的数据转ByteBuf
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/23 10:51
*/
@Slf4j
public class TestEventLoop_04_server {
public class TestEventLoop_02_server {
public static void main(String[] args) {
new ServerBootstrap()
......
package com.kwan.shuyu.heima.netty_04_netty.c3_fireChannelRead;
import io.netty.bootstrap.Bootstrap;
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 java.net.InetSocketAddress;
/**
* 客户端
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/23 16:43
*/
public class TestEventLoop_01_Client {
public static void main(String[] args) throws InterruptedException {
//1.启动类
new Bootstrap()
// 2.添加 EventLoop
.group(new NioEventLoopGroup())
//3.选择客户端channel实现
.channel(NioSocketChannel.class)
// 4.添加处理器
.handler(new ChannelInitializer<NioSocketChannel>() {
@Override //在连接建立后被调用
protected void initChannel(NioSocketChannel ch) throws Exception {
ch.pipeline().addLast(new StringEncoder());
}
})
//连接到服务器
.connect(new InetSocketAddress("localhost", 8080))
.sync()
.channel()
//发送数据
.writeAndFlush("hello world")
;
}
}
\ No newline at end of file
package com.kwan.shuyu.heima.netty_04_netty.c2_组件;
package com.kwan.shuyu.heima.netty_04_netty.c3_fireChannelRead;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
......@@ -11,14 +11,14 @@ import lombok.extern.slf4j.Slf4j;
import java.nio.charset.Charset;
/**
* 作用NioEventLoopGroup
* 使用fireChannelRead进行数据传递
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/23 10:51
*/
@Slf4j
public class TestEventLoop_05_group {
public class TestEventLoop_02_client {
public static void main(String[] args) {
//普通任务
final EventLoopGroup group = new DefaultEventLoopGroup();
......
package com.kwan.shuyu.heima.netty_04_netty.c2_组件;
package com.kwan.shuyu.heima.netty_04_netty.c4_EventLoopGroup;
import io.netty.channel.DefaultEventLoopGroup;
......
package com.kwan.shuyu.heima.netty_04_netty.c2_组件;
package com.kwan.shuyu.heima.netty_04_netty.c4_EventLoopGroup;
import io.netty.channel.DefaultEventLoopGroup;
......
package com.kwan.shuyu.heima.netty_04_netty.c2_组件;
package com.kwan.shuyu.heima.netty_04_netty.c5_schedule;
import io.netty.channel.EventLoopGroup;
......
package com.kwan.shuyu.heima.netty_04_netty.c6_codec;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.DefaultEventLoopGroup;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import lombok.extern.slf4j.Slf4j;
/**
* 解码StringDecoder
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/23 10:51
*/
@Slf4j
public class TestEventLoop_01_server {
public static void main(String[] args) {
//普通任务
final EventLoopGroup group = new DefaultEventLoopGroup();
new ServerBootstrap()
//boss 只负责ServerSocketChannel 上 accept 事件 worker只负责socketChannel上的读写
.group(new NioEventLoopGroup(), new NioEventLoopGroup(2))
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<NioSocketChannel>() {
@Override
protected void initChannel(NioSocketChannel ch) throws Exception {
//添加解码器
ch.pipeline().addLast("handler0", new StringDecoder());
}
})
.bind(8080);
}
}
\ No newline at end of file
package com.kwan.shuyu.heima.netty_04_netty.c6_codec;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
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 lombok.extern.slf4j.Slf4j;
import java.net.InetSocketAddress;
/**
* 编码StringEncoder
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/23 16:43
*/
@Slf4j
public class TestEventLoop_02_client {
public static void main(String[] args) throws InterruptedException {
//1.启动类
final ChannelFuture channelFuture = new Bootstrap()
// 2.添加 EventLoop
.group(new NioEventLoopGroup())
//3.选择客户端channel实现
.channel(NioSocketChannel.class)
// 4.添加处理器4
.handler(new ChannelInitializer<NioSocketChannel>() {
@Override //在连接建立后被调用
protected void initChannel(NioSocketChannel ch) throws Exception {
ch.pipeline().addLast(new StringEncoder());
}
})
//连接到服务器
.connect(new InetSocketAddress("localhost", 8080));
Channel channel = channelFuture.sync().channel();
}
}
\ No newline at end of file
package com.kwan.shuyu.heima.netty_04_netty.c7_channel;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import lombok.extern.slf4j.Slf4j;
import java.nio.charset.Charset;
/**
* 服务端三个handler
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/23 10:51
*/
@Slf4j
public class TestEventLoop_01_server {
public static void main(String[] args) {
//普通任务
final EventLoopGroup group = new DefaultEventLoopGroup();
new ServerBootstrap()
//boss 只负责ServerSocketChannel 上 accept 事件 worker只负责socketChannel上的读写
.group(new NioEventLoopGroup(), new NioEventLoopGroup(2))
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<NioSocketChannel>() {
@Override
protected void initChannel(NioSocketChannel ch) throws Exception {
//支持链式调用
ch.pipeline().addLast("handler1", new ChannelInboundHandlerAdapter() {
// ByteBuf
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
log.info("我是handler1,{}", buf.toString(Charset.defaultCharset()));
ctx.fireChannelRead(msg);
}
}).addLast(group, "handler2", new ChannelInboundHandlerAdapter() {
// handler2 是一个普通任务,第一个参数是EventExecutorGroup,指定group执行
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
log.info("我是handler1,{}", buf.toString(Charset.defaultCharset()));
}
});
//添加解码器
ch.pipeline().addLast("handler0", new StringDecoder());
}
})
.bind(8080);
}
}
\ No newline at end of file
package com.kwan.shuyu.heima.netty_04_netty.c3_channel;
package com.kwan.shuyu.heima.netty_04_netty.c7_channel;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
......@@ -11,7 +11,7 @@ import java.net.InetSocketAddress;
/**
* Channel
* 客户端使用sync链式调用
*
* @author : qinyingjie
* @version : 2.2.0
......
package com.kwan.shuyu.heima.netty_04_netty.c8_log;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import lombok.extern.slf4j.Slf4j;
import java.nio.charset.Charset;
/**
* 普通任务使用DefaultEventLoopGroup执行
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/23 10:51
*/
@Slf4j
public class TestEventLoop_01_server {
public static void main(String[] args) {
//普通任务
final EventLoopGroup group = new DefaultEventLoopGroup();
new ServerBootstrap()
//boss 只负责ServerSocketChannel 上 accept 事件 worker只负责socketChannel上的读写
.group(new NioEventLoopGroup(), new NioEventLoopGroup(2))
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<NioSocketChannel>() {
@Override
protected void initChannel(NioSocketChannel ch) throws Exception {
//支持链式调用
ch.pipeline().addLast("handler1", new ChannelInboundHandlerAdapter() {
// ByteBuf
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
log.info("我是handler1,{}", buf.toString(Charset.defaultCharset()));
ctx.fireChannelRead(msg);
}
}).addLast(group, "handler2", new ChannelInboundHandlerAdapter() {
// handler2 是一个普通任务,第一个参数是EventExecutorGroup,指定group执行
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
log.info("我是handler1,{}", buf.toString(Charset.defaultCharset()));
}
});
//添加解码器
ch.pipeline().addLast("handler0", new StringDecoder());
}
})
.bind(8080);
}
}
\ No newline at end of file
package com.kwan.shuyu.heima.netty_04_netty.c8_log;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
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;
/**
* 打印字节日志LoggingHandler
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/23 16:43
*/
@Slf4j
public class TestEventLoop_02_client {
public static void main(String[] args) throws InterruptedException {
//1.启动类
final ChannelFuture channelFuture = new Bootstrap()
// 2.添加 EventLoop
.group(new NioEventLoopGroup())
//3.选择客户端channel实现
.channel(NioSocketChannel.class)
// 4.添加处理器4
.handler(new ChannelInitializer<NioSocketChannel>() {
@Override //在连接建立后被调用
protected void initChannel(NioSocketChannel ch) throws Exception {
ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO));
ch.pipeline().addLast(new StringEncoder());
}
})
//连接到服务器
.connect(new InetSocketAddress("localhost", 8080));
Channel channel = channelFuture.sync().channel();
}
}
\ No newline at end of file
package com.kwan.shuyu.heima.netty_04_netty.c3_channel;
package com.kwan.shuyu.heima.netty_04_netty.c9_sync;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
......@@ -6,12 +6,13 @@ import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import lombok.extern.slf4j.Slf4j;
import java.nio.charset.Charset;
/**
* 作用NioEventLoopGroup
* 服务端三个handler
*
* @author : qinyingjie
* @version : 2.2.0
......@@ -29,6 +30,7 @@ public class TestEventLoop_01_server {
.childHandler(new ChannelInitializer<NioSocketChannel>() {
@Override
protected void initChannel(NioSocketChannel ch) throws Exception {
//支持链式调用
ch.pipeline().addLast("handler1", new ChannelInboundHandlerAdapter() {
// ByteBuf
@Override
......@@ -38,13 +40,15 @@ public class TestEventLoop_01_server {
ctx.fireChannelRead(msg);
}
}).addLast(group, "handler2", new ChannelInboundHandlerAdapter() {
// handler2 是一个普通任务
// handler2 是一个普通任务,第一个参数是EventExecutorGroup,指定group执行
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
log.info("我是handler1,{}", buf.toString(Charset.defaultCharset()));
}
});
//添加解码器
ch.pipeline().addLast("handler0", new StringDecoder());
}
})
.bind(8080);
......
package com.kwan.shuyu.heima.netty_04_netty.c3_channel;
package com.kwan.shuyu.heima.netty_04_netty.c9_sync;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
......@@ -12,13 +12,13 @@ import java.net.InetSocketAddress;
/**
* ChannelFuture
* 客户端使用sync同步阻塞
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/23 16:43
*/
public class TestEventLoop_03_ChannelFuture {
public class TestEventLoop_02_client {
public static void main(String[] args) throws InterruptedException {
//1.启动类
final ChannelFuture channelFuture = new Bootstrap()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册