Servers for working with multiple threads and Google App Engine

In Google App Engine (GAE), frontend instances can create up to 10 threads to maximize throughput. According to this page , such multithreading can be done like this:

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 the back end of GAE, I will show many servlets mapped to specific URLs, such FizzServlet

as 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 think I'm gasping for breath on how to put these two ideas together. From what I understand, you have 3 different mechanisms / elements here:

  • The App Engine instance itself, whose lifecycle I can hook into by injecting ServletContextListener

    and running my own code when GAE launches the instance; and
  • This material ThreadFactory

    / ThreadManager

    (above)
  • Servlets / Listeners

I think I am wondering how to implement code like this so that every time a new request is included, say FizzServlet#doGet

, to make sure the request is sent to an available stream (if there is one available). This way, if it FizzServlet

was the only servlet I exposed, it could be called up to 10 times before it caused the new (11th) incoming request to hang while processing the previous request.

I'm looking for code to mark between the servlet and this threading code. Thanks in advance.

+3


source to share


1 answer


I think I am wondering how to implement the code in such a way that every time a new request is included, say FizzServlet # doGet, how to make sure the request is sent to an available stream (if there is one available). Thus, if FizzServlet was the only servlet I was showing, it could be called up to 10 times before it would cause the new (11th) incoming request to hang while processing the previous request.

What the GAE servlet engine does for you. You deploy an application that contains a servlet, and when a request comes in, the servlet engine uses a thread to process the request and calls your servlet. You have nothing to do.



If your servlet method, doGet()

or doPost()

called by GAE, needs to perform multiple tasks in parallel (such as accessing several other websites, for example), then you will start the topics yourself, as explained on the page you linked to.

+2


source







All Articles