3

In a web service environment, I want to make a few independent http calls per each request that my application receives. As these calls are independent, it doesn't make sense to make them serially so I need a sort of concurrency.

To my understanding, it doesn't make sense to carelessly create new threads for each http call. And also, recreating all these threads brings a lot of overhead.

So I think my best option would be to create a FixedThreadPool in a singleton for all http calls and use that across the application.

Is this the best choice?

jmj
  • 237,923
  • 42
  • 401
  • 438
parsa
  • 2,628
  • 3
  • 34
  • 44
  • Are you using any particular web-server? Most web-servers will do this for you. For instance, Tomcat already has a fixed thread pool, and will allocate threads from that pool for each request. – rodion Jan 22 '11 at 08:51
  • Having a FixedThreadPool (OR any concurrency util) for making Http service call is a huge overkill. Why do you want to perform threading for an operation like this. – uncaught_exceptions Jan 22 '11 at 08:55
  • @doc_180: I'm new to concurrency so please shoot your alternatives. – parsa Jan 22 '11 at 08:58
  • @rodion: I'm using Jetty, and what I observe is that calls are made serially. – parsa Jan 22 '11 at 08:59
  • Hmmm, Tomcat allocating fixed thread pool does not happen in the client side, does it? I believe he is trying to create a Rest Client and wants to make http calls using FixedThreadPool. If its very important, I will use a framework like Spring REST Template to implement my client.) – uncaught_exceptions Jan 22 '11 at 09:01
  • @Parsa, like I mentioned, I might think of using a framework like Spring rest client. Is my understanding correct? Are you implementing a REST client? And can you justify the need for threading? Are each calls synchronous and holds up your process? – uncaught_exceptions Jan 22 '11 at 09:04
  • Yes I'm implementing a REST client indeed, think of it as a web service that takes 2 numbers from a request, and has to make a call to another server to check if the numbers are valid Ints. And then sums them up. So the validity check can be done independently. Right now it is sync and holds up the process. And actually I prefer not to use Spring. – parsa Jan 22 '11 at 09:11
  • @Parsa, I was concerned with using threading for making http calls. Ofcourse usage of concurrency util does take away some pain points. Of course you need to write an abstration layer to insulate your client code from multithreading. There is a similiar discussion here:http://stackoverflow.com/questions/2000439/java-async-rest-web-service-using-jersey or you could use asynchronous technology like javascript or flex to make rest calls. Otherwise I do not see any other alternative. Executors.newFixedThreadPool sounds about right. – uncaught_exceptions Jan 22 '11 at 09:25

2 Answers2

4

You should take a serious look at Akka, and specifically, the Camel support it offers.

David
  • 5,184
  • 3
  • 41
  • 67
3

You may want to consider using the AsyncHttpClient AsyncHttpClient library for very easy asynchronous http calls. A good series of blog posts that describes how to use this library is Going Asynchronous using AsyncHttpClient

denis phillips
  • 12,550
  • 5
  • 33
  • 47