netty

本文介绍了一个基于Netty的JT808 GPS服务器实现方法。该服务器使用NioEventLoopGroup处理I/O操作,并通过ServerBootstrap进行配置。文章展示了如何设置服务器监听端口、配置通道选项以及添加消息编解码器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import com.yd.lbs.gps.acceptor.util.PropertiesUtil;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class Jt808GpsServer {

    private static Logger logger = LoggerFactory.getLogger(Jt808GpsServer.class);

    public static void main(String[] args) throws InterruptedException {
        int port = PropertiesUtil.GPS_PORT;
        if (args != null && args.length > 0) {
            try {
                port = Integer.parseInt(args[0]);
            } catch (NumberFormatException e) {
            }
        }
        logger.info("Jt808 gps server port is:{} ", port);
        new Jt808GpsServer().bind(port);
    }

    public void bind(int port) throws InterruptedException {

        //EventLoopGroup 类似线程组,boss用于监听是否有tcp链接过来;worker用于建立链接并处理
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        //如果不设置参数值,则默认取CPU核心数*2
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG, 100)
                    .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
                    .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
                    .handler(new LoggingHandler(LogLevel.INFO))
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        public void initChannel(SocketChannel ch) {
                            ChannelPipeline pipeline = ch.pipeline();
                            pipeline.addLast("decode", new Jt808MessageDecoder());
                            pipeline.addLast("encode", new Jt808MessageEncoder());
                            pipeline.addLast(new Jt808GpsServerHandler());

                        }
                    });
            //绑定端口,同步等待成功
            ChannelFuture f = b.bind(port).sync();
            //等待服务器端监听端口关闭
            f.channel().closeFuture().sync();
            logger.info("Jt808 gps server is started up...");
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}

 

转载于:https://www.cnblogs.com/tonggc1668/p/6541953.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值