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.