首页 > 代码库 > netty
netty
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(); } } }
netty
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。