LapiApplication.java 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package com.jpsoft.smart;
  2. import com.jpsoft.smart.lapi.handler.HeartReportHandler;
  3. import com.jpsoft.smart.lapi.handler.ParseHttpHandler;
  4. import com.jpsoft.smart.lapi.handler.PersonVerificationHandler;
  5. import io.netty.bootstrap.ServerBootstrap;
  6. import io.netty.channel.Channel;
  7. import io.netty.channel.ChannelFuture;
  8. import io.netty.channel.ChannelInitializer;
  9. import io.netty.channel.nio.NioEventLoopGroup;
  10. import io.netty.channel.socket.nio.NioServerSocketChannel;
  11. import io.netty.handler.codec.http.HttpObjectAggregator;
  12. import io.netty.handler.codec.http.HttpServerCodec;
  13. import lombok.extern.slf4j.Slf4j;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.beans.factory.annotation.Value;
  16. import org.springframework.boot.CommandLineRunner;
  17. import org.springframework.boot.SpringApplication;
  18. import javax.annotation.PreDestroy;
  19. @Slf4j
  20. //@SpringBootApplication
  21. //@EnableTransactionManagement
  22. //@MapperScan("com.jpsoft.smart.**.dao")
  23. public class LapiApplication implements CommandLineRunner {
  24. public static void main(String[] args) {
  25. SpringApplication.run(LapiApplication.class, args);
  26. }
  27. @Autowired
  28. private PersonVerificationHandler personVerificationHandler;
  29. @Autowired
  30. private HeartReportHandler heartReportHandler;
  31. @Value("${server.port}")
  32. private Integer port;
  33. private Channel serverChannel;
  34. @Override
  35. public void run(String... args) throws Exception {
  36. NioEventLoopGroup bossGroup = new NioEventLoopGroup();
  37. NioEventLoopGroup workerGroup = new NioEventLoopGroup();
  38. ServerBootstrap b = new ServerBootstrap();
  39. b.group(bossGroup,workerGroup)
  40. .channel(NioServerSocketChannel.class)
  41. .childHandler(new ChannelInitializer(){
  42. @Override
  43. protected void initChannel(Channel channel) throws Exception {
  44. // channel.pipeline().addLast(new TextDecodeHandler());
  45. // channel.pipeline().addLast(new HttpRequestDecoder());
  46. channel.pipeline().addLast(new HttpServerCodec());
  47. channel.pipeline().addLast(new HttpObjectAggregator(2048*10000));
  48. channel.pipeline().addLast(new ParseHttpHandler());
  49. channel.pipeline().addLast(heartReportHandler);
  50. channel.pipeline().addLast(personVerificationHandler);
  51. }
  52. });
  53. ChannelFuture channelFuture = b.bind("0.0.0.0",port);
  54. serverChannel = channelFuture.channel();
  55. channelFuture.addListener((future)-> {
  56. if(future.isSuccess()){
  57. log.warn(String.format("netty 已监听%d端口",port));
  58. }
  59. });
  60. }
  61. @PreDestroy
  62. public void stop(){
  63. if (serverChannel!=null && serverChannel.isOpen()){
  64. serverChannel.close();
  65. }
  66. }
  67. }