开发者

Configuring HttpClient for usage as Restlet client

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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