RenegadeAndy
RenegadeAndy

Reputation: 5690

Servlet doesnt appear to execute in a threaded manner

I have developed a simple server using Tomcat which runs a servlet.

The servlet calls a command line program - which takes about 20 seconds to execute then returns the result to the user via JSON. The problem is - if i make above 2 simultaneous requests, the servlet blocks until one of the previous requests is completed.

An example of this can be seen below - "Im in" is the top of the servlet, and the list of results is after the servlet is executed. All requests were made at the same time - but you can clearly see they are not dealt with simultaneously. What setting do I need to change in tomcat in order to have all requests handeled at the same time?

Im in 

Im in 

FVFNT01 STOP_IDLE FVFNT03 STOP_IDLE FVFNT16 STOP_IDLE FVFNT17 STOP_IDLE
FVFNT01 STOP_IDLE FVFNT03 STOP_IDLE FVFNT16 STOP_IDLE FVFNT17 STOP_IDLE
Im in

FVFNT01 STOP_IDLE FVFNT03 STOP_IDLE FVFNT16 STOP_IDLE FVFNT17 STOP_IDLE
Im in

FVFNT01 STOP_IDLE FVFNT03 STOP_IDLE FVFNT16 STOP_IDLE FVFNT17 STOP_IDLE
Im in

FVFNT01 STOP_IDLE FVFNT03 STOP_IDLE FVFNT16 STOP_IDLE FVFNT17 STOP_IDLE
Im in

FVFNT01 STOP_IDLE FVFNT03 STOP_IDLE FVFNT16 STOP_IDLE FVFNT17 STOP_IDLE
Im in

FVFNT01 STOP_IDLE FVFNT03 STOP_IDLE FVFNT16 STOP_IDLE FVFNT17 STOP_IDLE
Im in

FVFNT01 STOP_IDLE FVFNT03 STOP_IDLE FVFNT16 STOP_IDLE FVFNT17 STOP_IDLE

Upvotes: 2

Views: 361

Answers (4)

talel
talel

Reputation: 355

Same request to the same server will be threaded once you will run you app on the Tomcat(and not via eclipse in debugging mode).

A simple test case can be done: make some simple doGet which will do the next thing:

PrintWriter printWriter = response.getWriter();
printWriter.append("doGet: Hello from testServlet! doing some thread check :)");
try{Thread.sleep(15*1000);}catch(Exception e){}
printWriter.close();

now run a web browser and run this servlet multiple times and you will see that all of them returns at the same time(+-). if the servlet was not threaded you would expect the the first one will return after 15sec, the second one after 30sec and so on...

Upvotes: 0

BalusC
BalusC

Reputation: 1108642

The problem is in your test client. It is firing requests synchronously. It should fire requests asynchronously, then the servlet will be able to do the same :)

Roughly the same question was asked 4 days ago, I've posted an answer with a code example how the test client should look like: Servlet requests are executed sequentially for no apparent reason in Glassfish v3. You may find it useful as well.

Upvotes: 1

Dónal
Dónal

Reputation: 187499

Requests to servlets are handled concurrently by default. There is no setting that enables/disables this behaviour. This is confirmed by the JavaDoc for HttpServlet:

Servlets typically run on multithreaded servers, so be aware that a servlet must handle concurrent requests and be careful to synchronize access to shared resources.

However, if your servlet implements the marker interface SingleThreadModel, the servlet will only handle one request at-a-time. However, use of this interface is generally considered a bad practice, and because you didn't mention it, I assume you're not using it.

Of course even if you're not implementing SingleThreadModel you can make any servlet single-threaded using (probably inappropriate) synchronisation, e.g.

class MyServlet extends HttpServlet {

  private void Object sharedObject = new Object()

  protected synchronized void doGet(HttpServletRequest req, HttpServletResponse resp) {
    // method logic goes here
  }

  protected void doPost(HttpServletRequest req, HttpServletResponse resp) {

    synchronized(sharedObject) {
      // method logic goes here
    }

  }

  protected void doPut(HttpServletRequest req, HttpServletResponse resp) {

    synchronized(this) {
      // method logic goes here
    }
  }
}

In the example above, only one thread may execute the same request method at-a-time, though it is possible (for example) for one thread to execute doPost() while another is executing doGet().

If you don't understand why this is, then I recommend you do some reading on concurrent programming in Java before investigating your problem further.

Upvotes: 1

Peter Štibraný
Peter Štibraný

Reputation: 32893

Make sure your requests are using different HTTP connections and check your tomcat configuration if it permits more than 1 thread (by default it does, so unless you've changed it, it should be OK).

Upvotes: 0

Related Questions