Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Le code suivant permet d'ajouter une tâche à une file d'attente avec des options.
Dans 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>
Dans 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")publicclassEnqueueextendsHttpServlet{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("/");}}
Dans 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")publicclassWorkerextendsHttpServlet{privatestaticfinalLoggerlog=Logger.getLogger(Worker.class.getName());protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{Stringkey=request.getParameter("key");// Do something with key.// ...}}
Les tâches ajoutées à cette file d'attente seront exécutées en appelant le gestionnaire de requêtes à l'URL /worker avec le paramètre key. Elles s'exécutent au débit défini dans le fichier queue.xml ou au débit par défaut de cinq tâches par seconde.
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/09/04 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 2025/09/04 (UTC)."],[[["\u003cp\u003eThis content demonstrates how to add a task to a queue using legacy bundled services and APIs within the first-generation App Engine standard environment.\u003c/p\u003e\n"],["\u003cp\u003eThe provided \u003ccode\u003eindex.html\u003c/code\u003e includes a form to submit a value (\u003ccode\u003ekey\u003c/code\u003e) to the \u003ccode\u003e/enqueue\u003c/code\u003e URL for task queuing.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eEnqueue.java\u003c/code\u003e servlet handles POST requests to \u003ccode\u003e/taskqueues/enqueue\u003c/code\u003e, adding tasks with the provided \u003ccode\u003ekey\u003c/code\u003e to the default queue.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eWorker.java\u003c/code\u003e servlet, mapped to \u003ccode\u003e/taskqueues/worker\u003c/code\u003e, processes the tasks, receiving the \u003ccode\u003ekey\u003c/code\u003e parameter for execution.\u003c/p\u003e\n"],["\u003cp\u003eTasks in the queue are executed by calling the \u003ccode\u003e/worker\u003c/code\u003e request handler, with the rate determined by the \u003ccode\u003equeue.xml\u003c/code\u003e file or defaulting to 5 tasks per second.\u003c/p\u003e\n"]]],[],null,["# A Java Task Queue Example\n\nThe following code adds a task to a queue with options.\n| This page describes how to use the legacy bundled services and APIs. This API can only run in first-generation runtimes in the App Engine standard environment. If you are updating to the App Engine Java 11/17 runtime, refer to the [migration guide](/appengine/migration-center/standard/migrate-to-second-gen/java-differences) to learn about your migration options for legacy bundled services.\n\n\u003cbr /\u003e\n\nIn `index.html`: \n\n \u003c!-- A basic index.html file served from the \"/\" URL. --\u003e\n \u003chtml\u003e\n \u003cbody\u003e\n \u003cp\u003eEnqueue a value, to be processed by a worker.\u003c/p\u003e\n \u003cform action=\"/enqueue\" method=\"post\"\u003e\n \u003cinput type=\"text\" name=\"key\"\u003e\n \u003cinput type=\"submit\"\u003e\n \u003c/form\u003e\n \u003c/body\u003e\n \u003c/html\u003e\n\nIn `Enqueue.java`: \n\n // The Enqueue servlet should be mapped to the \"/enqueue\" URL.\n // With @WebServlet annotation the webapp/WEB-INF/web.xml is no longer required.\n @WebServlet(\n name = \"TaskEnque\",\n description = \"taskqueue: Enqueue a job with a key\",\n urlPatterns = \"/taskqueues/enqueue\"\n )\n public class Enqueue extends HttpServlet {\n\n protected void doPost(HttpServletRequest request, HttpServletResponse response)\n throws ServletException, IOException {\n String key = request.getParameter(\"key\");\n\n // Add the task to the default queue.\n Queue queue = QueueFactory.getDefaultQueue();\n queue.add(TaskOptions.Builder.withUrl(\"/worker\").param(\"key\", key));\n\n response.sendRedirect(\"/\");\n }\n }\n\nIn `Worker.java`: \n\n // The Worker servlet should be mapped to the \"/worker\" URL.\n // With @WebServlet annotation the webapp/WEB-INF/web.xml is no longer required.\n @WebServlet(\n name = \"TaskWorker\",\n description = \"TaskQueues: worker\",\n urlPatterns = \"/taskqueues/worker\"\n )\n public class Worker extends HttpServlet {\n\n private static final Logger log = Logger.getLogger(Worker.class.getName());\n\n protected void doPost(HttpServletRequest request, HttpServletResponse response)\n throws ServletException, IOException {\n String key = request.getParameter(\"key\");\n\n // Do something with key.\n // ...\n }\n }\n\nTasks 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."]]