配置 HttpClient 以用作 Restlet 客户端

发布于 2024-09-10 12:16:59 字数 681 浏览 7 评论 0原文

我一直在为我的客户端代码配置 Restlet。我正在使用 Restlet 2 和 HttpClient 4。我将扩展 jar 和 HttpClient jar 添加到构建路径,它似乎可以工作。

但是,我不知道如何详细配置它。我没有手动创建任何客户端,而是使用 ClientResource 进行交互,这是我直接使用 Restlet 的唯一部分。客户端的具体实例似乎隐藏在框架实现中。我发现了一些如何配置客户端的提示,但它们都是为 Restlet 1.x 编写的。

详细来说,我想配置以下部分:

  • 更改客户端请求的用户代理。 clientResource.getClientInfo().setAgent(…) 不起作用。
  • 增加每台主机的并行连接数。
  • 启用每个主机的持久连接和池化。显然,到目前为止,Restlet 为每个 ClientResource 创建了一个新连接,这并不是真正高效。

当然,我已经看过 HttpClientHelper,但我不知道在哪里以及如何添加它。已经搜索过相关文档,但没有找到。

感谢您的帮助!

I'm stuck configuring Restlet for my client-side code. I'm using Restlet 2 and HttpClient 4. I added the extension jar and the HttpClient jars to the build path and it seems to work.

However, I don't know how to configure it in detail. I don't create any client manually, instead I use ClientResources for interactions, which is the only part where I use Restlet directly. The concrete instantiation of clients seems to be hidden in the framework implementation. I found some hints how I might configure clients, but they all were written for Restlet 1.x.

In detail, I want to configure the following parts:

  • Change the User Agent for client requests. clientResource.getClientInfo().setAgent(…) does not work.
  • Increase the number of parallel connections per host.
  • Enable persistent connections and pooling per host. Obviously, Restlet so far creates a new connection per ClientResource, which is not really efficient.

Of course, I already had a look at HttpClientHelper, but I don't know where and how to add this. Already searched the documentation for that, but no hits.

Thanks for help!

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

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

发布评论

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

评论(1

冷清清 2024-09-17 12:16:59

首先,为了确保 Restlet 使用 Apache 的 HttpClient 进行连接,您需要在类路径中包含 org.restlet.ext.httpclient.jar。其次,您是否将 Context 传递到 ClientResource 的构造函数中?如果没有,您将需要:

    final Context context = new Context();
    context.getParameters().set("maxConnectionsPerHost", "20");

    final ClientResource requestResource = new ClientResource(context, "http://localhost:8182/request");
    requestResource.getClientInfo().setAgent("Example-Client/1.0");

这会处理您感兴趣的 maxConnectionsPerHost 设置。此外,调用 setAgent 对我来说按预期工作。我不确定您的实例可能存在什么问题。

关于持久连接,似乎 HttpClient 会为您处理这个问题。 Restlet 利用 HttpClient 的 ThreadSafeClientConnManager 描述 此处。它提到了对该链接的持久连接的支持。看来这个对象也会解决你的池问题。您可能希望重用相同的 ClientResource 实例来利用这一点。我没有立即意识到 ClientResource 的线程安全策略,但我希望它是线程安全的。

First, to make sure that you Restlet uses Apache's HttpClient for connections, you need to have org.restlet.ext.httpclient.jar on the classpath. Second, Are you passing a Context into the constructor of your ClientResource? If not you will need to:

    final Context context = new Context();
    context.getParameters().set("maxConnectionsPerHost", "20");

    final ClientResource requestResource = new ClientResource(context, "http://localhost:8182/request");
    requestResource.getClientInfo().setAgent("Example-Client/1.0");

That takes care of the maxConnectionsPerHost setting you are interested in. Also, calling setAgent was working as expected for me. I'm not sure what could be the problem in your instance.

Regarding persistent connections, it seems that HttpClient takes care of that for you. Restlet utilizes HttpClient's ThreadSafeClientConnManager described here. It mentions support for persistent connections at that link. It seems that this object will also take care of your pooling concerns. You would want to reuse the same instance of ClientResource to take advantage of this. I'm not immediately aware of ClientResource's thread-safety policy but I would hope that it is thread-safe.

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