다음 코드는 옵션이 있는 큐에 태스크를 추가합니다.
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>
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"
)
public class Enqueue extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String key = request.getParameter("key");
// Add the task to the default queue.
Queue queue = QueueFactory.getDefaultQueue();
queue.add(TaskOptions.Builder.withUrl("/worker").param("key", key));
response.sendRedirect("/");
}
}
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"
)
public class Worker extends HttpServlet {
private static final Logger log = Logger.getLogger(Worker.class.getName());
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String key = request.getParameter("key");
// Do something with key.
// ...
}
}
이 큐에 추가된 태스크는 URL /worker
에서 매개변수 key
로 요청 핸들러를 호출하여 실행됩니다. queue.xml
파일에 설정된 속도 또는 초당 태스크 5개의 기본 속도로 실행됩니다.