c.lindemann
c.lindemann

Reputation: 15

Invoking asynchronous method from servlet

Context:

I have done alot of reading, but only found this semi-relevant.

I have a servlet that calls a method from another Java class, while passing session-sensitive data. It is running on the Tomcat server.

It looks something like this:

@WebServlet(urlPatterns = {"/MyServlet", "/"})
public class MyServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException{ 
        HttpSession session = request.getSession(true); //Start a session
        String UserID = session.getId();                //Get a unique user ID
        MyClass cls = new MyClass();                    //Initialize my class
        String shortID = cls.newID(UserID);         //Call a method from class
        System.out.println("Your short ID is: " + shortID); //Print the result
    }
}

And say the class that I'm calling looks like this:

public class MyClass {

    public String newID(String UserID){
        String shortID;
            ... //Method for shortening the UserID
            return(shortID);
    }
}

The method of course requires much more processing than shown in this trivial example.

Question 1:

Now, my current understanding is that when n users call MyServlet simultaneously, Tomcat creates n threads in doGet method. So when method newID is called synchronously, Tomcat queues the threads and and allows them to execute one after another. Is this correct?

Question 2:

The problem arises when I have a large number of users, as the n th user will have to wait for newID method to be completed n-1 times, which might take a while. Therefore I will want to call the newID method asynchronously in order to run n threads in parallel, so to increase the throughput.

How can I achieve this?

Would you recommend using reflections, or wrapping the newID method inside a Runnable?

Does Tomcat take care of ExecutorService, or do I need to implement it too?

Any help will be much appreciated. Example code will be useful too!


Upvotes: 1

Views: 2077

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279960

Question 1:

The Servlet Container keeps a pool of threads that it uses to handle requests. It also keeps a single instance of your servlet class. When a request comes in, it dispatches one of those threads which eventually hits the corresponding doXXX method. Threads aren't really queued. They work in parallel. So requests that come in at the same time are being handled parallelly (to a certain degree, read here.

Question 2:

Say you've configured your servlet container to have a pool of 10 threads to handle requests. But at some point you get 15 requests. Then yes, 5 requests will be queued awaiting an available thread. In that case, you can use Servlet 3.0 async support (read1, read2) to have the request handled in a separate thread (that the container also manages). When your processing is complete, a pool thread is re-dispatched to finish the request processing.

Read this for information about how to manage Threads.

Upvotes: 1

Related Questions