Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import io.netty.bootstrap.ServerBootstrap;
- import io.netty.channel.Channel;
- import io.netty.channel.ChannelFuture;
- import io.netty.channel.ChannelFutureListener;
- import io.netty.channel.ChannelHandlerContext;
- import io.netty.channel.ChannelInitializer;
- import io.netty.channel.ChannelOption;
- import io.netty.channel.ChannelPipeline;
- 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 io.netty.handler.codec.LineBasedFrameDecoder;
- import io.netty.handler.codec.MessageToMessageEncoder;
- import io.netty.handler.codec.string.StringDecoder;
- import io.netty.handler.codec.string.StringEncoder;
- import io.netty.util.CharsetUtil;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
- import org.springframework.stereotype.Service;
- import javax.annotation.PostConstruct;
- import javax.annotation.PreDestroy;
- import java.util.List;
- @Service
- @ConfigurationProperties(prefix="server")
- public class SpringBootNettyServer {
- private static Logger LOG = LoggerFactory.getLogger(SpringBootNettyServer.class);
- private int backlogSize = 128;
- private int port = 143;
- private int maxLineLength = 1024;
- @Autowired
- private ThreadPoolTaskExecutor defaultThreadPool;
- private Channel channel;
- @PostConstruct
- public void init() {
- defaultThreadPool.execute(this::doBinding);
- }
- @PreDestroy
- public void stop() {
- channel.close();
- }
- private void doBinding() {
- EventLoopGroup bossGroup = new NioEventLoopGroup();
- EventLoopGroup workerGroup = new NioEventLoopGroup();
- try {
- ServerBootstrap b = new ServerBootstrap();
- b.group(bossGroup, workerGroup)
- .channel(NioServerSocketChannel.class)
- .childHandler(new ChannelInitializer<SocketChannel>() {
- @Override
- public void initChannel(final SocketChannel ch) throws Exception {
- ChannelPipeline pipeline = ch.pipeline();
- pipeline.addLast(new LineBasedFrameDecoder(maxLineLength));
- pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
- pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));
- pipeline.addLast(new CrLfEncoder());
- // ADD MORE PIPELINE ENTRIES HERE
- ch.closeFuture().addListener(new ChannelFutureListener() {
- @Override
- public void operationComplete(ChannelFuture future) throws Exception {
- LOG.info("Lost connection from {}", ch.remoteAddress());
- }
- });
- }
- })
- .option(ChannelOption.SO_BACKLOG, backlogSize)
- .childOption(ChannelOption.SO_KEEPALIVE, true);
- LOG.info("Binding server to port {}", port);
- // Bind and start to accept incoming connections.
- ChannelFuture f = b.bind(port).sync();
- channel = f.channel();
- channel.closeFuture().sync();
- } catch (InterruptedException e) {
- LOG.error("Waiting on server", e);
- } finally {
- workerGroup.shutdownGracefully();
- bossGroup.shutdownGracefully();
- }
- }
- private static class CrLfEncoder extends MessageToMessageEncoder<String> {
- @Override
- protected void encode(ChannelHandlerContext ctx, String msg, List<Object> out) throws Exception {
- out.add(msg);
- out.add("\r\n");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement