user1768830
user1768830

Reputation:

Multi-Threading & Google App Engine Servlets

On Google App Engine (GAE) it is possible for frontend instances to create up to 10 threads to maximize throughput. According to this page, such multi-threading can be accomplished as follows:

Runnable myTask = new Runnable({
    @Override
    public void run() {
        // Do whatever
    }
});

ThreadFactory threadFactory = ThreadManager.currentRequestThreadFactory();

// GAE caps frontend instances to 10 worker threads per instance.
threadFactory.newRequestThread(myTask);

To hit my GAE server-side, I'll expose many servlets mapped to certain URLs, such as the FizzServlet mapped to http://myapp.com/fizz:

public class FizzServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        // Handle the request here. Somehow send it to an available
        // worker thread.
    }
}

I guess I'm choking on how to connect these two ideas. As far as I see it, you have 3 different mechanisms/items here:

  1. The App Engine instance itself, whose lifecycle I can "hook" by implementing a ServletContextListener and run custom code when GAE fires up the instance; and
  2. This ThreadFactory/ThreadManager stuff (above)
  3. The servlets/listeners

I guess I'm wondering how to implement code such that every time a new request comes into, say, FizzServlet#doGet, how to make sure that request gets sent to an available thread (if there is one available). That way, if FizzServlet was the only servlet I was exposing, it could get called up to 10 times before it would cause a new (11th) incoming request to hang while a previous request was processing.

I'm looking for the glue code between the servlet and this thread-creating code. Thanks in advance.

Upvotes: 3

Views: 2956

Answers (1)

JB Nizet
JB Nizet

Reputation: 691685

I guess I'm wondering how to implement code such that every time a new request comes into, say, FizzServlet#doGet, how to make sure that request gets sent to an available thread (if there is one available). That way, if FizzServlet was the only servlet I was exposing, it could get called up to 10 times before it would cause a new (11th) incoming request to hang while a previous request was processing.

That's what the GAE servlet engine does for you. You deploy an app containing a servlet, and when a request comes in, the servlet engine uses a thread to process the request and calls your servlet. You don't have anything to do.

If your servlet's doGet() or doPost() method, invoked by GAE, needs to perform several tasks in parallel (like contacting several other web sites for example), then you'll start threads by yourself as explained in the page you linked to.

Upvotes: 2

Related Questions