Restlet HTTP Connection Pool

ε祈祈猫儿з 提交于 2019-12-02 18:03:23

问题


I am fairly new to Restlet and wrote small piece of code to make a HTTP call. It is working but I was wondering how can I add HTTP Connection pooling (apache) into it. I am not able to find any tutorial or reference code for it.

        Client client = new Client(Protocol.HTTP); 
        ChallengeResponse challengeResponse =  new ChallengeResponse(
        ChallengeScheme.HTTP_AZURE_SHAREDKEY,
            acctName,
            accKey);

        String url = RestHelper.createRequestURI("CCC");

        Request request = new Request(Method.GET, url);
        request.setChallengeResponse(challengeResponse);
        Response response = client.handle(request); 

Any references or help will be appreciated.


回答1:


In fact, Restlet internally manages a pool at the client connector level. Configuration of this pool can be done using the context of your client. The following example describes to configure it:

Client client = new Client(new Context(), Protocol.HTTP);
client.getContext().getParameters().add("maxConnectionsPerHost","5");
client.getContext().getParameters().add("maxTotalConnections","5");

You can notice that these parameters depend on the underlying client connector you use.

Here are some helpful links:

  • Doc related to connectors: http://restlet.com/technical-resources/restlet-framework/guide/2.3/core/base/connectors
  • Javadoc containing parameters for the HTTP client connector: http://restlet.com/technical-resources/restlet-framework/javadocs/2.3/jse/ext/org/restlet/ext/httpclient/HttpClientHelper.html

Notice that if you use ClientResource, you need to share the same client to have only one instance of the client connector under the hood. Otherwise a new one is instantiated for each request. See the way to implement this below:

Client client = new Client(new Context(), Protocol.HTTP);
ClientResource cr = new ClientResource("http://myurl");
cr.setNext(client);

Hope it helps, Thierry



来源:https://stackoverflow.com/questions/28417669/restlet-http-connection-pool

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!