目录
目标
Netty版本
Netty官方API
客户端如何与服务器建立连接&连接成功后的操作方式
实现
如何处理客户端与服务器连接关闭后的操作
正确关闭连接的方式
方法一
方法二
目标
了解Netty如何处理客户端与服务器之间的连接与关闭问题。
Netty版本
<dependency>
<groupId></groupId>
<artifactId>netty-all</artifactId>
<version>4.1.</version>
</dependency>
Netty官方API
Netty API Reference (4.1.)/4.1/api/
客户端如何与服务器建立连接&连接成功后的操作方式
Bootstrap的connect方法是实现了客户端与服务器之间的连接。需要明确一点,客户端与服务器之间的连接不是main线程在做,而是由我们指定的EventLoop来做。如此看来,connect方法是异步非阻塞的,如果连接还没有建立好就无法获取Channel,因此,ChannelFuture有2种方法处理连接建立后的操作,其中:
- ChannelFuture的sync()方法,作用是阻塞main线程,等到连接建立好程序才向下运行。Channel由主线程获取。
- ChannelFuture用addListener()方法。异步调用回调对象的operationComplete方法。Channel由我们指定的EventLoop线程获取,本质是把ChannelFutureListener对象传递给EventLoopGroup线程,EventLoopGroup线程建立好连接以后调用operationComplete方法。
实现
package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import .slf4j.Slf4j;
import ;
@Slf4j
public class NettyClient {
/**
* 建立连接后的操作:
* 方法一:用sync()方法。
*/
public void fun(ChannelFuture channelFuture) throws InterruptedException {
/**
* 如果连接还没有建立好就无法获取Channel,因此,有了sync()方法,
* 作用是阻塞等到连接建立好程序才向下运行。
*/
();
Channel channel = ();
//打印main线程
("channel==========={}",channel);
//向服务器发送数据
("Hello world!");
}
/**
* 建立连接后的操作:
* 方法二:用addListener()方法。异步调用回调对象的operationComplete方法。
*/
public void fun2(ChannelFuture channelFuture) throws InterruptedException {
//把ChannelFutureListener对象传递给EventLoopGroup线程,EventLoopGroup线程建立好连接以后调用operationComplete方法。
(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture channelFuture) throws Exception {
Channel channel = ();
//打印nioEventLoopGroup线程
("channel==========={}",channel);
//向服务器发送数据
("Hello world!");
}
});
}
public static void main(String[] args) throws InterruptedException {
//启动Netty客户端
Bootstrap bootstrap = new Bootstrap();
//选择EventLoop
(new NioEventLoopGroup());
//选择客户端Channel实现,Channel是数据的传输通道。
();
//添加处理器,
(new ChannelInitializer<NioSocketChannel>() {
//连接建立后初始化Channel
@Override
protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
//把字符串编码成ByteBuf
().addLast(new StringEncoder(("UTF-8")));
}
});
/**
* Netty客户端和服务端建立连接是由EventLoopGroup线程负责的,而不是由main线程负责的。
* 因此,这个建立连接的过程是异步非阻塞操作。
*/
ChannelFuture channelFuture = ("localhost", 8999);
//new NettyClient().fun( channelFuture);
new NettyClient().fun2( channelFuture);
}
}
如何处理客户端与服务器连接关闭后的操作
Channel的close方法用来关闭客户端与服务器之间地连接,但是close方法是个异步方法,如果需要关闭连接才处理后续的业务则要考虑不同线程的执行顺序问题。
正确关闭连接的方式
方法一
package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class RespClient {
public static void main(String[] args) throws InterruptedException {
NioEventLoopGroup nioEventLoopGroup = new NioEventLoopGroup();
try {
//启动Netty客户端
Bootstrap bootstrap = new Bootstrap();
//选择EventLoop
(nioEventLoopGroup);
//选择客户端Channel实现,Channel是数据的传输通道。
();
//添加处理器,
(new ChannelInitializer<NioSocketChannel>() {
//连接建立后初始化Channel
@Override
protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
().addLast(new ChannelInboundHandlerAdapter() {
@Override
public void channelActive(ChannelHandlerContext ctx) {
}
//接收响应
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
}
});
}
});
ChannelFuture channelFuture = ("172.19.216.135", 6379).sync();
().close();
().closeFuture().sync();
}finally{
("连接关闭了。");
();
}
}
}
方法二
package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import .slf4j.Slf4j;
@Slf4j
public class NettyClient {
public static void main(String[] args) throws InterruptedException {
NioEventLoopGroup nioEventLoopGroup = new NioEventLoopGroup();
//启动Netty客户端
Bootstrap bootstrap = new Bootstrap();
//选择EventLoop
(nioEventLoopGroup);
//选择客户端Channel实现,Channel是数据的传输通道。
();
//添加处理器,
(new ChannelInitializer<NioSocketChannel>() {
//连接建立后初始化Channel
@Override
protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
//把字符串编码成ByteBuf
().addLast(new StringEncoder());
}
});
ChannelFuture channelFuture = ("localhost", 8999);
();
Channel channel = ();
("{}"+channel);
("Hello World!") ;
//close()是异步方法,如果该方法下面没有阻塞,则无法保证线程的执行顺序。
();
channelFuture= ();
//关闭连接的那个线程负责执行该方法。
(new ChannelFutureListener() {
//关闭连接的那个线程负责执行该方法。
@Override
public void operationComplete(ChannelFuture channelFuture) throws Exception {
("连接关闭了。");
/**
* 优雅地关闭nioEventLoopGroup,即:
* 拒绝接收新的任务,完成现有的任务。
*/
();
}
});
}
}