简体   繁体   English

配置HttpClient以用作Restlet客户端

[英]Configuring HttpClient for usage as Restlet client

I'm stuck configuring Restlet for my client-side code. 我无法为我的客户端代码配置Restlet。 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. 我正在使用Restlet 2和HttpClient 4.我将扩展jar和HttpClient jar添加到构建路径,它似乎工作。

However, I don't know how to configure it in detail. 但是,我不知道如何详细配置它。 I don't create any client manually, instead I use ClientResource s for interactions, which is the only part where I use Restlet directly. 我没有手动创建任何客户端,而是使用ClientResource进行交互,这是我直接使用Restlet的唯一部分。 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. 我发现了一些如何配置客户端的提示,但它们都是为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. clientResource.getClientInfo().setAgent(…)不起作用。
  • 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. 显然,Restlet到目前为止每个ClientResource创建一个新的连接,这不是很有效。

Of course, I already had a look at HttpClientHelper , but I don't know where and how to add this. 当然,我已经看过HttpClientHelper ,但我不知道在哪里以及如何添加它。 Already searched the documentation for that, but no hits. 已经搜索了文档,但没有点击。

Thanks for help! 感谢帮助!

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. 首先,为了确保您使用Apache的HttpClient进行连接,您需要在类路径上使用org.restlet.ext.httpclient.jar。 Second, Are you passing a Context into the constructor of your ClientResource ? 其次,您是否将Context传递给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. 这会处理你感兴趣的maxConnectionsPerHost设置。另外,调用setAgent对我来说正如预期的那样工作。 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. 关于持久连接,似乎HttpClient会为您解决这个问题。 Restlet utilizes HttpClient's ThreadSafeClientConnManager described here . Restlet使用此处描述的HttpClient的ThreadSafeClientConnManager 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. 您可能希望重用ClientResource的相同实例来利用此功能。 I'm not immediately aware of ClientResource 's thread-safety policy but I would hope that it is thread-safe. 我没有立即意识到ClientResource的线程安全策略,但我希望它是线程安全的。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM