zoukankan      html  css  js  c++  java
  • SpringBoot 集成Netty实现UDP Server

    注:ApplicationRunner 接口是在容器启动成功后的最后一步回调(类似开机自启动)。

    UDPServer

    package com.vmware.vCenterEvent.netty;
    
    import io.netty.bootstrap.Bootstrap;
    import io.netty.channel.*;
    import io.netty.channel.nio.NioEventLoopGroup;
    import io.netty.channel.socket.nio.NioDatagramChannel;
    import lombok.extern.log4j.Log4j2;
    import org.springframework.boot.ApplicationArguments;
    import org.springframework.boot.ApplicationRunner;
    import org.springframework.stereotype.Component;
    
    @Log4j2
    @Component
    public class UdpServer implements ApplicationRunner {
    
        private final Bootstrap bootstrap;
    
        private final NioEventLoopGroup group;
    
        private Channel channel;
    
        private void Start() throws InterruptedException {
            try {
                channel = bootstrap.bind("0.0.0.0", 8888).sync().channel();
                System.out.println("UdpServer start success");
                channel.closeFuture().await();
            } finally {
                group.shutdownGracefully();
            }
        }
    
        private static final class NettyUdpServerHolder {
            static final NettyUdpServer INSTANCE = new NettyUdpServer();
        }
    
        public static NettyUdpServer getInstance() {
            return NettyUdpServerHolder.INSTANCE;
        }
    
        private NettyUdpServer() {
            group = new NioEventLoopGroup();
            bootstrap = new Bootstrap();
            bootstrap.group(group)
                    .channel(NioDatagramChannel.class)
                    .option(ChannelOption.SO_BROADCAST, true)
                    .option(ChannelOption.SO_RCVBUF, 1024 * 1024 * 100)
                    .handler(new ChannelInitializer<Channel>() {
                        @Override
                        protected void initChannel(Channel channel) throws Exception {
                            ChannelPipeline pipeline = channel.pipeline();
                            pipeline.addLast(new NettyUdpServerHandler());
                        }
                    });
        }
    
        @Override
        public void run(ApplicationArguments args) throws Exception {
            NettyUdpServer.getInstance().Start();
        }
    }

    UDPServerHandler

    import com.vmware.vCenterEvent.domain.Syslog;
    import io.netty.channel.ChannelHandlerContext;
    import io.netty.channel.SimpleChannelInboundHandler;
    import io.netty.channel.socket.DatagramPacket;
    import io.netty.util.CharsetUtil;
    import lombok.extern.log4j.Log4j2;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.jms.core.JmsMessagingTemplate;
    import org.springframework.stereotype.Component;
    
    import javax.annotation.PostConstruct;
    
    @Log4j2
    @Component
    public class NettyUdpServerHandler extends SimpleChannelInboundHandler<DatagramPacket> {
    
        public static NettyUdpServerHandler nettyUdpServerHandler;
    
        @Autowired
        private JmsMessagingTemplate jmsMessagingTemplate;
    
        public NettyUdpServerHandler(){
        }
    
        @PostConstruct
        public void init(){
            nettyUdpServerHandler = this;
            nettyUdpServerHandler.jmsMessagingTemplate = this.jmsMessagingTemplate;
        }
    
        @Override
        protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) {
            // log.info("开始接收数据");
            String msgString = msg.content().toString(CharsetUtil.UTF_8);

         // 将接收到数据放入ActiveMQ队列中 nettyUdpServerHandler.jmsMessagingTemplate.convertAndSend("mq", msgString); } }
  • 相关阅读:
    【CF932F】Escape Through Leaf 启发式合并set维护凸包
    【CF932E】Team Work/【BZOJ5093】图的价值 数学+NTT
    【CF917D】Stranger Trees 树形DP+Prufer序列
    【CF914G】Sum the Fibonacci 快速??变换模板
    【CF772D】Varying Kibibits FWT
    【CF802C】Heidi and Library (hard) 费用流
    【CF802L】Send the Fool Further! (hard) 高斯消元
    【CF809D】Hitchhiking in the Baltic States Splay
    【CF815D】Karen and Cards 单调栈+扫描线
    【CF819D】Mister B and Astronomers EXGCD
  • 原文地址:https://www.cnblogs.com/vincenshen/p/10759422.html
Copyright © 2011-2022 走看看