Reputation: 1613
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:
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
Reputation: 1683
We had a similar scenario some time ago and the key points to multi-threaded use were
MultiThreadedHttpConnectionManager
(you already do)HttpMethod
(e. g. GetMethod
) and HttpState
between threadsHttpState
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