Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Kwan的解忧杂货铺@新空间代码工作室
netty-demo
提交
d4a59a72
N
netty-demo
项目概览
Kwan的解忧杂货铺@新空间代码工作室
/
netty-demo
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
N
netty-demo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
d4a59a72
编写于
4月 26, 2023
作者:
Kwan的解忧杂货铺@新空间代码工作室
🐭
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix:详细分类
上级
7ac95a63
变更
21
隐藏空白更改
内联
并排
Showing
21 changed file
with
422 addition
and
28 deletion
+422
-28
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c10_listener/TestEventLoop_01_server.java
.../netty_04_netty/c10_listener/TestEventLoop_01_server.java
+56
-0
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c10_listener/TestEventLoop_02_client.java
.../netty_04_netty/c10_listener/TestEventLoop_02_client.java
+4
-3
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c11_Scanner/TestEventLoop_01_server.java
...a/netty_04_netty/c11_Scanner/TestEventLoop_01_server.java
+56
-0
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c11_Scanner/TestEventLoop_02_client.java
...a/netty_04_netty/c11_Scanner/TestEventLoop_02_client.java
+3
-3
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c1_basic/HelloClient.java
...kwan/shuyu/heima/netty_04_netty/c1_basic/HelloClient.java
+1
-1
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c1_basic/HelloServer.java
...kwan/shuyu/heima/netty_04_netty/c1_basic/HelloServer.java
+2
-2
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c2_ByteBuf/TestEventLoop_01_Client.java
...ma/netty_04_netty/c2_ByteBuf/TestEventLoop_01_Client.java
+2
-2
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c2_ByteBuf/TestEventLoop_02_server.java
...ma/netty_04_netty/c2_ByteBuf/TestEventLoop_02_server.java
+3
-3
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c3_fireChannelRead/TestEventLoop_01_Client.java
..._04_netty/c3_fireChannelRead/TestEventLoop_01_Client.java
+42
-0
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c3_fireChannelRead/TestEventLoop_02_client.java
..._04_netty/c3_fireChannelRead/TestEventLoop_02_client.java
+3
-3
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c4_EventLoopGroup/TestEventLoop_01_EventLoopGroup.java
...ty/c4_EventLoopGroup/TestEventLoop_01_EventLoopGroup.java
+1
-1
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c4_EventLoopGroup/TestEventLoop_02_submit.java
...y_04_netty/c4_EventLoopGroup/TestEventLoop_02_submit.java
+1
-1
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c5_schedule/TestEventLoop_03_scheduleAtFixedRate.java
...tty/c5_schedule/TestEventLoop_03_scheduleAtFixedRate.java
+1
-1
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c6_codec/TestEventLoop_01_server.java
...eima/netty_04_netty/c6_codec/TestEventLoop_01_server.java
+38
-0
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c6_codec/TestEventLoop_02_client.java
...eima/netty_04_netty/c6_codec/TestEventLoop_02_client.java
+41
-0
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c7_channel/TestEventLoop_01_server.java
...ma/netty_04_netty/c7_channel/TestEventLoop_01_server.java
+56
-0
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c7_channel/TestEventLoop_02_Client.java
...ma/netty_04_netty/c7_channel/TestEventLoop_02_Client.java
+2
-2
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c8_log/TestEventLoop_01_server.java
.../heima/netty_04_netty/c8_log/TestEventLoop_01_server.java
+56
-0
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c8_log/TestEventLoop_02_client.java
.../heima/netty_04_netty/c8_log/TestEventLoop_02_client.java
+44
-0
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c9_sync/TestEventLoop_01_server.java
...heima/netty_04_netty/c9_sync/TestEventLoop_01_server.java
+7
-3
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c9_sync/TestEventLoop_02_client.java
...heima/netty_04_netty/c9_sync/TestEventLoop_02_client.java
+3
-3
未找到文件。
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c10_listener/TestEventLoop_01_server.java
0 → 100644
浏览文件 @
d4a59a72
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
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c
3_channel/TestEventLoop_04_ChannelFuture
.java
→
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c
10_listener/TestEventLoop_02_client
.java
浏览文件 @
d4a59a72
package
com.kwan.shuyu.heima.netty_04_netty.c
3_channel
;
package
com.kwan.shuyu.heima.netty_04_netty.c
10_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_0
4_ChannelFuture
{
public
class
TestEventLoop_0
2_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
();
...
...
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c11_Scanner/TestEventLoop_01_server.java
0 → 100644
浏览文件 @
d4a59a72
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
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c
3_channel/TestEventLoop_05_ChannelFuture
.java
→
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c
11_Scanner/TestEventLoop_02_client
.java
浏览文件 @
d4a59a72
package
com.kwan.shuyu.heima.netty_04_netty.c
3_channel
;
package
com.kwan.shuyu.heima.netty_04_netty.c
11_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_0
5_ChannelFuture
{
public
class
TestEventLoop_0
2_client
{
public
static
void
main
(
String
[]
args
)
throws
InterruptedException
{
//1.启动类
final
ChannelFuture
channelFuture
=
new
Bootstrap
()
...
...
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c1_
基础
/HelloClient.java
→
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c1_
basic
/HelloClient.java
浏览文件 @
d4a59a72
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
;
...
...
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c1_
基础
/HelloServer.java
→
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c1_
basic
/HelloServer.java
浏览文件 @
d4a59a72
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
...
...
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c2_
组件/TestEventLoop_04
_Client.java
→
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c2_
ByteBuf/TestEventLoop_01
_Client.java
浏览文件 @
d4a59a72
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_0
4
_Client
{
public
class
TestEventLoop_0
1
_Client
{
public
static
void
main
(
String
[]
args
)
throws
InterruptedException
{
//1.启动类
new
Bootstrap
()
...
...
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c2_
组件/TestEventLoop_04
_server.java
→
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c2_
ByteBuf/TestEventLoop_02
_server.java
浏览文件 @
d4a59a72
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_0
4
_server
{
public
class
TestEventLoop_0
2
_server
{
public
static
void
main
(
String
[]
args
)
{
new
ServerBootstrap
()
...
...
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c3_fireChannelRead/TestEventLoop_01_Client.java
0 → 100644
浏览文件 @
d4a59a72
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
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c
2_组件/TestEventLoop_05_group
.java
→
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c
3_fireChannelRead/TestEventLoop_02_client
.java
浏览文件 @
d4a59a72
package
com.kwan.shuyu.heima.netty_04_netty.c
2_
组件
;
package
com.kwan.shuyu.heima.netty_04_netty.c
3_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_0
5_group
{
public
class
TestEventLoop_0
2_client
{
public
static
void
main
(
String
[]
args
)
{
//普通任务
final
EventLoopGroup
group
=
new
DefaultEventLoopGroup
();
...
...
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c
2_组件
/TestEventLoop_01_EventLoopGroup.java
→
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c
4_EventLoopGroup
/TestEventLoop_01_EventLoopGroup.java
浏览文件 @
d4a59a72
package
com.kwan.shuyu.heima.netty_04_netty.c
2_
组件
;
package
com.kwan.shuyu.heima.netty_04_netty.c
4_EventLoopGroup
;
import
io.netty.channel.DefaultEventLoopGroup
;
...
...
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c
2_组件
/TestEventLoop_02_submit.java
→
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c
4_EventLoopGroup
/TestEventLoop_02_submit.java
浏览文件 @
d4a59a72
package
com.kwan.shuyu.heima.netty_04_netty.c
2_
组件
;
package
com.kwan.shuyu.heima.netty_04_netty.c
4_EventLoopGroup
;
import
io.netty.channel.DefaultEventLoopGroup
;
...
...
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c
2_组件
/TestEventLoop_03_scheduleAtFixedRate.java
→
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c
5_schedule
/TestEventLoop_03_scheduleAtFixedRate.java
浏览文件 @
d4a59a72
package
com.kwan.shuyu.heima.netty_04_netty.c
2_
组件
;
package
com.kwan.shuyu.heima.netty_04_netty.c
5_schedule
;
import
io.netty.channel.EventLoopGroup
;
...
...
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c6_codec/TestEventLoop_01_server.java
0 → 100644
浏览文件 @
d4a59a72
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
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c6_codec/TestEventLoop_02_client.java
0 → 100644
浏览文件 @
d4a59a72
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
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c7_channel/TestEventLoop_01_server.java
0 → 100644
浏览文件 @
d4a59a72
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
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c
3
_channel/TestEventLoop_02_Client.java
→
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c
7
_channel/TestEventLoop_02_Client.java
浏览文件 @
d4a59a72
package
com.kwan.shuyu.heima.netty_04_netty.c
3
_channel
;
package
com.kwan.shuyu.heima.netty_04_netty.c
7
_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
...
...
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c8_log/TestEventLoop_01_server.java
0 → 100644
浏览文件 @
d4a59a72
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
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c8_log/TestEventLoop_02_client.java
0 → 100644
浏览文件 @
d4a59a72
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
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c
3_channel
/TestEventLoop_01_server.java
→
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c
9_sync
/TestEventLoop_01_server.java
浏览文件 @
d4a59a72
package
com.kwan.shuyu.heima.netty_04_netty.c
3_channel
;
package
com.kwan.shuyu.heima.netty_04_netty.c
9_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
);
...
...
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c
3_channel/TestEventLoop_03_ChannelFuture
.java
→
src/main/java/com/kwan/shuyu/heima/netty_04_netty/c
9_sync/TestEventLoop_02_client
.java
浏览文件 @
d4a59a72
package
com.kwan.shuyu.heima.netty_04_netty.c
3_channel
;
package
com.kwan.shuyu.heima.netty_04_netty.c
9_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_0
3_ChannelFuture
{
public
class
TestEventLoop_0
2_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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录