Here is my code.
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.client.JerseyClient;
import org.glassfish.jersey.client.JerseyClientBuilder;
public class Jersey2HttpClient {
private static class InstanceHolder {
private static final JerseyClient INSTANCE = createClient();
private static JerseyClient createClient() {
ClientConfig clientConfig = new ClientConfig();
clientConfig.property(ClientProperties.READ_TIMEOUT, 20000);
clientConfig.property(ClientProperties.CONNECT_TIMEOUT, 20000);
PoolingHttpClientConnectionManager connectionManager =
new PoolingHttpClientConnectionManager();
connectionManager.setMaxTotal(200);
connectionManager.setDefaultMaxPerRoute(50);
clientConfig.property(ApacheClientProperties.CONNECTION_MANAGER, connectionManager);
clientConfig.connectorProvider(new ApacheConnectorProvider());
JerseyClient client = JerseyClientBuilder.createClient(clientConfig);
client.register(RequestLogger.requestLoggingFilter);
return client;
}
}
public static JerseyClient getInstance() {
return InstanceHolder.INSTANCE;
}
}
I have the following questions.
If every time the same client will be returned(through getInstance()) to the calling thread when are 'connection pooling objects' created? It seems like the same object (client) is used for connections.
What exactly happens when the following code is executed.
JerseyClient client = JerseyClientBuilder.createClient(clientConfig);
In other words why is creating a client an expensive operation? I haven't even mentioned the url or the request yet.
Sorry for my weak knowledge on this subject.
Client
instances are heavy-weightThe initialization of Client
instances might be an expensive operation because Client
s are heavy-weight objects that manage the underlying communication infrastructure with the server.
You should create only a small number of Client
instances and reuse them when possible. The documentation states the following:
Client
s are heavy-weight objects that manage the client-side communication infrastructure. Initialization as well as disposal of aClient
instance may be a rather expensive operation. It is therefore advised to construct only a small number ofClient
instances in the application.Client
instances must be properly closed before being disposed to avoid leaking resources.
ApacheConnectorProvider
By default, the transport layer in Jersey is provided by HttpURLConnection
. This support is implemented in Jersey via HttpUrlConnectorProvider
. You can replace the default connector if you want to.
Jersey integrates with Apache HTTP Client via the ApacheConnectorProvider
. To use it, ensure you have the following dependecy:
<dependency>
<groupId>org.glassfish.jersey.connectors</groupId>
<artifactId>jersey-apache-connector</artifactId>
<version>2.23.2</version>
</dependency>
In your code, you have instantiated a PoolingHttpClientConnectionManager
but you are not using it anywhere. Add the following lines to your code:
clientConfig.property(ApacheClientProperties.CONNECTION_MANAGER, connectionManager);
clientConfig.connectorProvider(new ApacheConnectorProvider());
For additional details, refer to Jersey documentation about connectors.