2017-06-14 299 views

回答

2

这里是我所看到的主要contrastable方面:

编码风格

让我们网状的discard server example这大概是最简单的例子给出它的文档中的第一个。

对于akka-http这是比较简单的:

object WebServer { 
    def main(args: Array[String]) { 

    implicit val system = ActorSystem("my-system") 
    implicit val materializer = ActorMaterializer() 

    val route = 
     extractRequestEntity { entity => 
     onComplete(entity.discardBytes(materializer)) { _ => 
      case _ => complete(StatusCodes.Ok) 
     } 
     } 

    val bindingFuture = Http().bindAndHandle(route, "localhost", 8080) 
} 

对于网状这是更为详细:

public class DiscardServerHandler extends ChannelInboundHandlerAdapter { // (1) 

    @Override 
    public void channelRead(ChannelHandlerContext ctx, Object msg) { // (2) 
     // Discard the received data silently. 
     ((ByteBuf) msg).release(); // (3) 
    } 

    @Override 
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { // (4) 
     // Close the connection when an exception is raised. 
     cause.printStackTrace(); 
     ctx.close(); 
    } 
} 

public class DiscardServer { 

    private int port; 

    public DiscardServer(int port) { 
     this.port = port; 
    } 

    public void run() throws Exception { 
     EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1) 
     EventLoopGroup workerGroup = new NioEventLoopGroup(); 
     try { 
      ServerBootstrap b = new ServerBootstrap(); // (2) 
      b.group(bossGroup, workerGroup) 
      .channel(NioServerSocketChannel.class) // (3) 
      .childHandler(new ChannelInitializer<SocketChannel>() { // (4) 
       @Override 
       public void initChannel(SocketChannel ch) throws Exception { 
        ch.pipeline().addLast(new DiscardServerHandler()); 
       } 
      }) 
      .option(ChannelOption.SO_BACKLOG, 128)   // (5) 
      .childOption(ChannelOption.SO_KEEPALIVE, true); // (6) 

      // Bind and start to accept incoming connections. 
      ChannelFuture f = b.bind(port).sync(); // (7) 

      // Wait until the server socket is closed. 
      // In this example, this does not happen, but you can do that to gracefully 
      // shut down your server. 
      f.channel().closeFuture().sync(); 
     } finally { 
      workerGroup.shutdownGracefully(); 
      bossGroup.shutdownGracefully(); 
     } 
    } 

    public static void main(String[] args) throws Exception { 
     new DiscardServer(8080).run(); 
    } 
} 

指令

一个阿卡-HTTP的最大的优势,在我意见,是Directives,它为复杂的请求处理逻辑提供DSL。例如,假设我们想用GETPUT请求的一条消息进行响应,并针对所有其他请求方法提供另一条消息。这用指令是很容易的:

val route = 
    (get | put) { 
    complete("You sent a GET or PUT") 
    } ~ 
    complete("Shame shame") 

,如果你想从一个请求路径的订单项目及数量:

val route = 
    path("order"/Segment/IntNumber) { (item, qty) => 
    complete(s"Your order: item: $item quantity: $qty") 
    } 

此功能不网状中存在。

最后一个项目我想说明的是全能流。 akka-http基于akka-stream。因此,akka-http很好地处理了请求实体的流式性质。以网状的Looking Into the Received Data例如,阿卡这看起来像

//a stream with a Source, intermediate processing steps, and a Sink 
val entityToConsole : (RequestEntity) => Future[Done] = 
    (_ : RequestEntity) 
    .getDataBytes() 
    .map(_.utf8String) 
    .to(Sink.foreach[String](println)) 
    .run() 

val route = 
    extractRequestEntity { entity => 
    onComplete(entityToConsole(entity)) { _ => 
     case Success(_) => complete(200, "all data written to console") 
     case Failure(_) => complete(404, "problem writing to console) 
    } 
    } 

的Netty必须用字节的缓冲区和while循环处理同样的问题:

@Override 
public void channelRead(ChannelHandlerContext ctx, Object msg) { 
    ByteBuf in = (ByteBuf) msg; 
    try { 
     while (in.isReadable()) { // (1) 
      System.out.print((char) in.readByte()); 
      System.out.flush(); 
     } 
    } finally { 
     ReferenceCountUtil.release(msg); // (2) 
    } 
} 
相关问题