I have an REST API which in back end has to call an asynchronous task. This task will POST a message to 3rd party URL. I want to keep this task to a thread pool and need a performance of 300 tasks/second. I have used a HttpClient pool with 50 conections per route and each thread using a HttpClient can do 5 tasks in a second. So I am keeping > 60 threads to achieve my target. Is this approach correct?
-
1I think the following might be useful http://stackoverflow.com/questions/4851535/http-connection-pooling-using-httpclient – user2793390 Mar 07 '14 at 05:07
1 Answers
If you have more tasks to do async like logging, data base entries etc then make a class to do them all in, use a thread pool executor and test that. Keep the number of initial and max threads as configurable, as results will vary in dev, test and live environment. Also use the http client features to do the network part optimally.
Like the PoolingHttpClientConnectionManager http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/conn/PoolingHttpClientConnectionManager.html
So really you need to use a thread pool and within that optomize the network part. Later if your doing data base actions can optomize those seperately too.
TheadPoolExecutor javadoc has some pointers http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ThreadPoolExecutor.html
This consrtructor seems to give most options to tune and handle cases if the queue overflows
ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler)

- 4,493
- 4
- 41
- 70