12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- package com.zhili.stationcontrol.gatewayserver.component;
- import com.zhili.stationcontrol.gatewayserver.handler.GatewayMessageDecoder;
- import com.zhili.stationcontrol.gatewayserver.handler.GatewayMessageEncoder;
- import com.zhili.stationcontrol.gatewayserver.handler.GatewayMessageHandler;
- import io.netty.bootstrap.ServerBootstrap;
- import io.netty.channel.ChannelFuture;
- import io.netty.channel.ChannelInitializer;
- import io.netty.channel.ChannelOption;
- import io.netty.channel.EventLoopGroup;
- import io.netty.channel.nio.NioEventLoopGroup;
- import io.netty.channel.socket.SocketChannel;
- import io.netty.channel.socket.nio.NioServerSocketChannel;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.scheduling.annotation.Async;
- import org.springframework.scheduling.annotation.EnableAsync;
- import org.springframework.stereotype.Component;
- /**
- * @author :HuangBin
- * @description:TODO
- * @date :2022/10/28 15:22
- */
- @EnableAsync
- @Component
- @Slf4j
- //192.168.3.177:9989
- public class GatewayServer {
- @Async
- public void run() {
- EventLoopGroup bossGroup = new NioEventLoopGroup(1);
- EventLoopGroup workerGroup = new NioEventLoopGroup();
- try {
- ServerBootstrap server = new ServerBootstrap();
- server.group(bossGroup, workerGroup)
- .channel(NioServerSocketChannel.class)
- .childOption(ChannelOption.SO_KEEPALIVE, true)
- .childOption(ChannelOption.TCP_NODELAY, true)
- .option(ChannelOption.SO_BACKLOG, 1024)
- .childHandler(new ChannelInitializer<SocketChannel>() {
- @Override
- protected void initChannel(SocketChannel socketChannel) throws Exception {
- socketChannel.pipeline()
- .addLast(new GatewayMessageDecoder())
- .addLast(new GatewayMessageEncoder())
- .addLast(new GatewayMessageHandler());
- }
- });
- ChannelFuture channelFuture = server.bind(9989).sync();
- channelFuture.channel().closeFuture().sync();
- } catch (InterruptedException e) {
- e.printStackTrace();
- } finally {
- bossGroup.shutdownGracefully();
- workerGroup.shutdownGracefully();
- }
- }
- }
|