|
| 1 | +/** |
| 2 | + * Copyright 2015 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.taskqueue; |
| 18 | + |
| 19 | +import com.google.appengine.api.taskqueue.TaskOptions; |
| 20 | +import com.google.appengine.api.taskqueue.Queue; |
| 21 | +import com.google.appengine.api.taskqueue.QueueFactory; |
| 22 | +import com.google.appengine.api.taskqueue.TaskHandle; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import javax.servlet.ServletException; |
| 26 | +import javax.servlet.http.HttpServlet; |
| 27 | +import javax.servlet.http.HttpServletRequest; |
| 28 | +import javax.servlet.http.HttpServletResponse; |
| 29 | + |
| 30 | +import java.util.Date; |
| 31 | +import java.util.List; |
| 32 | +import java.util.logging.Logger; |
| 33 | +import java.util.concurrent.TimeUnit; |
| 34 | + |
| 35 | +/** |
| 36 | + * Form Handling Servlet |
| 37 | + * This servlet has one method |
| 38 | + * {@link #doPost(<#HttpServletRequest req#>, <#HttpServletResponse resp#>)} which takes the form |
| 39 | + * submisson from /src/main/webapp/tasks.jsp to add and delete tasks. |
| 40 | + */ |
| 41 | +public class TaskqueueServlet extends HttpServlet { |
| 42 | + |
| 43 | + private static final Logger log = Logger.getLogger(TaskqueueServlet.class.getName()); |
| 44 | + private static final int numberOfTasksToAdd = 100; |
| 45 | + private static final int numberOfTasksToLease = 100; |
| 46 | + private static boolean useTaggedTasks = true; |
| 47 | + private static String output; |
| 48 | + private static String message; |
| 49 | + |
| 50 | + // Process the http POST of the form |
| 51 | + @Override |
| 52 | + public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, |
| 53 | + ServletException { |
| 54 | + if (req.getParameter("addTask") != null) { |
| 55 | + String content = req.getParameter("content"); |
| 56 | + String output = String.format("Adding %d Tasks to the Task Queue with a payload of '%s'", |
| 57 | + numberOfTasksToAdd, content.toString()); |
| 58 | + log.info(output.toString()); |
| 59 | + |
| 60 | + // Add Tasks to Task Queue |
| 61 | + // [START get_queue] |
| 62 | + Queue q = QueueFactory.getQueue("pull-queue"); |
| 63 | + // [END get_queue] |
| 64 | + if (!useTaggedTasks) { |
| 65 | + for (int i = 0; i < numberOfTasksToAdd; i++) { |
| 66 | + // [START add_task] |
| 67 | + q.add(TaskOptions.Builder.withMethod(TaskOptions.Method.PULL) |
| 68 | + .payload(content.toString())); |
| 69 | + // [END add_task] |
| 70 | + } |
| 71 | + } else { |
| 72 | + for (int i = 0; i < numberOfTasksToAdd; i++) { |
| 73 | + // [START add_task_w_tag] |
| 74 | + q.add(TaskOptions.Builder.withMethod(TaskOptions.Method.PULL) |
| 75 | + .payload(content.toString()) |
| 76 | + .tag("process".getBytes())); |
| 77 | + // [END add_task_w_tag] |
| 78 | + |
| 79 | + } |
| 80 | + } |
| 81 | + try { |
| 82 | + message = "Added " + numberOfTasksToAdd + " tasks to the task queue."; |
| 83 | + req.setAttribute("message", message); |
| 84 | + req.getRequestDispatcher("tasks.jsp").forward(req,resp); |
| 85 | + } catch (ServletException e) { |
| 86 | + throw new ServletException("ServletException error: ", e); |
| 87 | + } |
| 88 | + } else if (req.getParameter("leaseTask") != null) { |
| 89 | + output = String.format("Pulling %d Tasks from the Task Queue", numberOfTasksToLease); |
| 90 | + log.info(output.toString()); |
| 91 | + |
| 92 | + // Pull tasks from the Task Queue and process them |
| 93 | + Queue q = QueueFactory.getQueue("pull-queue"); |
| 94 | + if (!useTaggedTasks) { |
| 95 | + // [START lease_tasks] |
| 96 | + List<TaskHandle> tasks = q.leaseTasks(3600, TimeUnit.SECONDS, numberOfTasksToLease); |
| 97 | + // [END lease_tasks] |
| 98 | + message = processTasks(tasks, q); |
| 99 | + } else { |
| 100 | + // [START lease_tasks_by_tag] |
| 101 | + // Lease only tasks tagged with "process" |
| 102 | + List<TaskHandle> tasks = q.leaseTasksByTag(3600, TimeUnit.SECONDS, numberOfTasksToLease, "process"); |
| 103 | + // You can also specify a tag to lease via LeaseOptions passed to leaseTasks. |
| 104 | + // [END lease_tasks_by_tag] |
| 105 | + message = processTasks(tasks, q); |
| 106 | + } |
| 107 | + req.setAttribute("message", message); |
| 108 | + req.getRequestDispatcher("tasks.jsp").forward(req,resp); |
| 109 | + } else { |
| 110 | + resp.sendRedirect("/"); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + //Method to process and delete tasks |
| 115 | + private static String processTasks(List<TaskHandle> tasks, Queue q) { |
| 116 | + String payload; |
| 117 | + int numberOfDeletedTasks = 0; |
| 118 | + for (TaskHandle task : tasks) { |
| 119 | + payload = new String(task.getPayload()); |
| 120 | + output = String.format("Processing: taskName='%s' payload='%s'", task.getName() |
| 121 | + .toString(), payload.toString()); |
| 122 | + log.info(output.toString()); |
| 123 | + output = String.format("Deleting taskName='%s'", task.getName().toString()); |
| 124 | + log.info(output.toString()); |
| 125 | + // [START delete_task] |
| 126 | + q.deleteTask(task); |
| 127 | + // [END delete_task] |
| 128 | + numberOfDeletedTasks++; |
| 129 | + } |
| 130 | + if (numberOfDeletedTasks > 0) { |
| 131 | + message = "Processed and deleted " + numberOfTasksToLease + " tasks from the " + |
| 132 | + " task queue."; |
| 133 | + } else { |
| 134 | + message = "Task Queue has no tasks available for lease."; |
| 135 | + } |
| 136 | + return message; |
| 137 | + } |
| 138 | +} |
0 commit comments