Mikhail
Mikhail

Reputation: 1613

How to configure apache http client v3.0 for web application (multithreading)

I use apache http client (version 3.0 because we have java 1.4 and cannot change it) in web application. I have component (class) that is used for POST requests to the server, and this component is used on JSP pages, i.e. from different threads.

So I create client only once in some init() method of that component:

  HttpClient client =
            new HttpClient(new MultiThreadedHttpConnectionManager());
  client.getHostConfiguration().setProxy(proxyHost, proxyPortInt.intValue());
  HttpState state = new HttpState();
  state.setProxyCredentials(new AuthScope(proxyHost, proxyPortInt.intValue()),
            new UsernamePasswordCredentials(username, userpassword));
  client.setState(state);

and then use it in method that can be accesses in several threads:

PostMethod method = new PostMethod(urlStr);
method.setRequestEntity(new StringRequestEntity(requestStr));
method.setRequestHeader("Host", "the_same_host_every_time ");
method.setRequestHeader("Content-Type", "application/soap+xml");
method.setRequestHeader("Content-Length", String.valueOf(requestStr.length()));
InputStream responseStream = null;
try {
  int resultCode = client.executeMethod(method);
  responseStream = method.getResponseBodyAsStream();
...

This app makes requests to the same host every time. MultiThreadedHttpConnectionManager has the following properties:

  1. DEFAULT_MAX_HOST_CONNECTIONS The default maximum number of connections allowed per host (Per RFC 2616 section 8.1.4, this value defaults to 2.)
  2. DEFAULT_MAX_TOTAL_CONNECTIONS The default maximum number of connections allowed overall

I didn't change them yet. Does this mean that my app will be able to do max 2 request at the same time (because all they are to the same host)? Why default value is 2? Should I change DEFAULT_MAX_HOST_CONNECTIONS value (there are a lot of users for this application that can access JSP simultaneously). What's about DEFAULT_MAX_TOTAL_CONNECTIONS, is this parameter important for my application?

Thanks in advance!

Upvotes: 1

Views: 2136

Answers (1)

Axel Knauf
Axel Knauf

Reputation: 1683

We had a similar scenario some time ago and the key points to multi-threaded use were

  • using a MultiThreadedHttpConnectionManager (you already do)
  • NOT sharing instances of HttpMethod (e. g. GetMethod) and HttpState between threads

HttpState encapsulates the conversational state between subsequent requests including the session ID (if any). So when trying to do concurrent requests to some kind of "backend" (e. g. a web site) you do not want to share it between threads in order not to mix up sessions and get unpredictable results.

The reference documentation has a chapter on threading and also a section called "Concurrent execution of HTTP Methods". The earlier explains the configuration parameters you mention, the latter multi-threaded use in general.

Upvotes: 1

Related Questions