Netty常用類介紹
- Bootstrap類,Bootstrap是Netty應用程序的啓動類,我們可以通過其指定採用某種Channel,以及處理IO操作的EventLoopgroup,同時還可以指定我們需要操作的handler.
- 一個EventLoop可以爲多個Channel服務,EventLoopGroup會包含多個EventLoop。
- ChannelInitializer 類中提供了ChannelPipeLine,我們可以通過這個類進行攔截,處理某個時間,ChannelPipeLine還可以指定我們對傳輸的信息進行那種編碼,解碼,以及怎樣處理各種事件
- Handler類,我們通常是繼承ChannelInboundHandlerAdapter來重寫channelActive(連接成功調用),channelRead(當消息來的時候調用),exceptionCaught(發生異常時調用)等方法。
- Future和ChannelFutures。可以通過註冊一個監聽,當操作執行成功或失敗時監聽會自動觸發。
netty jar包下載
簡單的列子
客戶端
public class NettyClient {
private String address;
private int port;
public NettyClient(String address,int port){
this.address=address;
this.port=port;
}
public void start(){
EventLoopGroup group=new NioEventLoopGroup();
Bootstrap bootstrap=new Bootstrap().group(group).channel(NioSocketChannel.class).handler(new ClientChannelInitializer());
try {
Channel channel=bootstrap.connect(address,port).sync().channel();
while(true){
String str=new Scanner(System.in).next();
channel.writeAndFlush(str);
}
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
group.shutdownGracefully();
}
}
ChannelInitializer類的實現(主要是指定連接的編碼,解碼處理事件的handler)
public class ClientChannelInitializer extends ChannelInitializer<SocketChannel> {
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline=socketChannel.pipeline();
pipeline.addLast("encoder",new StringEncoder());
pipeline.addLast("decoder",new StringDecoder());
pipeline.addLast("handler",new ClientHandler());
}
}
Handler類的實現(主要定義一些事件的處理)
public class ClientHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("客戶端連接成功");
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
System.out.println("客戶端斷開連接");
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println(msg.toString());
}
}
服務端
public class NettyServer {
private int port;
public NettyServer(int port){
this.port=port;
}
public void startServer(){
EventLoopGroup bossGroup=new NioEventLoopGroup();
EventLoopGroup workGroup=new NioEventLoopGroup();
ServerBootstrap serverBootstrap=new ServerBootstrap().group(bossGroup,workGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ServerChannelInitializer());
try {
ChannelFuture future=serverBootstrap.bind(port).sync();
future.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
服務端的ChannelInitializer類
public class ServerChannelInitializer extends ChannelInitializer<SocketChannel> {
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline=socketChannel.pipeline();
pipeline.addLast("encoder",new StringEncoder());
pipeline.addLast("decoder",new StringDecoder());
pipeline.addLast("handler",new ServerHandler());
}
}
服務端的Handler類
public class ServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println(ctx.channel().remoteAddress()+" "+msg.toString());
ctx.write("已收到消息");
ctx.flush();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
System.out.println(cause.toString());
ctx.close();
}
}
我也是在學習,如果有錯誤的,請提出來,一起進步,一起學習