Jersey 客户端连接关闭内存泄漏问题

发布于 2024-12-14 06:23:07 字数 692 浏览 4 评论 0原文

我正在使用 Jersey v10 并编写了以下代码。这是关闭 Jersey 客户端连接以避免内存泄漏的正确方法吗?在此之前我没有对他进行任何调用。

ClientConfig config = setupHttps();
    final Client c = Client.create(config);

    final WebResource r = c.resource(baseUri);
    ClientResponse response = null;
    try {
        response = r.path("/....")
                .header("contentId", id)
                .header("sid", sid).get(ClientResponse.class);
        ...



    } catch (Exception e) {
        log.error("Error returning contentServiceName.");

    } finally {
        if (response != null) {
            response.close();
        }
        if (c!= null) {
            c.destroy();
        }

    }

TIA, 维杰

I am using Jersey v10 and have written the following code.Is this the right way to close a Jersey client connection to avoid memory leaks.I was not doing any calls int he finally before this.

ClientConfig config = setupHttps();
    final Client c = Client.create(config);

    final WebResource r = c.resource(baseUri);
    ClientResponse response = null;
    try {
        response = r.path("/....")
                .header("contentId", id)
                .header("sid", sid).get(ClientResponse.class);
        ...



    } catch (Exception e) {
        log.error("Error returning contentServiceName.");

    } finally {
        if (response != null) {
            response.close();
        }
        if (c!= null) {
            c.destroy();
        }

    }

TIA,
Vijay

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾挽 2024-12-21 06:23:07

据我所知,是的,这是关闭泽西岛客户的正确方法......但有以下警告。

1)您试图防止的不是内存泄漏,而是连接(到您正在寻址的服务器)泄漏...

2)以下内容是关于 Client 泽西岛手册第 3 章

客户端实例是昂贵的资源。建议重复使用已配置的实例来创建 Web 资源。 Web 资源的创建、请求的构建和响应的接收都保证是线程安全的。因此,Client 实例和 WebResource 实例可以在多个线程之间共享

因此,如果您计划进行多个调用,最好不要为每个调用调用 destroy 。对于 WebResources 来说也是如此(但程度较小)。

3)我所描述的是泽西岛1.1(但我看到到目前为止有关此的线程回到 2009 年)。

As far as I know, yes, this is the right way to close a Jersey client ... with the following caveats.

1) What you're trying to prevent is not memory leaks, but connection (to the server you're addressing) leaks ...

2) The following is written about the Client class in Chapter 3 of the Jersey Handbook:

Client instances are expensive resources. It is recommended a configured instance is reused for the creation of Web resources. The creation of Web resources, the building of requests and receiving of responses are guaranteed to be thread safe. Thus a Client instance and WebResource instances may be shared between multiple threads

Therefore, if you're planning on making multiple calls, it's a good idea not to call destroy for every call. The same (but to a lesser extent) is true for WebResources.

3) What I'm describing is from Jersey 1.1 (but I see threads about this as far back as 2009).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文