Tuesday, July 10, 2012

Jersey ApacheHttpClient with connection pooling

Using Jersey ApacheHttpClient
When using the jersey client. Make use you use ApacheHttpClient instead of jersey client.
http://jersey.java.net/nonav/apidocs/1.2/contribs/jersey-apache-client/com/sun/jersey/client/apache/ApacheHttpClient.html

This is the standard way you get the client

        MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
        connectionManager.getParams().setConnectionTimeout(connectTimeout);
        connectionManager.getParams().setSoTimeout(readTimeout);
        connectionManager.getParams().setDefaultMaxConnectionsPerHost(maxConnectionsPerHost);
        HttpClient httpClient = new HttpClient(connectionManager);
        ApacheHttpClientHandler httpClientHandler = new ApacheHttpClientHandler(httpClient);
        contentServerClient = new ApacheHttpClient(httpClientHandler);
        contentServerClient.setConnectTimeout(connectTimeout);
        contentServerClient.setReadTimeout(readTimeout);


once you get the client get the web resource from the uri

   WebResource resource = getContentServerClient().resource(contentServerUrl).path("streets").path("6708NE");

        try {
           Street street = resource.get(Street.class);
        } catch (UniformInterfaceException e) {
            throw new NoContentFoundException("Can't locate street for Id " + kaniId);
        }

No comments:

Post a Comment