Java NIO 프레임워크 Netty의 간단한 사용 예

9991 단어 JavaNIONetty
앞서 Java 네트워크 IO 프로그래밍 요약(BIO, NIO, AIO 모두 전체 인스턴스 코드 포함), 자바 원생 IO 지원을 사용하여 네트워크 프로그래밍을 하는 방법을 소개했고 본고는 더욱 간단한 방식인 자바 NIO 프레임워크를 소개한다.
Netty는 업계에서 가장 유행하는 NIO 프레임워크 중 하나로 양호한 건장성, 기능, 성능, 맞춤형 구성과 확장성을 가지고 있다.또한 매우 간단한 API를 제공하여 우리의 네트워크 프로그래밍을 크게 간소화시켰다.
Java IO 소개의 문장과 같이 본고가 보여준 예는 같은 기능을 실현했다.
1. 서버
Server:

package com.anxpp.io.calculator.netty; 
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; 
public class Server { 
  private int port; 
  public Server(int port) { 
    this.port = port; 
  } 
  public void run() throws Exception { 
    EventLoopGroup bossGroup = new NioEventLoopGroup(); 
    EventLoopGroup workerGroup = new NioEventLoopGroup(); 
    try { 
      ServerBootstrap b = new ServerBootstrap(); 
      b.group(bossGroup, workerGroup) 
       .channel(NioServerSocketChannel.class) 
       .option(ChannelOption.SO_BACKLOG, 1024) 
       .childOption(ChannelOption.SO_KEEPALIVE, true) 
       .childHandler(new ChannelInitializer<SocketChannel>() { 
         @Override 
         public void initChannel(SocketChannel ch) throws Exception { 
           ch.pipeline().addLast(new ServerHandler()); 
         } 
       }); 
      ChannelFuture f = b.bind(port).sync(); 
      System.out.println(" :"+port); 
      f.channel().closeFuture().sync(); 
    } finally { 
      workerGroup.shutdownGracefully(); 
      bossGroup.shutdownGracefully(); 
    } 
  } 
  public static void main(String[] args) throws Exception { 
    int port; 
    if (args.length > 0) { 
      port = Integer.parseInt(args[0]); 
    } else { 
      port = 9090; 
    } 
    new Server(port).run(); 
  } 
} 
ServerHandler:

package com.anxpp.io.calculator.netty; 
import io.netty.buffer.ByteBuf; 
import io.netty.buffer.Unpooled; 
import io.netty.channel.ChannelHandlerContext; 
import io.netty.channel.ChannelInboundHandlerAdapter; 
import java.io.UnsupportedEncodingException; 
import com.anxpp.io.utils.Calculator; 
public class ServerHandler extends ChannelInboundHandlerAdapter { 
  @Override 
  public void channelRead(ChannelHandlerContext ctx, Object msg) throws UnsupportedEncodingException { 
    ByteBuf in = (ByteBuf) msg; 
    byte[] req = new byte[in.readableBytes()]; 
    in.readBytes(req); 
    String body = new String(req,"utf-8"); 
    System.out.println(" :"+body); 
    String calrResult = null; 
    try{ 
      calrResult = Calculator.Instance.cal(body).toString(); 
    }catch(Exception e){ 
      calrResult = " :" + e.getMessage(); 
    } 
    ctx.write(Unpooled.copiedBuffer(calrResult.getBytes())); 
  } 
  @Override 
  public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { 
    ctx.flush(); 
  } 
  /** 
   *   
   */ 
  @Override 
  public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { 
    cause.printStackTrace(); 
    ctx.close(); 
  } 
} 
package com.anxpp.io.calculator.netty; 
import io.netty.buffer.ByteBuf; 
import io.netty.buffer.Unpooled; 
import io.netty.channel.ChannelHandlerContext; 
import io.netty.channel.ChannelInboundHandlerAdapter; 
import java.io.UnsupportedEncodingException; 
import com.anxpp.io.utils.Calculator; 
public class ServerHandler extends ChannelInboundHandlerAdapter { 
  @Override 
  public void channelRead(ChannelHandlerContext ctx, Object msg) throws UnsupportedEncodingException { 
    ByteBuf in = (ByteBuf) msg; 
    byte[] req = new byte[in.readableBytes()]; 
    in.readBytes(req); 
    String body = new String(req,"utf-8"); 
    System.out.println(" :"+body); 
    String calrResult = null; 
    try{ 
      calrResult = Calculator.Instance.cal(body).toString(); 
    }catch(Exception e){ 
      calrResult = " :" + e.getMessage(); 
    } 
    ctx.write(Unpooled.copiedBuffer(calrResult.getBytes())); 
  } 
  @Override 
  public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { 
    ctx.flush(); 
  } 
  /** 
   *   
   */ 
  @Override 
  public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { 
    cause.printStackTrace(); 
    ctx.close(); 
  } 
} 
2. 클라이언트
Client:

