httpClient连接未关闭

时间:2016-08-23 05:07:46

标签: java http connection vert.x

vert.x core: 3.3.0

上下文

我只是尝试在核心示例io.vertx.example.core.http.simple.Client中运行http客户端。 在运行此示例时,它发现已建立的连接在请求完成后未关闭。

服务器端我没有看到任何问题。因为在尝试使用jmeter和服务器时工作正常。所以我认为问题在于HttpClient。

任何人都可以帮助我吗?

提前致谢。 重现的步骤

running io.vertx.example.core.http.simple.Server code
running io.vertx.example.core.http.simple.Client code

附加

即使请求和响应结束后,仍会显示以下内容。在给予 LINUX

lsof -i -P
java 32551 USER 223u IPv4 16264097 0t0 TCP localhost:8080->localhost:26980 (ESTABLISHED)
java 32634 USER 218u IPv4 16264087 0t0 TCP localhost:26980->localhost:8080 (ESTABLISHED)

WINDOWS

TCP    127.0.0.1:8080         FSSCHND12957:56893     ESTABLISHED
TCP    127.0.0.1:56893        FSSCHND12957:8080      ESTABLISHED

在LINUX和WINDOWS系统中都尝试过。

客户代码

package io.vertx.example.core.http.simple;

import io.vertx.core.AbstractVerticle;
import io.vertx.example.util.Runner;

/*

    @author Tim Fox
    */
    public class Client extends AbstractVerticle {

    // Convenience method so you can run it in your IDE
    public static void main(String[] args) {
    Runner.runExample(Client.class);
    }

    @Override
    public void start() throws Exception {
    vertx.createHttpClient().getNow(8080, "localhost", "/", resp -> {
    System.out.println("Got response " + resp.statusCode());
    resp.bodyHandler(body -> {
    System.out.println("Got data " + body.toString("ISO-8859-1"));
    });
    });
    }
    }

服务器代码

package io.vertx.example.core.http.simple;

import io.vertx.core.AbstractVerticle;
import io.vertx.example.util.Runner;

/*

    @author Tim Fox
    */
    public class Server extends AbstractVerticle {

    // Convenience method so you can run it in your IDE
    public static void main(String[] args) {
    Runner.runExample(Server.class);
    }

    @Override
    public void start() throws Exception {
    vertx.createHttpServer().requestHandler(req -> {
    req.response().putHeader("content-type", "text/html").end("
    Hello from vert.x!
    ");

    }).listen(8080);
    }
    }

1 个答案:

答案 0 :(得分:1)

我们必须关闭我们通常在java中执行的httpClient。只有end()没有关闭连接。 httpClient.close()是必需的....这解决了我的问题..

修改后的代码:

public class Client extends AbstractVerticle {

// Convenience method so you can run it in your IDE
public static void main(String[] args) {
    Runner.runExample(Client.class);
}

@Override
public void start() throws Exception {
    HttpClient httpClient = vertx.createHttpClient().getNow(8080, "localhost", "/", resp -> {
        System.out.println("Got response " + resp.statusCode());
        resp.bodyHandler(body -> {
            System.out.println("Got data " + body.toString("ISO-8859-1"));
            httpClient.close();
        });
    });
}
}