views:

133

answers:

1

Hi,

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!

+1  A: 

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.

laz
Thanks for your answer. Unfortunately, setting the user agent still doesn't work and http://www.restlet.org/documentation/2.0/jse/api/org/restlet/resource/ClientResource.html states that ClientResources are not designed to be shared among several threads, which is really bad.
PartlyCloudy
I should have just read the Javadoc! I found something interesting that may not be an intended side effect of using `ClientResource`. After making a request with the `ClientResource` instance, the results of `requestResource.getNext()` will return the `Client` object used to perform the call. You could save the reference to that object and reuse it on future `ClientResources` by providing to `setNext(Uniform next)`. Since the `Client` encapsulates all of the HttpClient objects you would save on recreating all of those objects. I'm not sure how much an abuse that approach would be!
laz
That trick actually works for some concurrent requests. If however the number of requests increases and there are much more requests than connections, it starts to throw some internal exceptions randomly. So I'd prefer an official solution. Nevertheless thanks for your hint.
PartlyCloudy