当前位置: 首页 > news >正文

java-netty知识点笔记和注意事项

  1. 如何获取ctx的id
    使用ctx.ctx.toString()就可以了
    public void channelRead(ChannelHandlerContext ctx, Object msg) {//传来的消息包装成字节缓冲区String byteBuf = (String) msg;
//        ByteBuf byteBuf = (ByteBuf) msg;//Netty提供了字节缓冲区的toString方法,并且可以设置参数为编码格式:CharsetUtil.UTF_8System.out.println("客户端读取服务返回的数据:" + byteBuf+" and ctx is "+ctx.toString());ctxOut=ctx;RESPSTR=byteBuf;}

结果如下

客户端读取服务返回的数据:0 and ctx is ChannelHandlerContext(NettyClientHandlerInner#0, [id: 0x59510140, L:/127.0.0.1:54769 - R:/127.0.0.1:1003])
  1. 阻塞执行问题
    在启动了连接server后,如果是封装到一个方法里,程序是不能往下执行的,如下
    如果把nettyClientHandlerInner.sendMSG(“write:0”);放在了client.connectToServer(nettyClientHandlerInner);后面,程序是执行到connectToServer就不会往下执行了
            NettyClientHandlerInner nettyClientHandlerInner=new NettyClientHandlerInner();new Thread(new Runnable() {@Overridepublic void run() {nettyClientQA client = new nettyClientQA();nettyClientHandlerInner.sendMSG("write:0");client.connectToServer(nettyClientHandlerInner);}}).start();

上面代码执行到sendMSG的时候,会报错,因为还没有通道激活初始化,因此,解决暂时方法是,应该还有更好的解决方法,等后面想到了再更新

            NettyClientHandlerInner nettyClientHandlerInner=new NettyClientHandlerInner();nettyClientQA client = new nettyClientQA();new Thread(new Runnable() {@Overridepublic void run() {new Thread(new Runnable() {@Overridepublic void run() {try {Thread.sleep(5000);} catch (InterruptedException e) {e.printStackTrace();}nettyClientHandlerInner.sendMSG("write:0");}}).start();client.connectToServer(nettyClientHandlerInner);}}).start();
  1. 如何捕获client handler的异常
    加个内部类即可
package sample.appfunction.netty;import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.util.CharsetUtil;import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;public class nettyClientQA {public  void connectToServer(NettyClientHandlerInner nettyClientHandler){EventLoopGroup workerGroup = new NioEventLoopGroup();try {Bootstrap b = new Bootstrap(); // (1)b.group(workerGroup); // (2)b.channel(NioSocketChannel.class); // (3)b.option(ChannelOption.SO_KEEPALIVE, true); // (4)b.handler(new ChannelInitializer<SocketChannel>() {@Overridepublic void initChannel(SocketChannel ch) throws Exception {ch.pipeline().addLast(new LineBasedFrameDecoder(1024));ch.pipeline().addLast(new StringDecoder());ch.pipeline().addLast(nettyClientHandler);ch.pipeline().addLast(new ExceptionHandlingChannelHandler());}});// Start the client.ChannelFuture f = b.connect("127.0.0.1", 1003).sync(); // (5)f.addListener(new ChannelFutureListener() {@Overridepublic void operationComplete(ChannelFuture future) {if (future.isSuccess()) {// 连接成功,无需重连System.out.println("connect to server success");} else {// 连接失败,进行重连System.out.println("connect to server failed,try it again");future.channel().eventLoop().schedule(new Runnable() {@Overridepublic void run() {b.connect(); // 重新连接服务端}}, 1, TimeUnit.SECONDS); // 1秒后进行重连}}});// Wait until the connection is closed.f.channel().closeFuture().sync();} catch (InterruptedException e) {e.printStackTrace();} finally {workerGroup.shutdownGracefully();}}public class ExceptionHandlingChannelHandler extends ChannelInboundHandlerAdapter {@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {cause.printStackTrace(); // 打印异常堆栈跟踪System.out.println("发生异常");}}
}
  1. 在server handler的异常如何捕捉
    @ChannelHandler.Sharablepublic  class NettyServerHandler extends ChannelInboundHandlerAdapter { // (1)@Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) throws IOException { // (2)String byteBuf = (String) msg;pubMSG=byteBuf;
//            System.out.println("客户端发来的消息是:" + byteBuf);System.out.println("收到连接的原信息 "+pubMSG);if(byteBuf.contains("write:")){currentValue=Integer.parseInt(byteBuf.split(":")[1]);System.out.println("收到写入请求 "+currentValue);}if(byteBuf.contains("read")){ctx.writeAndFlush(Unpooled.copiedBuffer(currentValue+"\r\n", CharsetUtil.UTF_8));System.out.println("收到读取请求 "+currentValue);}}//数据读取完毕事件public void channelReadComplete(ChannelHandlerContext ctx) throws IOException, InterruptedException {//数据读取完毕,将信息包装成一个Buffer传递给下一个Handler,Unpooled.copiedBuffer会返回一个Buffer//调用的是事件处理器的上下文对象的writeAndFlush方法//意思就是说将  你好  传递给了下一个handler
//            ctx.writeAndFlush(Unpooled.copiedBuffer("服务端已经读取消息完成!"+pubMSG+"\r\n", CharsetUtil.UTF_8));}@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { // (4)// Close the connection when an exception is raised.
//            cause.printStackTrace();Channel channel = ctx.channel();if(channel.isActive())ctx.close();System.out.println(cause.getCause());System.out.println(cause.getMessage());}}```
http://www.lryc.cn/news/247929.html

相关文章:

  • 英伟达不同系列GPU介绍
  • C语言——I /深入理解指针(二)
  • MySQL使用函数和存储过程实现:向数据表快速插入大量测试数据
  • 力扣labuladong——一刷day59
  • 接口性能测试 —— Jmeter并发与持续性压测
  • redis报错3
  • Proteus的网络标号与总线
  • 4、stable diffusion
  • LeetCode51. N-Queens
  • 前端vue3——html2canvas给网站截图生成宣传海报
  • C语言实现串的部分算法
  • UE5、CesiumForUnreal实现加载GeoJson绘制多面(MultiPolygon)功能(支持点选高亮)
  • pandas教程:USDA Food Database USDA食品数据库
  • 0基础学习VR全景平台篇第122篇:VR视频剪辑和输出 - PR软件教程
  • ucharts中,当数据为0时,不显示
  • React函数组件渲染两次
  • 人工智能 - 图像分类:发展历史、技术全解与实战
  • go标准库
  • 【Web安全】拿到phpMyAdmin如何获取权限
  • Python与GPU编程快速入门(一)
  • C语言--每日选择题--Day29
  • ESP32:物联网时代的神器
  • docker和docker-compose生产的容器,不在同一个网段,解决方式
  • 基于JavaWeb+SSM+Vue校园综合服务小程序系统的设计和实现
  • 私域运营:资源盘点及争取策略
  • 图书管理系统源码,图书管理系统开发,图书借阅系统源码整体功能演示
  • (C++)字符串相乘
  • 1992-2021年区县经过矫正的夜间灯光数据(GNLD、VIIRS)
  • RK3568笔记六:基于Yolov8的训练及部署
  • 【活动回顾】sCrypt在柏林B2029开发者周