You could leverage HttpClient ability to maintain stateful connections [1]
One can either manually manage user identity by setting a user token uniquely identifying a particular user in the execution context
HttpClientContext context = HttpClientContext.create();
context.setUserToken("user 1");
Or use a custom user token handler to do it automatically for all requests.
This code snippet demonstrates how one can use thread id to make HttpClient re-use persistent connections only when created by the same thread.
UserTokenHandler userTokenHandler = new UserTokenHandler() {
@Override
public Object getUserToken(final HttpContext context) {
return Thread.currentThread().getId();
}
};
CloseableHttpClient client = HttpClients.custom()
.setUserTokenHandler(userTokenHandler)
.build();
[1] http://hc.apache.org/httpcomponents-client-4.3.x/tutorial/html/advanced.html#stateful_conn