Question

I'm trying to run a long-running process in Google App Engine using Java. (basically i need something like a daemon thread? or just something runs indefinitely and doesn't block UI but can still communicate with other threads)

so i tried:

@SuppressWarnings("serial")
public class TestingAServlet extends HttpServlet {

    public void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws IOException {
        resp.setContentType("text/plain");
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    resp.getWriter().println("hi, from background thread");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        System.out.println("done");
    }
}

but i get an exception:

java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "modifyThreadGroup")

how do i make a background thread that runs indefinitely on GAE with java?

Was it helpful?

Solution

AppEngine doesn't allow usage of traditional threads.

You can, however, use the Modules API to spawn background tasks: https://developers.google.com/appengine/docs/java/modules/#Java_Background_threads https://developers.google.com/appengine/docs/java/javadoc/com/google/appengine/api/ThreadManager

Note that such background tasks can only be spawned on manual scaling instances.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top