Advertisement
Guest User

Untitled

a guest
Dec 21st, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. import io.netty.bootstrap.ServerBootstrap;
  2. import io.netty.channel.Channel;
  3. import io.netty.channel.ChannelFuture;
  4. import io.netty.channel.ChannelFutureListener;
  5. import io.netty.channel.ChannelHandlerContext;
  6. import io.netty.channel.ChannelInitializer;
  7. import io.netty.channel.ChannelOption;
  8. import io.netty.channel.ChannelPipeline;
  9. import io.netty.channel.EventLoopGroup;
  10. import io.netty.channel.nio.NioEventLoopGroup;
  11. import io.netty.channel.socket.SocketChannel;
  12. import io.netty.channel.socket.nio.NioServerSocketChannel;
  13. import io.netty.handler.codec.LineBasedFrameDecoder;
  14. import io.netty.handler.codec.MessageToMessageEncoder;
  15. import io.netty.handler.codec.string.StringDecoder;
  16. import io.netty.handler.codec.string.StringEncoder;
  17. import io.netty.util.CharsetUtil;
  18. import org.slf4j.Logger;
  19. import org.slf4j.LoggerFactory;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.boot.context.properties.ConfigurationProperties;
  22. import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
  23. import org.springframework.stereotype.Service;
  24.  
  25. import javax.annotation.PostConstruct;
  26. import javax.annotation.PreDestroy;
  27. import java.util.List;
  28.  
  29. @Service
  30. @ConfigurationProperties(prefix="server")
  31. public class SpringBootNettyServer {
  32. private static Logger LOG = LoggerFactory.getLogger(SpringBootNettyServer.class);
  33.  
  34. private int backlogSize = 128;
  35. private int port = 143;
  36.  
  37. private int maxLineLength = 1024;
  38.  
  39. @Autowired
  40. private ThreadPoolTaskExecutor defaultThreadPool;
  41.  
  42. private Channel channel;
  43.  
  44. @PostConstruct
  45. public void init() {
  46. defaultThreadPool.execute(this::doBinding);
  47. }
  48.  
  49. @PreDestroy
  50. public void stop() {
  51. channel.close();
  52. }
  53.  
  54. private void doBinding() {
  55. EventLoopGroup bossGroup = new NioEventLoopGroup();
  56. EventLoopGroup workerGroup = new NioEventLoopGroup();
  57. try {
  58. ServerBootstrap b = new ServerBootstrap();
  59. b.group(bossGroup, workerGroup)
  60. .channel(NioServerSocketChannel.class)
  61. .childHandler(new ChannelInitializer<SocketChannel>() {
  62. @Override
  63. public void initChannel(final SocketChannel ch) throws Exception {
  64. ChannelPipeline pipeline = ch.pipeline();
  65. pipeline.addLast(new LineBasedFrameDecoder(maxLineLength));
  66. pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
  67.  
  68. pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));
  69. pipeline.addLast(new CrLfEncoder());
  70.  
  71. // ADD MORE PIPELINE ENTRIES HERE
  72.  
  73. ch.closeFuture().addListener(new ChannelFutureListener() {
  74. @Override
  75. public void operationComplete(ChannelFuture future) throws Exception {
  76. LOG.info("Lost connection from {}", ch.remoteAddress());
  77. }
  78. });
  79.  
  80. }
  81. })
  82. .option(ChannelOption.SO_BACKLOG, backlogSize)
  83. .childOption(ChannelOption.SO_KEEPALIVE, true);
  84.  
  85. LOG.info("Binding server to port {}", port);
  86.  
  87. // Bind and start to accept incoming connections.
  88. ChannelFuture f = b.bind(port).sync();
  89.  
  90. channel = f.channel();
  91. channel.closeFuture().sync();
  92. } catch (InterruptedException e) {
  93. LOG.error("Waiting on server", e);
  94. } finally {
  95. workerGroup.shutdownGracefully();
  96. bossGroup.shutdownGracefully();
  97. }
  98.  
  99. }
  100.  
  101. private static class CrLfEncoder extends MessageToMessageEncoder<String> {
  102. @Override
  103. protected void encode(ChannelHandlerContext ctx, String msg, List<Object> out) throws Exception {
  104. out.add(msg);
  105. out.add("\r\n");
  106. }
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement