【Java】SpringBoot+Mybatis+Netty 4+Redis+Mysql监听处理监测设备数据

1 篇文章 0 订阅

**

需求:监听指定多个端口,接收硬件消息,并向硬件发送问询帧。

**
问题记录:
1、netty使用自动注对象为空
解决办法:

@ChannelHandler.Sharable
public class NettyServerHandler extends ChannelInboundHandlerAdapter {

    @Autowired
    private RedisUtil redisUtil;

    @PostConstruct
    public void init() {
        nettyServerHandler = this;
    }

	/**
     * 调用方式:nettyServerHandler.redisUtil
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        boolean b = nettyServerHandler.redisUtil.hasKey("@emp");
    }

2、监听多个端口
解决办法:

public void run() throws Exception {
        EventLoopGroup leader = new NioEventLoopGroup();
        EventLoopGroup coder = new NioEventLoopGroup();
        try {
            // 服务端启动引导器
            ServerBootstrap server = new ServerBootstrap();

            server
                    //把事件处理线程池添加进启动引导器
                    .group(leader, coder)
                    //设置通道的建立方式
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        //构造一个通道管道流水线
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            // 此处添加服务端的通道处理器,不同的端口用不同的类处理
                            int localPort = socketChannel.localAddress().getPort();
                            if (localPort == 7760) {
                                socketChannel.pipeline().addLast(new NettyServerHandler());
                            } else if (localPort == 7761) {
                                socketChannel.pipeline().addLast(new NettyServerHandler2());
                            }
                        }
                    })
                    .option(ChannelOption.SO_BACKLOG, 128)
                    .childOption(ChannelOption.SO_KEEPALIVE, true);
            // 服务端绑定端口并且开始接收进来的连接请求
            ChannelFuture channelFuture = server.bind(port).sync();
            ChannelFuture channelFuture2 = server.bind(port2).sync();
            //通过回调只关闭自己监听的channel
            channelFuture.channel().closeFuture().addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {       
                   
                    future.channel().close();
                }
            });
            channelFuture2.channel().closeFuture().addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    future.channel().close();
                }
            });
            // 查看一下操作是不是成功结束了
            if (channelFuture.isSuccess()) {
                //如果没有成功结束就处理一些事情,结束了就执行关闭服务端等操作
                System.out.println("服务端启动成功,监听端口是:" + port);
            }
            if (channelFuture2.isSuccess()) {
                //如果没有成功结束就处理一些事情,结束了就执行关闭服务端等操作
                System.out.println("服务端启动成功,监听端口是:" + port2);
            }
        } finally {
            // 关闭事件处理组
            // leader.shutdownGracefully();
            // coder.shutdownGracefully();
            // System.out.println("服务端已关闭!");
        }

    }

3、远程主机强迫关闭了一个现有的连接。
这个问题没解决,搜索看大家说是因为在读写操作的时候断开连接了,因为我使用redis,这个问题影响不大。

欢迎讨论。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值