Java 8 has reached end of support and will be deprecated on January 31, 2026. After deprecation, you won't be able to deploy Java 8 applications, even if your organization previously used an organization policy to re-enable deployments of legacy runtimes. Your existing Java 8 applications will continue to run and receive traffic after their deprecation date. We recommend that you migrate to the latest supported version of Java.

A Java Task Queue Example

The following code adds a task to a queue with options.

In index.html:

<!-- A basic index.html file served from the "/" URL. -->
<html>
<body>
<p>Enqueue a value, to be processed by a worker.</p>
<form action="/enqueue" method="post">
 <input type="text" name="key">
 <input type="submit">
</form>
</body>
</html>

In Enqueue.java:

// The Enqueue servlet should be mapped to the "/enqueue" URL.
// With @WebServlet annotation the webapp/WEB-INF/web.xml is no longer required.
@WebServlet(
name="TaskEnque",
description="taskqueue: Enqueue a job with a key",
urlPatterns="/taskqueues/enqueue"
)
publicclass EnqueueextendsHttpServlet{
protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
Stringkey=request.getParameter("key");
// Add the task to the default queue.
Queuequeue=QueueFactory.getDefaultQueue();
queue.add(TaskOptions.Builder.withUrl("/worker").param("key",key));
response.sendRedirect("/");
}
}

In Worker.java:

// The Worker servlet should be mapped to the "/worker" URL.
// With @WebServlet annotation the webapp/WEB-INF/web.xml is no longer required.
@WebServlet(
name="TaskWorker",
description="TaskQueues: worker",
urlPatterns="/taskqueues/worker"
)
publicclass WorkerextendsHttpServlet{
privatestaticfinalLoggerlog=Logger.getLogger(Worker.class.getName());
protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
Stringkey=request.getParameter("key");
// Do something with key.
// ...
}
}

Tasks added to this queue will execute by calling the request handler at the URL /worker with the parameter key. They will execute at the rate set in the queue.xml file, or the default rate of 5 tasks per second.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025年10月30日 UTC.