package com.anxpp.io.calculator.netty; 
import io.netty.bootstrap.Bootstrap; 
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.NioSocketChannel; 
import java.util.Scanner; 
public class Client implements Runnable{ 
  static ClientHandler client = new ClientHandler(); 
  public static void main(String[] args) throws Exception { 
    new Thread(new Client()).start(); 
    @SuppressWarnings("resource") 
    Scanner scanner = new Scanner(System.in); 
    while(client.sendMsg(scanner.nextLine())); 
  } 
  @Override 
  public void run() { 
    String host = "127.0.0.1"; 
    int port = 9090; 
    EventLoopGroup workerGroup = new NioEventLoopGroup(); 
    try { 
      Bootstrap b = new Bootstrap(); 
      b.group(workerGroup); 
      b.channel(NioSocketChannel.class); 
      b.option(ChannelOption.SO_KEEPALIVE, true); 
      b.handler(new ChannelInitializer<SocketChannel>() { 
        @Override 
        public void initChannel(SocketChannel ch) throws Exception { 
          ch.pipeline().addLast(client); 
        } 
      }); 
      ChannelFuture f = b.connect(host, port).sync(); 
      f.channel().closeFuture().sync(); 
    } catch (InterruptedException e) { 
      e.printStackTrace(); 
    } finally { 
      workerGroup.shutdownGracefully(); 
    } 
  } 
} 
ClientHandler:

package com.anxpp.io.calculator.netty; 
import io.netty.buffer.ByteBuf; 
import io.netty.buffer.Unpooled; 
import io.netty.channel.ChannelHandlerContext; 
import io.netty.channel.ChannelInboundHandlerAdapter; 
import java.io.UnsupportedEncodingException; 
public class ClientHandler extends ChannelInboundHandlerAdapter { 
  ChannelHandlerContext ctx; 
  /** 
   * tcp  
   */ 
  @Override 
  public void channelActive(ChannelHandlerContext ctx) throws Exception { 
    this.ctx = ctx; 
  } 
  public boolean sendMsg(String msg){ 
    System.out.println(" :"+msg); 
    byte[] req = msg.getBytes(); 
    ByteBuf m = Unpooled.buffer(req.length); 
    m.writeBytes(req); 
    ctx.writeAndFlush(m); 
    return msg.equals("q")?false:true; 
  } 
  /** 
   *   
   * @throws UnsupportedEncodingException 
   */ 
  @Override 
  public void channelRead(ChannelHandlerContext ctx, Object msg) throws UnsupportedEncodingException { 
    ByteBuf buf = (ByteBuf) msg; 
    byte[] req = new byte[buf.readableBytes()]; 
    buf.readBytes(req); 
    String body = new String(req,"utf-8"); 
    System.out.println(" :"+body); 
  } 
  /** 
   *   
   */ 
  @Override 
  public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { 
    cause.printStackTrace(); 
    ctx.close(); 
  } 
} 
3. 계산용 도구 클래스

package com.anxpp.io.utils;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public enum Calculator {
  Instance;
  private final static ScriptEngine jse = new ScriptEngineManager().getEngineByName("JavaScript");
  public Object cal(String expression) throws ScriptException{
    return jse.eval(expression);
  }
}
4. 테스트
각각 서버와 클라이언트를 시작한 다음 클라이언트 콘솔에 표현식을 입력합니다.

1+5+5+5+5+5
 :1+5+5+5+5+5
 :26
156158*458918+125615
 :156158*458918+125615
 :7.1663842659E10
1895612+555+5+5+5+5+5+5+5-5*4/4
 :1895612+555+5+5+5+5+5+5+5-5*4/4
 :1896197
서버가 되돌아오는 결과를 볼 수 있습니다.
서버 콘솔 보기:

 :9090
 :1+5+5+5+5+5
 :156158*458918+125615
 :1895612+555+5+5+5+5+5+5+5-5*4/4
5, 그 이상
관련 기사:
Java 네트워크 IO 프로그래밍 요약(BIO, NIO, AIO 모두 전체 인스턴스 코드 포함)
본문 예제 및 Java BIO NIO AIO 예제 소스 Git 주소: https://github.com/anxpp/Java-IO.git
Netty 관련 컨텐츠는 누추한 통신 서버가 완료될 때까지 계속 업데이트됩니다.
이상은 본문의 전체 내용입니다. 여러분의 학습에 도움이 되고 저희를 많이 응원해 주십시오.

좋은 웹페이지 즐겨찾기