fix:group

上级 92778f13
package com.kwan.shuyu.heima.netty_04_netty;
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;
package com.kwan.shuyu.heima.netty_04_netty.c1_basic;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelHandlerContext;
......
package com.kwan.shuyu.heima.netty_04_netty.c2_组件;
import io.netty.channel.DefaultEventLoopGroup;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import lombok.extern.slf4j.Slf4j;
/**
* 测试EventLoop
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/23 10:51
*/
@Slf4j
public class TestEventLoop_01_EventLoopGroup {
public static void main(String[] args) {
//创建事件循环组
EventLoopGroup group1 = new NioEventLoopGroup(2);//io 本件,普通任务,定时任务
EventLoopGroup group2 = new DefaultEventLoopGroup();// 普通任务,定时任务
System.out.println(group1.next());
System.out.println(group1.next());
System.out.println(group1.next());
System.out.println(group1.next());
System.out.println(group1.next());
System.out.println(group1.next());
System.out.println(group1.next());
}
}
package com.kwan.shuyu.heima.netty_04_netty.c2_组件;
import io.netty.channel.DefaultEventLoopGroup;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import lombok.extern.slf4j.Slf4j;
/**
* 测试EventLoop
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/23 10:51
*/
@Slf4j
public class TestEventLoop_02_submit {
public static void main(String[] args) {
//创建事件循环组
EventLoopGroup group1 = new NioEventLoopGroup(2);//io 本件,普通任务,定时任务
EventLoopGroup group2 = new DefaultEventLoopGroup();// 普通任务,定时任务
System.out.println(group1.next());
//提交普通任务
group1.next().submit(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("ok");
});
log.info("main");
}
}
package com.kwan.shuyu.heima.netty_04_netty.c2_组件;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.TimeUnit;
/**
* 测试EventLoop
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/23 10:51
*/
@Slf4j
public class TestEventLoop_03_scheduleAtFixedRate {
public static void main(String[] args) {
//创建事件循环组
EventLoopGroup group1 = new NioEventLoopGroup(2);//io 本件,普通任务,定时任务
System.out.println(group1.next());
//执行定时任务
group1.next().scheduleAtFixedRate(() -> {
log.info("ok");
}, 0, 1, TimeUnit.SECONDS);
log.info("main");
}
}
package com.kwan.shuyu.heima.netty_04_netty.c2_组件;
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;
public class TestEventLoop_04_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_组件;
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.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;
/**
* 测试EventLoop
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/23 10:51
*/
@Slf4j
public class TestEventLoop_04_server {
public static void main(String[] args) {
new ServerBootstrap()
.group(new NioEventLoopGroup(2))
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<NioSocketChannel>() {
@Override
protected void initChannel(NioSocketChannel ch) throws Exception {
ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
// ByteBuf
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
log.info(buf.toString(Charset.defaultCharset()));
}
});
}
})
.bind(8080);
}
}
package com.kwan.shuyu.heima.netty_04_netty.c2_组件;
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 lombok.extern.slf4j.Slf4j;
import java.nio.charset.Charset;
/**
* 测试EventLoop
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/23 10:51
*/
@Slf4j
public class TestEventLoop_05_group {
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(buf.toString(Charset.defaultCharset()));
ctx.fireChannelRead(msg);
}
}).addLast(group, "handler2", new ChannelInboundHandlerAdapter() {
// handler2 是一个普通任务
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
log.info(buf.toString(Charset.defaultCharset()));
}
});
}
})
.bind(8080);
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册