3

Here is my use case.

  1. Client sends a request to server.
  2. The server need to do the following 2.a. Make a network call to get some data (D).
    2.b. Create a processor (P) to process the data.
  3. P processes D and sends the response back to client

Creating the processor is expensive (around the order of 1-3 seconds.) But it doesn't depend on the data D.

My plan is to execute the network call and creation of the processor in parallel using two different threads.

I've never done multithread programming inside an app server. My question is what is the best way to deal with threads inside an app server (specifically Tomcat and Jetty)

Thanks.

Soumya Simanta
  • 11,523
  • 24
  • 106
  • 161

3 Answers3

5

IMO your best bet is to use the Executor framework. This makes dealing with concurrency much easier.

Here are a couple of tutorials to get you started.

The fact that your code is running inside a web container such as Tomcat should not bother you too much. It means that the actual thread handling the request is in fact a worker thread, taken from a thread pool managed by the application server itself. However, as long as your own threads do their job cleanly (i.e. only modify data confined to this actual request and don't interfere with other, external threads), and there aren't too many of them at the same time, everything should be fine.

Péter Török
  • 114,404
  • 31
  • 268
  • 329
4

Tomcat 7 supports Servlet 3.0 and it's async servlets - it maintains own thread pool and provides standard API to execute requests in separate threads. You can see example here: Async Servlets in Tomcat 7

Konstantin V. Salikhov
  • 4,554
  • 2
  • 35
  • 48
  • 1
    i'll just toss out that jetty 8 supports servlet 3.0 as well..and ultimately you should adopt a standard approach as opposed to a bespoke implementation, at least if your going to adopt the servlet api in the first place. – jesse mcconnell May 09 '12 at 18:53
  • As far as I know, Spring MVC doesn't support this yet. You also can't use autowiring in such a servlet, but you can obtain spring context with the following code and obtain your beans from it: ServletContext context = getServletContext(); WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(context); – Konstantin V. Salikhov May 09 '12 at 19:45
  • Full async support is coming in the next version of Spring, there's blogs about it being published at the moment: http://blog.springsource.org/2012/05/06/spring-mvc-3-2-preview-introducing-servlet-3-async-support/ – Pidster May 10 '12 at 08:31
0

If creating the processor (P) is expensive, can you pre-create a pool of P instances, and re-use them, in the same way that a database connection pool is created?

The Apache Commons Pool project could give you a start point.

Pidster
  • 628
  • 4
  • 9