Created
          December 18, 2016 05:59 
        
      - 
      
- 
        Save panjiesw/7d46feee1dc6cbaca0ffc0b228819537 to your computer and use it in GitHub Desktop. 
    Reactor Netty echo server attempt
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | package com.github.panjiesw.netty.reactor.server; | |
| import io.netty.channel.EventLoopGroup; | |
| import io.netty.channel.nio.NioEventLoopGroup; | |
| import io.netty.util.CharsetUtil; | |
| import org.slf4j.Logger; | |
| import org.slf4j.LoggerFactory; | |
| import reactor.ipc.netty.tcp.TcpServer; | |
| import java.net.InetSocketAddress; | |
| /** | |
| * @author Panjie SW. | |
| */ | |
| public class ReactorServer { | |
| private static final Logger logger = LoggerFactory.getLogger(ReactorServer.class); | |
| private final int port; | |
| private ReactorServer(int port) { | |
| this.port = port; | |
| } | |
| private void start() throws InterruptedException { | |
| EventLoopGroup group = new NioEventLoopGroup(1); | |
| try { | |
| final TcpServer server = TcpServer.create(opts -> opts | |
| .eventLoopGroup(group) | |
| .listen(new InetSocketAddress(port))); | |
| server | |
| .newHandler((in, out) -> out.send( | |
| in.receive() | |
| .take(1) | |
| .log(ReactorServer.class.getName()) | |
| .map(data -> { | |
| logger.info("Server Received: {}", data.toString(CharsetUtil.UTF_8)); | |
| return data; | |
| }))) | |
| .block() | |
| .channel() | |
| .closeFuture().sync(); | |
| } finally { | |
| group.shutdownGracefully().sync(); | |
| } | |
| } | |
| public static void main(String[] args) throws InterruptedException { | |
| if (args.length != 1) { | |
| System.err.println("Usage: " + ReactorServer.class.getSimpleName() + " <port>"); | |
| } else { | |
| int port = Integer.parseInt(args[0]); | |
| new ReactorServer(port).start(); | |
| } | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment