Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestEasy client: Invalid use of BasicClientConnManager: connection still allocated

I am using RestEasy client to retrieve a list of entities from web server. This is my code:

@ApplicationScoped
public class RestHttpClient {
    private ResteasyClient client;

    @Inject
    private ObjectMapper mapper;

    @PostConstruct
    public void initialize() {
        HttpParams params = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(params, 5000);
        HttpConnectionParams.setSoTimeout(params, 5000);
        HttpClient httpClient = new DefaultHttpClient(params);
        this.client = new ResteasyClientBuilder().httpEngine(new ApacheHttpClient4Engine(httpClient)).build();
    }

    public <E> List<E> getList(final Class<E> resultClass, final String path,
            MultivaluedMap<String, Object> queryParams) {
        ResteasyWebTarget target = this.client.target(path);
        Response response = null;
        try {
            response = target.queryParams(queryParams).request().get();
            String jsonString = response.readEntity(String.class);
            TypeFactory typeFactory = TypeFactory.defaultInstance();
            List<E> list = this.mapper.readValue(
                    jsonString, typeFactory.constructCollectionType(ArrayList.class, resultClass));
            return list;
        } catch (Exception e) {
            // Handle exception
        } finally {
            if (response != null)
                response.close();
        }

        return null;
    }
}

It works fine, but... if I call getList() method multiple times in quick succession, sometimes I get the error "Invalid use of BasicClientConnManager: connection still allocated". I can make the same sequence of calls over and over again, and it works at least 90% of the time, so it appears to be a race condition. I am closing the Response object in finally block, which should be enough to release all resources, but apparently it isn't. What else do I have to do to make sure the connection is released? I have found some answers on the net, but they are either too old or not RestEasy-specific. I am using resteasy-client 3.0.4.Final.

like image 658
Nikša Baldun Avatar asked Mar 13 '14 23:03

Nikša Baldun


1 Answers

I guess you only have one instance of your class RestHttpClient, and all threads/requests are using the same object.

The default ResteasyClientBuilder does not use a connection pool. Which means you can have only one parallel connection at a time. A request needs to be returned before you can use the ResteasyClient a second time (error message "connection still allocated"). You can avoid that by increasing the connection pool size:

ResteasyClientBuilder clientBuilder = new ResteasyClientBuilder();
clientBuilder = clientBuilder.connectionPoolSize( 20 );
ResteasyWebTarget target = clientBuilder.build().target( "http://your.host" );

I am using the following RestClientFactory to set up a new client. It gives you a debug output of the raw response, specifies the keystore (needed for client ssl certificates), a connection pool and the option for the use of a proxy.

public class RestClientFactory {

    public static class Options {
        private final String baseUri;
        private final String proxyHostname;
        private final String proxyPort;
        private final String keystore;
        private final String keystorePassword;
        private final String connectionPoolSize;
        private final String connectionTTL;

        public Options(String baseUri, String proxyHostname, String proxyPort) {
            this.baseUri = baseUri;
            this.proxyHostname = proxyHostname;
            this.proxyPort = proxyPort;
            this.connectionPoolSize = "100";
            this.connectionTTL = "500";
            this.keystore = System.getProperty( "javax.net.ssl.keyStore" );
            this.keystorePassword = System.getProperty( "javax.net.ssl.keyStorePassword" );
        }

        public Options(String baseUri, String proxyHostname, String proxyPort, String keystore, String keystorePassword, String connectionPoolSize, String connectionTTL) {
            this.baseUri = baseUri;
            this.proxyHostname = proxyHostname;
            this.proxyPort = proxyPort;
            this.connectionPoolSize = connectionPoolSize;
            this.connectionTTL = connectionTTL;
            this.keystore = keystore;
            this.keystorePassword = keystorePassword;
        }
    }

    private static Logger log = LoggerFactory.getLogger( RestClientFactory.class );

    public static <T> T createClient(Options options, Class<T> proxyInterface) throws Exception {
        log.info( "creating ClientBuilder using options {}", ReflectionToStringBuilder.toString( options ) );

        ResteasyClientBuilder clientBuilder = new ResteasyClientBuilder();

        ResteasyProviderFactory providerFactory = new ResteasyProviderFactory();
        RegisterBuiltin.register( providerFactory );
        providerFactory.getClientReaderInterceptorRegistry().registerSingleton( new ReaderInterceptor() {
            @Override
            public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException, WebApplicationException {
                if (log.isDebugEnabled()) {
                    InputStream is = context.getInputStream();
                    String responseBody = IOUtils.toString( is );

                    log.debug( "received response:\n{}\n\n", responseBody );

                    context.setInputStream( new ByteArrayInputStream( responseBody.getBytes() ) );
                }
                return context.proceed();
            }
        } );
        clientBuilder.providerFactory( providerFactory );

        if (StringUtils.isNotBlank( options.proxyHostname ) && StringUtils.isNotBlank( options.proxyPort )) {
            clientBuilder = clientBuilder.defaultProxy( options.proxyHostname, Integer.parseInt( options.proxyPort ) );
        }

        // why the fuck do you have to specify the keystore with RestEasy?
        // not setting the keystore will result in not using the global one
        if ((StringUtils.isNotBlank( options.keystore )) && (StringUtils.isNotBlank( options.keystorePassword ))) {
            KeyStore ks;
            ks = KeyStore.getInstance( KeyStore.getDefaultType() );
            FileInputStream fis = new FileInputStream( options.keystore );
            ks.load( fis, options.keystorePassword.toCharArray() );
            fis.close();
            clientBuilder = clientBuilder.keyStore( ks, options.keystorePassword );
            // Not catching these exceptions on purpose
        }

        if (StringUtils.isNotBlank( options.connectionPoolSize )) {
            clientBuilder = clientBuilder.connectionPoolSize( Integer.parseInt( options.connectionPoolSize ) );
        }

        if (StringUtils.isNotBlank( options.connectionTTL )) {
            clientBuilder = clientBuilder.connectionTTL( Long.parseLong( options.connectionTTL ), TimeUnit.MILLISECONDS );
        }

        ResteasyWebTarget target = clientBuilder.build().target( options.baseUri );

        return target.proxy( proxyInterface );
    }
}

An example client interface:

public interface SimpleClient
{
   @GET
   @Path("basic")
   @Produces("text/plain")
   String getBasic();
}

Create a client:

SimpleClient client = RestClientFactory.createClient( 
    new RestClientFactory.Options(
        "https://your.service.host",
        "proxyhost",
        "8080",
        "keystore.jks",
        "changeit",
        "20",
        "500"
    )
     ,SimpleClient.class
);

See also: http://docs.jboss.org/resteasy/docs/3.0-beta-3/userguide/html/RESTEasy_Client_Framework.html#d4e2049

like image 56
adiesner Avatar answered Sep 20 '22 23:09

adiesner