简体   繁体   English

如何从 HTTP 服务器请求处理程序 in.netty 正确调用 HTTP 客户端?

[英]How to call properly HTTP client from HTTP server request handler in netty?

I am developing custom HTTP server with.netty 3.3.1.我正在使用 .netty 3.3.1 开发自定义 HTTP 服务器。

I need to implement something like this我需要实现这样的东西

  1. HTTP Server receives request HTTP 服务器收到请求
  2. HTTP Server parses it and invokes HTTP request as a client to other machine HTTP 服务器解析它并调用 HTTP 请求作为客户端到其他机器
  3. HTTP Server waits for the response of request sent in (2) HTTP 服务器等待(2)中发送的请求的响应
  4. HTTP Server sends response to request from (1) based on what had received in (3) HTTP 服务器根据在 (3) 中收到的内容发送对来自 (1) 的请求的响应

It means that client request (2) has to behave as synchronous.这意味着客户端请求 (2) 必须表现为同步。

What I wrote is based on HttpSnoopClient example but it does not work, because I receive我写的是基于HttpSnoopClient 示例,但它不起作用,因为我收到

java.lang.IllegalStateException: 
await*() in I/O thread causes a dead lock or sudden performance drop. Use addListener() instead or call await*() from a different thread. 

I've refactored the code from the example mentioned above and now it looks more less like this (starting from line 7f of HttpSnoopClient):我重构了上面提到的示例中的代码,现在它看起来不像这样(从 HttpSnoopClient 的第 7f 行开始):

    ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
    future.addListener(new ChannelFutureListener() {
      public void operationComplete(ChannelFuture future) {
        if (!future.isSuccess()) {
          System.err.println("Cannot connect"); 
            future.getCause().printStackTrace();
            bootstrap.releaseExternalResources();
            return;
          }
          System.err.println("Connected");

          Channel channel = future.getChannel();

          // Send the HTTP request.
          channel.write(request);
          channel.close();



          // Wait for the server to close the connection.
          channel.getCloseFuture().addListener(new ChannelFutureListener() {
            public void operationComplete(ChannelFuture future) {
              System.err.println("Disconnected"); 
              bootstrap.releaseExternalResources(); // DOES NOT WORK?
            }
          });   
        }
    });

  } 
}

The run() command from the above example is invoked in the messageReceived function of my herver handler.上例中的run()命令在我的 herver 处理程序的messageReceived function 中被调用。

So it became asynchronous and avoid await* functions.所以它变成了异步的并且避免了 await* 函数。 Request is invoked properly.正确调用请求。 But - for uknown reason for me - the line但是-出于我不知道的原因-这条线

              bootstrap.releaseExternalResources(); // DOES NOT WORK?

does not work.不起作用。 It throws an exception saying that I cannot kill the thread I am currently using (which sounds reasonable, but still does not give me an answer how to do that in a different way).它抛出一个异常,说我无法终止我当前正在使用的线程(这听起来很合理,但仍然没有给我一个如何以不同方式做到这一点的答案)。

I am also not sure is this a correct approach?我也不确定这是正确的方法吗?

Maybe you can recommend a tutorial of such event programming techniques in.netty?也许你可以在.netty 中推荐一个此类事件编程技术的教程? How to deal - in general - with a few asynchronous requests that has to be invoked in specified order and wait for each other?通常如何处理一些必须按指定顺序调用并相互等待的异步请求?

Thank you,谢谢,

If you really want to release the bootstrap on close you can do it like this:如果你真的想在关闭时释放引导程序,你可以这样做:

channel.getCloseFuture().addListener(new ChannelFutureListener() {
    public void operationComplete(ChannelFuture future) {
        System.err.println("Disconnected"); 
        new Thread(new Runnable() {
            public void run() {
                bootstrap.releaseExternalResources();
            }
        }).start();
    }
});   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 http客户端如何将http响应与请求(使用Netty)或一般情况相关联? - How does a http client associate an http response with a request (with Netty) or in general? 如何正确处理 HTTP 文件服务器上的客户端“连接:关闭”请求? - How to properly handle client “Connection: close” request on HTTP file server? 使用Netty的异步HTTP客户端 - Asynchronous HTTP client with Netty Cakephp XML HTTP服务器如何从客户端获取请求 - Cakephp XML HTTP server how to get the request from client 如何在golang中使用grpc从服务器向客户端发出HTTP请求 - How to make a HTTP request from server to client using grpc in golang 如何从客户端到服务器在Meteor中进行简单的HTTP请求 - How to make a simple http request in Meteor from client to server 如何通过http持久连接从服务器定期向客户端发送请求 - How to send request periodically to the client from the server by http persistent connection Netty Http客户端:如何将客户端启动,发送请求和客户端关闭分为不同的方法 - Netty Http Client: how to separate client-start, sending request, and client-shutdown into different methods Netty中的同步HTTP调用 - Synchronous HTTP call in Netty 如何使用Netty读取HTTP请求的正文? - How to read the body of the HTTP Request using Netty?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM