From 554a57ff206b70d3a710585eb5b06c6f38b672c6 Mon Sep 17 00:00:00 2001 From: Jonathan Simon Date: Wed, 27 Apr 2016 10:15:19 -0700 Subject: [PATCH 1/7] Adding pull task queue sample --- appengine/taskqueue/pull/README.md | 37 +++++ .../DevAppServer.launch | 14 ++ .../UpdateApplication.launch | 14 ++ appengine/taskqueue/pull/nbactions.xml | 55 +++++++ appengine/taskqueue/pull/pom.xml | 142 ++++++++++++++++++ .../example/taskqueue/TaskqueueServlet.java | 138 +++++++++++++++++ .../taskqueue/TaskqueueServlet.java-hold | 112 ++++++++++++++ .../taskqueue/TaskqueueServlet.java-old | 112 ++++++++++++++ .../taskqueue/TaskqueueServlet.java.save | 138 +++++++++++++++++ .../src/main/webapp/WEB-INF/appengine-web.xml | 6 + .../main/webapp/WEB-INF/logging.properties | 13 ++ .../pull/src/main/webapp/WEB-INF/queue.xml | 10 ++ .../pull/src/main/webapp/WEB-INF/web.xml | 19 +++ .../taskqueue/pull/src/main/webapp/tasks.jsp | 36 +++++ 14 files changed, 846 insertions(+) create mode 100644 appengine/taskqueue/pull/README.md create mode 100644 appengine/taskqueue/pull/eclipse-launch-profiles/DevAppServer.launch create mode 100644 appengine/taskqueue/pull/eclipse-launch-profiles/UpdateApplication.launch create mode 100644 appengine/taskqueue/pull/nbactions.xml create mode 100644 appengine/taskqueue/pull/pom.xml create mode 100644 appengine/taskqueue/pull/src/main/java/com/example/taskqueue/TaskqueueServlet.java create mode 100644 appengine/taskqueue/pull/src/main/java/com/example/taskqueue/TaskqueueServlet.java-hold create mode 100644 appengine/taskqueue/pull/src/main/java/com/example/taskqueue/TaskqueueServlet.java-old create mode 100644 appengine/taskqueue/pull/src/main/java/com/example/taskqueue/TaskqueueServlet.java.save create mode 100644 appengine/taskqueue/pull/src/main/webapp/WEB-INF/appengine-web.xml create mode 100644 appengine/taskqueue/pull/src/main/webapp/WEB-INF/logging.properties create mode 100644 appengine/taskqueue/pull/src/main/webapp/WEB-INF/queue.xml create mode 100644 appengine/taskqueue/pull/src/main/webapp/WEB-INF/web.xml create mode 100644 appengine/taskqueue/pull/src/main/webapp/tasks.jsp diff --git a/appengine/taskqueue/pull/README.md b/appengine/taskqueue/pull/README.md new file mode 100644 index 00000000000..796acd839a9 --- /dev/null +++ b/appengine/taskqueue/pull/README.md @@ -0,0 +1,37 @@ +# Pull task Queue sample for Google App Engine + +This sample demonstrates how to use [pull task queues][appid] on [Google App +Engine][ae-docs]. + +[appid]: https://cloud.google.com/appengine/docs/java/taskqueue/overview-pull +[ae-docs]: https://cloud.google.com/appengine/docs/java/ + +## Running locally +This example uses the +[Maven gcloud plugin](https://cloud.google.com/appengine/docs/java/managed-vms/maven). +To run this sample locally: + + $ mvn appengine:devserver + +## Deploying +In the following command, replace YOUR-PROJECT-ID with your +[Google Cloud Project ID](https://developers.google.com/console/help/new/#projectnumber). + + $ mvn appengine:update -Dappengine.appId=YOUR-PROJECT-ID -Dappengine.version=SOME-VERSION + +## Setup +To save your project settings so that you don't need to enter the + parameters, you can: + +1. Update the tag in src/main/webapp/WEB-INF/appengine-web.xml + with your project name. + +2. Update the tag in src/main/webapp/WEB-INF/appengine-web.xml + with a valid version number. + + +You will now be able to run + + $ mvn appengine:update + +without the need for any additional parameters. diff --git a/appengine/taskqueue/pull/eclipse-launch-profiles/DevAppServer.launch b/appengine/taskqueue/pull/eclipse-launch-profiles/DevAppServer.launch new file mode 100644 index 00000000000..2290cd0cea4 --- /dev/null +++ b/appengine/taskqueue/pull/eclipse-launch-profiles/DevAppServer.launch @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/appengine/taskqueue/pull/eclipse-launch-profiles/UpdateApplication.launch b/appengine/taskqueue/pull/eclipse-launch-profiles/UpdateApplication.launch new file mode 100644 index 00000000000..caa7ec79dbd --- /dev/null +++ b/appengine/taskqueue/pull/eclipse-launch-profiles/UpdateApplication.launch @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/appengine/taskqueue/pull/nbactions.xml b/appengine/taskqueue/pull/nbactions.xml new file mode 100644 index 00000000000..cd6020b1ee3 --- /dev/null +++ b/appengine/taskqueue/pull/nbactions.xml @@ -0,0 +1,55 @@ +#set( $symbol_pound = '#' ) +#set( $symbol_dollar = '$' ) +#set( $symbol_escape = '\' ) + + + + CUSTOM-appengine:devserver + appengine:devserver + + appengine:devserver + + + + CUSTOM-appengine:update + appengine:update + + appengine:update + + + + CUSTOM-appengine:rollback + appengine:rollback + + appengine:rollback + + + + CUSTOM-appengine:update_cron + appengine:update_cron + + appengine:update_cron + + + + CUSTOM-appengine:update_dos + appengine:update_dos + + appengine:update_dos + + + + CUSTOM-appengine:update_indexes + appengine:update_indexes + + appengine:update_indexes + + + + CUSTOM-appengine:update_queues + appengine:update_queues + + appengine:update_queues + + + diff --git a/appengine/taskqueue/pull/pom.xml b/appengine/taskqueue/pull/pom.xml new file mode 100644 index 00000000000..aec2c54b0c0 --- /dev/null +++ b/appengine/taskqueue/pull/pom.xml @@ -0,0 +1,142 @@ + + + + 4.0.0 + war + 1.0-SNAPSHOT + com.example.taskqueue + taskqueue + + + 1 + 1.9.34 + 2.0.9.74.v20150814 + UTF-8 + true + + + + 3.1.0 + + + + + com.google.appengine + appengine-api-1.0-sdk + ${appengine.version} + + + javax.servlet + servlet-api + 2.5 + provided + + + jstl + jstl + 1.2 + + + + + com.google.appengine + appengine-testing + ${appengine.version} + test + + + com.google.appengine + appengine-api-stubs + ${appengine.version} + test + + + + + + ${project.build.directory}/${project.build.finalName}/WEB-INF/classes + + + org.codehaus.mojo + versions-maven-plugin + 2.1 + + + compile + + display-dependency-updates + display-plugin-updates + + + + + + org.apache.maven.plugins + 3.1 + maven-compiler-plugin + + 1.7 + 1.7 + + + + org.apache.maven.plugins + maven-war-plugin + 2.4 + + true + + + + ${basedir}/src/main/webapp/WEB-INF + true + WEB-INF + + + + + + + com.google.appengine + appengine-maven-plugin + ${appengine.version} + + false + ${app.version} + + + + + + + + com.google.appengine + gcloud-maven-plugin + ${gcloud.plugin.version} + + true + + + + + + diff --git a/appengine/taskqueue/pull/src/main/java/com/example/taskqueue/TaskqueueServlet.java b/appengine/taskqueue/pull/src/main/java/com/example/taskqueue/TaskqueueServlet.java new file mode 100644 index 00000000000..9bd4529eb3d --- /dev/null +++ b/appengine/taskqueue/pull/src/main/java/com/example/taskqueue/TaskqueueServlet.java @@ -0,0 +1,138 @@ +/** + * Copyright 2015 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.taskqueue; + +import com.google.appengine.api.taskqueue.TaskOptions; +import com.google.appengine.api.taskqueue.Queue; +import com.google.appengine.api.taskqueue.QueueFactory; +import com.google.appengine.api.taskqueue.TaskHandle; + +import java.io.IOException; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import java.util.Date; +import java.util.List; +import java.util.logging.Logger; +import java.util.concurrent.TimeUnit; + +/** + * Form Handling Servlet + * This servlet has one method + * {@link #doPost(<#HttpServletRequest req#>, <#HttpServletResponse resp#>)} which takes the form + * submisson from /src/main/webapp/tasks.jsp to add and delete tasks. + */ +public class TaskqueueServlet extends HttpServlet { + + private static final Logger log = Logger.getLogger(TaskqueueServlet.class.getName()); + private static final int numberOfTasksToAdd = 100; + private static final int numberOfTasksToLease = 100; + private static boolean useTaggedTasks = true; + private static String output; + private static String message; + + // Process the http POST of the form + @Override + public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, + ServletException { + if (req.getParameter("addTask") != null) { + String content = req.getParameter("content"); + String output = String.format("Adding %d Tasks to the Task Queue with a payload of '%s'", + numberOfTasksToAdd, content.toString()); + log.info(output.toString()); + + // Add Tasks to Task Queue + // [START get_queue] + Queue q = QueueFactory.getQueue("pull-queue"); + // [END get_queue] + if (!useTaggedTasks) { + for (int i = 0; i < numberOfTasksToAdd; i++) { + // [START add_task] + q.add(TaskOptions.Builder.withMethod(TaskOptions.Method.PULL) + .payload(content.toString())); + // [END add_task] + } + } else { + for (int i = 0; i < numberOfTasksToAdd; i++) { + // [START add_task_w_tag] + q.add(TaskOptions.Builder.withMethod(TaskOptions.Method.PULL) + .payload(content.toString()) + .tag("process".getBytes())); + // [END add_task_w_tag] + + } + } + try { + message = "Added " + numberOfTasksToAdd + " tasks to the task queue."; + req.setAttribute("message", message); + req.getRequestDispatcher("tasks.jsp").forward(req,resp); + } catch (ServletException e) { + throw new ServletException("ServletException error: ", e); + } + } else if (req.getParameter("leaseTask") != null) { + output = String.format("Pulling %d Tasks from the Task Queue", numberOfTasksToLease); + log.info(output.toString()); + + // Pull tasks from the Task Queue and process them + Queue q = QueueFactory.getQueue("pull-queue"); + if (!useTaggedTasks) { + // [START lease_tasks] + List tasks = q.leaseTasks(3600, TimeUnit.SECONDS, numberOfTasksToLease); + // [END lease_tasks] + message = processTasks(tasks, q); + } else { + // [START lease_tasks_by_tag] + // Lease only tasks tagged with "process" + List tasks = q.leaseTasksByTag(3600, TimeUnit.SECONDS, numberOfTasksToLease, "process"); + // You can also specify a tag to lease via LeaseOptions passed to leaseTasks. + // [END lease_tasks_by_tag] + message = processTasks(tasks, q); + } + req.setAttribute("message", message); + req.getRequestDispatcher("tasks.jsp").forward(req,resp); + } else { + resp.sendRedirect("/"); + } + } + + //Method to process and delete tasks + private static String processTasks(List tasks, Queue q) { + String payload; + int numberOfDeletedTasks = 0; + for (TaskHandle task : tasks) { + payload = new String(task.getPayload()); + output = String.format("Processing: taskName='%s' payload='%s'", task.getName() + .toString(), payload.toString()); + log.info(output.toString()); + output = String.format("Deleting taskName='%s'", task.getName().toString()); + log.info(output.toString()); + // [START delete_task] + q.deleteTask(task); + // [END delete_task] + numberOfDeletedTasks++; + } + if (numberOfDeletedTasks > 0) { + message = "Processed and deleted " + numberOfTasksToLease + " tasks from the " + + " task queue."; + } else { + message = "Task Queue has no tasks available for lease."; + } + return message; + } +} diff --git a/appengine/taskqueue/pull/src/main/java/com/example/taskqueue/TaskqueueServlet.java-hold b/appengine/taskqueue/pull/src/main/java/com/example/taskqueue/TaskqueueServlet.java-hold new file mode 100644 index 00000000000..7d18358c45c --- /dev/null +++ b/appengine/taskqueue/pull/src/main/java/com/example/taskqueue/TaskqueueServlet.java-hold @@ -0,0 +1,112 @@ +/** + * Copyright 2015 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.taskqueue; + +import com.google.appengine.api.taskqueue.TaskOptions; +import com.google.appengine.api.taskqueue.Queue; +import com.google.appengine.api.taskqueue.QueueFactory; +import com.google.appengine.api.taskqueue.TaskHandle; + +import java.io.IOException; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import java.util.Date; +import java.util.List; +import java.util.logging.Logger; +import java.util.concurrent.TimeUnit; + +/** + * Form Handling Servlet + * This servlet has one method + * {@link #doPost(<#HttpServletRequest req#>, <#HttpServletResponse resp#>)} which takes the form + * submisson from /src/main/webapp/tasks.jsp to add and delete tasks. + */ +public class TaskqueueServlet extends HttpServlet { + + private static final Logger log = Logger.getLogger(TaskqueueServlet.class.getName()); + private static final int numberOfTasksToAdd = 100; + private static final int numberOfTasksToLease = 100; + private String message; + + // Process the http POST of the form + @Override + public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, + ServletException { + if (req.getParameter("addTask") != null) { + String content = req.getParameter("content"); + String output = String.format("Adding %d Tasks to the Task Queue with a payload of '%s'", + numberOfTasksToAdd, content.toString()); + log.info(output.toString()); + + // Add Tasks to Task Queue + // [START get_queue] + Queue q = QueueFactory.getQueue("pull-queue"); + // [END get_queue] + for (int i = 0; i < numberOfTasksToAdd; i++) { + // [START add_tasks] + q.add(TaskOptions.Builder.withMethod(TaskOptions.Method.PULL) + .payload(content.toString())); + // [END add_tasks] + } + try { + message = "Added " + numberOfTasksToAdd + " tasks to the task queue."; + req.setAttribute("message", message); + req.getRequestDispatcher("tasks.jsp").forward(req,resp); + } catch (ServletException e) { + throw new ServletException("ServletException error: ", e); + } + } else if (req.getParameter("leaseTask") != null) { + String output = String.format("Pulling %d Tasks from the Task Queue", numberOfTasksToLease); + log.info(output.toString()); + + // Pull tasks from the Task Queue and process them + Queue q = QueueFactory.getQueue("pull-queue"); + // [START lease_tasks] + List tasks = q.leaseTasks(3600, TimeUnit.SECONDS, numberOfTasksToLease); + // [END lease_tasks] + String payload; + int numberOfDeletedTasks = 0; + for (TaskHandle task : tasks) { + payload = new String(task.getPayload()); + output = String.format("Processing: taskName='%s' payload='%s'", task.getName() + .toString(), payload.toString()); + log.info(output.toString()); + output = String.format("Deleting taskName='%s'", task.getName().toString()); + log.info(output.toString()); + q.deleteTask(task); + numberOfDeletedTasks++; + } + try { + if (numberOfDeletedTasks > 0) { + message = "Processed and deleted " + numberOfTasksToLease + " tasks from the " + + " task queue."; + } else { + message = "Task Queue has no tasks available for lease."; + } + req.setAttribute("message", message); + req.getRequestDispatcher("tasks.jsp").forward(req,resp); + } catch (ServletException e) { + throw new ServletException("ServletException error: ", e); + } + } else { + resp.sendRedirect("/"); + } + } +} diff --git a/appengine/taskqueue/pull/src/main/java/com/example/taskqueue/TaskqueueServlet.java-old b/appengine/taskqueue/pull/src/main/java/com/example/taskqueue/TaskqueueServlet.java-old new file mode 100644 index 00000000000..7d18358c45c --- /dev/null +++ b/appengine/taskqueue/pull/src/main/java/com/example/taskqueue/TaskqueueServlet.java-old @@ -0,0 +1,112 @@ +/** + * Copyright 2015 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.taskqueue; + +import com.google.appengine.api.taskqueue.TaskOptions; +import com.google.appengine.api.taskqueue.Queue; +import com.google.appengine.api.taskqueue.QueueFactory; +import com.google.appengine.api.taskqueue.TaskHandle; + +import java.io.IOException; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import java.util.Date; +import java.util.List; +import java.util.logging.Logger; +import java.util.concurrent.TimeUnit; + +/** + * Form Handling Servlet + * This servlet has one method + * {@link #doPost(<#HttpServletRequest req#>, <#HttpServletResponse resp#>)} which takes the form + * submisson from /src/main/webapp/tasks.jsp to add and delete tasks. + */ +public class TaskqueueServlet extends HttpServlet { + + private static final Logger log = Logger.getLogger(TaskqueueServlet.class.getName()); + private static final int numberOfTasksToAdd = 100; + private static final int numberOfTasksToLease = 100; + private String message; + + // Process the http POST of the form + @Override + public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, + ServletException { + if (req.getParameter("addTask") != null) { + String content = req.getParameter("content"); + String output = String.format("Adding %d Tasks to the Task Queue with a payload of '%s'", + numberOfTasksToAdd, content.toString()); + log.info(output.toString()); + + // Add Tasks to Task Queue + // [START get_queue] + Queue q = QueueFactory.getQueue("pull-queue"); + // [END get_queue] + for (int i = 0; i < numberOfTasksToAdd; i++) { + // [START add_tasks] + q.add(TaskOptions.Builder.withMethod(TaskOptions.Method.PULL) + .payload(content.toString())); + // [END add_tasks] + } + try { + message = "Added " + numberOfTasksToAdd + " tasks to the task queue."; + req.setAttribute("message", message); + req.getRequestDispatcher("tasks.jsp").forward(req,resp); + } catch (ServletException e) { + throw new ServletException("ServletException error: ", e); + } + } else if (req.getParameter("leaseTask") != null) { + String output = String.format("Pulling %d Tasks from the Task Queue", numberOfTasksToLease); + log.info(output.toString()); + + // Pull tasks from the Task Queue and process them + Queue q = QueueFactory.getQueue("pull-queue"); + // [START lease_tasks] + List tasks = q.leaseTasks(3600, TimeUnit.SECONDS, numberOfTasksToLease); + // [END lease_tasks] + String payload; + int numberOfDeletedTasks = 0; + for (TaskHandle task : tasks) { + payload = new String(task.getPayload()); + output = String.format("Processing: taskName='%s' payload='%s'", task.getName() + .toString(), payload.toString()); + log.info(output.toString()); + output = String.format("Deleting taskName='%s'", task.getName().toString()); + log.info(output.toString()); + q.deleteTask(task); + numberOfDeletedTasks++; + } + try { + if (numberOfDeletedTasks > 0) { + message = "Processed and deleted " + numberOfTasksToLease + " tasks from the " + + " task queue."; + } else { + message = "Task Queue has no tasks available for lease."; + } + req.setAttribute("message", message); + req.getRequestDispatcher("tasks.jsp").forward(req,resp); + } catch (ServletException e) { + throw new ServletException("ServletException error: ", e); + } + } else { + resp.sendRedirect("/"); + } + } +} diff --git a/appengine/taskqueue/pull/src/main/java/com/example/taskqueue/TaskqueueServlet.java.save b/appengine/taskqueue/pull/src/main/java/com/example/taskqueue/TaskqueueServlet.java.save new file mode 100644 index 00000000000..263caea5455 --- /dev/null +++ b/appengine/taskqueue/pull/src/main/java/com/example/taskqueue/TaskqueueServlet.java.save @@ -0,0 +1,138 @@ +/** + * Copyright 2015 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.taskqueue; + +import com.google.appengine.api.taskqueue.TaskOptions; +import com.google.appengine.api.taskqueue.Queue; +import com.google.appengine.api.taskqueue.QueueFactory; +import com.google.appengine.api.taskqueue.TaskHandle; + +import java.io.IOException; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import java.util.Date; +import java.util.List; +import java.util.logging.Logger; +import java.util.concurrent.TimeUnit; + +/** + * Form Handling Servlet + * This servlet has one method + * {@link #doPost(<#HttpServletRequest req#>, <#HttpServletResponse resp#>)} which takes the form + * submisson from /src/main/webapp/tasks.jsp to add and delete tasks. + */ +public class TaskqueueServlet extends HttpServlet { + + private static final Logger log = Logger.getLogger(TaskqueueServlet.class.getName()); + private static final int numberOfTasksToAdd = 100; + private static final int numberOfTasksToLease = 100; + private static String output; + private static String message; + + // Set useTaggedTasks = true to try out tasks with tags + private static boolean useTaggedTasks = false; + + // Process the http POST of the form + @Override + public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, + ServletException { + if (req.getParameter("addTask") != null) { + String content = req.getParameter("content"); + String output = String.format("Adding %d Tasks to the Task Queue with a payload of '%s'", + numberOfTasksToAdd, content.toString()); + log.info(output.toString()); + + // Add Tasks to Task Queue + // [START get_queue] + Queue q = QueueFactory.getQueue("pull-queue"); + // [END get_queue] + if (!useTaggedTasks) { + for (int i = 0; i < numberOfTasksToAdd; i++) { + // [START add_task] + q.add(TaskOptions.Builder.withMethod(TaskOptions.Method.PULL) + .payload(content.toString())); + // [END add_task] + } + } else { + for (int i = 0; i < numberOfTasksToAdd; i++) { + // [START add_task_w_tag] + q.add(TaskOptions.Builder.withMethod(TaskOptions.Method.PULL) + .payload(content.toString()) + .tag("process".getBytes())); + // [END add_task_w_tag] + + } + } + try { + message = "Added " + numberOfTasksToAdd + " tasks to the task queue."; + req.setAttribute("message", message); + req.getRequestDispatcher("tasks.jsp").forward(req,resp); + } catch (ServletException e) { + throw new ServletException("ServletException error: ", e); + } + } else if (req.getParameter("leaseTask") != null) { + output = String.format("Pulling %d Tasks from the Task Queue", numberOfTasksToLease); + log.info(output.toString()); + + // Pull tasks from the Task Queue and process them + Queue q = QueueFactory.getQueue("pull-queue"); + if (!useTaggedTasks) { + // [START lease_tasks] + List tasks = q.leaseTasks(3600, TimeUnit.SECONDS, numberOfTasksToLease); + // [END lease_tasks] + message = processTasks(tasks, q); + } else { + // [START lease_tasks_by_tag] + // Lease only tasks tagged with "process" + List tasks = q.leaseTasksByTag(3600, TimeUnit.SECONDS, numberOfTasksToLease, "process"); + // You can also specify a tag to lease via LeaseOptions passed to leaseTasks. + // [END lease_tasks_by_tag] + message = processTasks(tasks, q); + } + req.setAttribute("message", message); + req.getRequestDispatcher("tasks.jsp").forward(req,resp); + } else { + resp.sendRedirect("/"); + } + } + + //Method to process and delete tasks + private static String processTasks(List tasks, Queue q) { + String payload; + int numberOfDeletedTasks = 0; + for (TaskHandle task : tasks) { + payload = new String(task.getPayload()); + output = String.format("Processing: taskName='%s' payload='%s'", task.getName() + .toString(), payload.toString()); + log.info(output.toString()); + output = String.format("Deleting taskName='%s'", task.getName().toString()); + log.info(output.toString()); + q.deleteTask(task); + numberOfDeletedTasks++; + } + if (numberOfDeletedTasks > 0) { + message = "Processed and deleted " + numberOfTasksToLease + " tasks from the " + + " task queue."; + } else { + message = "Task Queue has no tasks available for lease."; + } + return message; + } +} diff --git a/appengine/taskqueue/pull/src/main/webapp/WEB-INF/appengine-web.xml b/appengine/taskqueue/pull/src/main/webapp/WEB-INF/appengine-web.xml new file mode 100644 index 00000000000..c9e245399bf --- /dev/null +++ b/appengine/taskqueue/pull/src/main/webapp/WEB-INF/appengine-web.xml @@ -0,0 +1,6 @@ + + + YOUR-PROJECT-ID + YOUR-VERSION-ID + true + diff --git a/appengine/taskqueue/pull/src/main/webapp/WEB-INF/logging.properties b/appengine/taskqueue/pull/src/main/webapp/WEB-INF/logging.properties new file mode 100644 index 00000000000..19e9bf064c1 --- /dev/null +++ b/appengine/taskqueue/pull/src/main/webapp/WEB-INF/logging.properties @@ -0,0 +1,13 @@ +# A default java.util.logging configuration. +# (All App Engine logging is through java.util.logging by default). +# +# To use this configuration, copy it into your application's WEB-INF +# folder and add the following to your appengine-web.xml: +# +# +# +# +# + +# Set the default logging level for all loggers to WARNING +.level = INFO diff --git a/appengine/taskqueue/pull/src/main/webapp/WEB-INF/queue.xml b/appengine/taskqueue/pull/src/main/webapp/WEB-INF/queue.xml new file mode 100644 index 00000000000..6653b5ad7c7 --- /dev/null +++ b/appengine/taskqueue/pull/src/main/webapp/WEB-INF/queue.xml @@ -0,0 +1,10 @@ + + + pull-queue + pull + + bar@foo.com + bar@foo.com + + + diff --git a/appengine/taskqueue/pull/src/main/webapp/WEB-INF/web.xml b/appengine/taskqueue/pull/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 00000000000..1ed2ec81871 --- /dev/null +++ b/appengine/taskqueue/pull/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,19 @@ + + + + taskqueue + com.example.taskqueue.TaskqueueServlet + + + + taskqueue + /taskqueue + + + + tasks.jsp + + diff --git a/appengine/taskqueue/pull/src/main/webapp/tasks.jsp b/appengine/taskqueue/pull/src/main/webapp/tasks.jsp new file mode 100644 index 00000000000..33f0d6926a8 --- /dev/null +++ b/appengine/taskqueue/pull/src/main/webapp/tasks.jsp @@ -0,0 +1,36 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> +<%@ page import="java.util.List" %> +<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> + + + + + + + + +
+
+
+
+
+
+
+
+ + + + + From c65494aad412c6775668ffa6f73471c1da137327 Mon Sep 17 00:00:00 2001 From: Jonathan Simon Date: Wed, 27 Apr 2016 10:24:15 -0700 Subject: [PATCH 2/7] Remove artifact files --- .../taskqueue/TaskqueueServlet.java-hold | 112 -------------- .../taskqueue/TaskqueueServlet.java-old | 112 -------------- .../taskqueue/TaskqueueServlet.java.save | 138 ------------------ 3 files changed, 362 deletions(-) delete mode 100644 appengine/taskqueue/pull/src/main/java/com/example/taskqueue/TaskqueueServlet.java-hold delete mode 100644 appengine/taskqueue/pull/src/main/java/com/example/taskqueue/TaskqueueServlet.java-old delete mode 100644 appengine/taskqueue/pull/src/main/java/com/example/taskqueue/TaskqueueServlet.java.save diff --git a/appengine/taskqueue/pull/src/main/java/com/example/taskqueue/TaskqueueServlet.java-hold b/appengine/taskqueue/pull/src/main/java/com/example/taskqueue/TaskqueueServlet.java-hold deleted file mode 100644 index 7d18358c45c..00000000000 --- a/appengine/taskqueue/pull/src/main/java/com/example/taskqueue/TaskqueueServlet.java-hold +++ /dev/null @@ -1,112 +0,0 @@ -/** - * Copyright 2015 Google Inc. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.example.taskqueue; - -import com.google.appengine.api.taskqueue.TaskOptions; -import com.google.appengine.api.taskqueue.Queue; -import com.google.appengine.api.taskqueue.QueueFactory; -import com.google.appengine.api.taskqueue.TaskHandle; - -import java.io.IOException; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import java.util.Date; -import java.util.List; -import java.util.logging.Logger; -import java.util.concurrent.TimeUnit; - -/** - * Form Handling Servlet - * This servlet has one method - * {@link #doPost(<#HttpServletRequest req#>, <#HttpServletResponse resp#>)} which takes the form - * submisson from /src/main/webapp/tasks.jsp to add and delete tasks. - */ -public class TaskqueueServlet extends HttpServlet { - - private static final Logger log = Logger.getLogger(TaskqueueServlet.class.getName()); - private static final int numberOfTasksToAdd = 100; - private static final int numberOfTasksToLease = 100; - private String message; - - // Process the http POST of the form - @Override - public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, - ServletException { - if (req.getParameter("addTask") != null) { - String content = req.getParameter("content"); - String output = String.format("Adding %d Tasks to the Task Queue with a payload of '%s'", - numberOfTasksToAdd, content.toString()); - log.info(output.toString()); - - // Add Tasks to Task Queue - // [START get_queue] - Queue q = QueueFactory.getQueue("pull-queue"); - // [END get_queue] - for (int i = 0; i < numberOfTasksToAdd; i++) { - // [START add_tasks] - q.add(TaskOptions.Builder.withMethod(TaskOptions.Method.PULL) - .payload(content.toString())); - // [END add_tasks] - } - try { - message = "Added " + numberOfTasksToAdd + " tasks to the task queue."; - req.setAttribute("message", message); - req.getRequestDispatcher("tasks.jsp").forward(req,resp); - } catch (ServletException e) { - throw new ServletException("ServletException error: ", e); - } - } else if (req.getParameter("leaseTask") != null) { - String output = String.format("Pulling %d Tasks from the Task Queue", numberOfTasksToLease); - log.info(output.toString()); - - // Pull tasks from the Task Queue and process them - Queue q = QueueFactory.getQueue("pull-queue"); - // [START lease_tasks] - List tasks = q.leaseTasks(3600, TimeUnit.SECONDS, numberOfTasksToLease); - // [END lease_tasks] - String payload; - int numberOfDeletedTasks = 0; - for (TaskHandle task : tasks) { - payload = new String(task.getPayload()); - output = String.format("Processing: taskName='%s' payload='%s'", task.getName() - .toString(), payload.toString()); - log.info(output.toString()); - output = String.format("Deleting taskName='%s'", task.getName().toString()); - log.info(output.toString()); - q.deleteTask(task); - numberOfDeletedTasks++; - } - try { - if (numberOfDeletedTasks > 0) { - message = "Processed and deleted " + numberOfTasksToLease + " tasks from the " + - " task queue."; - } else { - message = "Task Queue has no tasks available for lease."; - } - req.setAttribute("message", message); - req.getRequestDispatcher("tasks.jsp").forward(req,resp); - } catch (ServletException e) { - throw new ServletException("ServletException error: ", e); - } - } else { - resp.sendRedirect("/"); - } - } -} diff --git a/appengine/taskqueue/pull/src/main/java/com/example/taskqueue/TaskqueueServlet.java-old b/appengine/taskqueue/pull/src/main/java/com/example/taskqueue/TaskqueueServlet.java-old deleted file mode 100644 index 7d18358c45c..00000000000 --- a/appengine/taskqueue/pull/src/main/java/com/example/taskqueue/TaskqueueServlet.java-old +++ /dev/null @@ -1,112 +0,0 @@ -/** - * Copyright 2015 Google Inc. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.example.taskqueue; - -import com.google.appengine.api.taskqueue.TaskOptions; -import com.google.appengine.api.taskqueue.Queue; -import com.google.appengine.api.taskqueue.QueueFactory; -import com.google.appengine.api.taskqueue.TaskHandle; - -import java.io.IOException; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import java.util.Date; -import java.util.List; -import java.util.logging.Logger; -import java.util.concurrent.TimeUnit; - -/** - * Form Handling Servlet - * This servlet has one method - * {@link #doPost(<#HttpServletRequest req#>, <#HttpServletResponse resp#>)} which takes the form - * submisson from /src/main/webapp/tasks.jsp to add and delete tasks. - */ -public class TaskqueueServlet extends HttpServlet { - - private static final Logger log = Logger.getLogger(TaskqueueServlet.class.getName()); - private static final int numberOfTasksToAdd = 100; - private static final int numberOfTasksToLease = 100; - private String message; - - // Process the http POST of the form - @Override - public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, - ServletException { - if (req.getParameter("addTask") != null) { - String content = req.getParameter("content"); - String output = String.format("Adding %d Tasks to the Task Queue with a payload of '%s'", - numberOfTasksToAdd, content.toString()); - log.info(output.toString()); - - // Add Tasks to Task Queue - // [START get_queue] - Queue q = QueueFactory.getQueue("pull-queue"); - // [END get_queue] - for (int i = 0; i < numberOfTasksToAdd; i++) { - // [START add_tasks] - q.add(TaskOptions.Builder.withMethod(TaskOptions.Method.PULL) - .payload(content.toString())); - // [END add_tasks] - } - try { - message = "Added " + numberOfTasksToAdd + " tasks to the task queue."; - req.setAttribute("message", message); - req.getRequestDispatcher("tasks.jsp").forward(req,resp); - } catch (ServletException e) { - throw new ServletException("ServletException error: ", e); - } - } else if (req.getParameter("leaseTask") != null) { - String output = String.format("Pulling %d Tasks from the Task Queue", numberOfTasksToLease); - log.info(output.toString()); - - // Pull tasks from the Task Queue and process them - Queue q = QueueFactory.getQueue("pull-queue"); - // [START lease_tasks] - List tasks = q.leaseTasks(3600, TimeUnit.SECONDS, numberOfTasksToLease); - // [END lease_tasks] - String payload; - int numberOfDeletedTasks = 0; - for (TaskHandle task : tasks) { - payload = new String(task.getPayload()); - output = String.format("Processing: taskName='%s' payload='%s'", task.getName() - .toString(), payload.toString()); - log.info(output.toString()); - output = String.format("Deleting taskName='%s'", task.getName().toString()); - log.info(output.toString()); - q.deleteTask(task); - numberOfDeletedTasks++; - } - try { - if (numberOfDeletedTasks > 0) { - message = "Processed and deleted " + numberOfTasksToLease + " tasks from the " + - " task queue."; - } else { - message = "Task Queue has no tasks available for lease."; - } - req.setAttribute("message", message); - req.getRequestDispatcher("tasks.jsp").forward(req,resp); - } catch (ServletException e) { - throw new ServletException("ServletException error: ", e); - } - } else { - resp.sendRedirect("/"); - } - } -} diff --git a/appengine/taskqueue/pull/src/main/java/com/example/taskqueue/TaskqueueServlet.java.save b/appengine/taskqueue/pull/src/main/java/com/example/taskqueue/TaskqueueServlet.java.save deleted file mode 100644 index 263caea5455..00000000000 --- a/appengine/taskqueue/pull/src/main/java/com/example/taskqueue/TaskqueueServlet.java.save +++ /dev/null @@ -1,138 +0,0 @@ -/** - * Copyright 2015 Google Inc. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.example.taskqueue; - -import com.google.appengine.api.taskqueue.TaskOptions; -import com.google.appengine.api.taskqueue.Queue; -import com.google.appengine.api.taskqueue.QueueFactory; -import com.google.appengine.api.taskqueue.TaskHandle; - -import java.io.IOException; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import java.util.Date; -import java.util.List; -import java.util.logging.Logger; -import java.util.concurrent.TimeUnit; - -/** - * Form Handling Servlet - * This servlet has one method - * {@link #doPost(<#HttpServletRequest req#>, <#HttpServletResponse resp#>)} which takes the form - * submisson from /src/main/webapp/tasks.jsp to add and delete tasks. - */ -public class TaskqueueServlet extends HttpServlet { - - private static final Logger log = Logger.getLogger(TaskqueueServlet.class.getName()); - private static final int numberOfTasksToAdd = 100; - private static final int numberOfTasksToLease = 100; - private static String output; - private static String message; - - // Set useTaggedTasks = true to try out tasks with tags - private static boolean useTaggedTasks = false; - - // Process the http POST of the form - @Override - public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, - ServletException { - if (req.getParameter("addTask") != null) { - String content = req.getParameter("content"); - String output = String.format("Adding %d Tasks to the Task Queue with a payload of '%s'", - numberOfTasksToAdd, content.toString()); - log.info(output.toString()); - - // Add Tasks to Task Queue - // [START get_queue] - Queue q = QueueFactory.getQueue("pull-queue"); - // [END get_queue] - if (!useTaggedTasks) { - for (int i = 0; i < numberOfTasksToAdd; i++) { - // [START add_task] - q.add(TaskOptions.Builder.withMethod(TaskOptions.Method.PULL) - .payload(content.toString())); - // [END add_task] - } - } else { - for (int i = 0; i < numberOfTasksToAdd; i++) { - // [START add_task_w_tag] - q.add(TaskOptions.Builder.withMethod(TaskOptions.Method.PULL) - .payload(content.toString()) - .tag("process".getBytes())); - // [END add_task_w_tag] - - } - } - try { - message = "Added " + numberOfTasksToAdd + " tasks to the task queue."; - req.setAttribute("message", message); - req.getRequestDispatcher("tasks.jsp").forward(req,resp); - } catch (ServletException e) { - throw new ServletException("ServletException error: ", e); - } - } else if (req.getParameter("leaseTask") != null) { - output = String.format("Pulling %d Tasks from the Task Queue", numberOfTasksToLease); - log.info(output.toString()); - - // Pull tasks from the Task Queue and process them - Queue q = QueueFactory.getQueue("pull-queue"); - if (!useTaggedTasks) { - // [START lease_tasks] - List tasks = q.leaseTasks(3600, TimeUnit.SECONDS, numberOfTasksToLease); - // [END lease_tasks] - message = processTasks(tasks, q); - } else { - // [START lease_tasks_by_tag] - // Lease only tasks tagged with "process" - List tasks = q.leaseTasksByTag(3600, TimeUnit.SECONDS, numberOfTasksToLease, "process"); - // You can also specify a tag to lease via LeaseOptions passed to leaseTasks. - // [END lease_tasks_by_tag] - message = processTasks(tasks, q); - } - req.setAttribute("message", message); - req.getRequestDispatcher("tasks.jsp").forward(req,resp); - } else { - resp.sendRedirect("/"); - } - } - - //Method to process and delete tasks - private static String processTasks(List tasks, Queue q) { - String payload; - int numberOfDeletedTasks = 0; - for (TaskHandle task : tasks) { - payload = new String(task.getPayload()); - output = String.format("Processing: taskName='%s' payload='%s'", task.getName() - .toString(), payload.toString()); - log.info(output.toString()); - output = String.format("Deleting taskName='%s'", task.getName().toString()); - log.info(output.toString()); - q.deleteTask(task); - numberOfDeletedTasks++; - } - if (numberOfDeletedTasks > 0) { - message = "Processed and deleted " + numberOfTasksToLease + " tasks from the " + - " task queue."; - } else { - message = "Task Queue has no tasks available for lease."; - } - return message; - } -} From b4e1c7b95cc0eaa12377cbabdb0c33e5bee24386 Mon Sep 17 00:00:00 2001 From: Jonathan Simon Date: Wed, 27 Apr 2016 10:33:45 -0700 Subject: [PATCH 3/7] Update README.md --- appengine/taskqueue/pull/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/appengine/taskqueue/pull/README.md b/appengine/taskqueue/pull/README.md index 796acd839a9..c15d0363dc0 100644 --- a/appengine/taskqueue/pull/README.md +++ b/appengine/taskqueue/pull/README.md @@ -1,4 +1,4 @@ -# Pull task Queue sample for Google App Engine +# Pull Task Queue sample for Google App Engine This sample demonstrates how to use [pull task queues][appid] on [Google App Engine][ae-docs]. From 442536d32b74694a4c0ae13999c64d267934f66f Mon Sep 17 00:00:00 2001 From: Jonathan Simon Date: Wed, 27 Apr 2016 10:55:03 -0700 Subject: [PATCH 4/7] Adding Task Queue API REST Sample --- taskqueue/pull/README.md | 97 +++++++++ taskqueue/pull/TaskQueueSample.launch | 14 ++ taskqueue/pull/logging.properties | 9 + taskqueue/pull/pom.xml | 95 +++++++++ .../pull/src/main/java/TaskQueueSample.java | 195 ++++++++++++++++++ 5 files changed, 410 insertions(+) create mode 100644 taskqueue/pull/README.md create mode 100644 taskqueue/pull/TaskQueueSample.launch create mode 100644 taskqueue/pull/logging.properties create mode 100644 taskqueue/pull/pom.xml create mode 100644 taskqueue/pull/src/main/java/TaskQueueSample.java diff --git a/taskqueue/pull/README.md b/taskqueue/pull/README.md new file mode 100644 index 00000000000..8abbe8f34ed --- /dev/null +++ b/taskqueue/pull/README.md @@ -0,0 +1,97 @@ +# Pull Task Queue REST API sample +This sample command line application demonstrates how to access App Engine pull task queues using the Task +Queue REST API and the Google Java API Client Library. + +Important Note: This sample requires an existing App Engine Pull Task Queue. +Deploy this +[sample App Engine pull task queue app](https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/appengine/taskqueue/pull) +to create one first and then come back and run this sample. + +## Setup Instructions +1. Create or select a project in the Google Cloud Console: + 1. Visit the [Cloud Console][2] + 1. If this is your first time then click "Create Project," otherwise you can +reuse an existing project by clicking on it. + 1. Note: You will need to enable billing for the project to use Compute + Engine. + 1. Click "Overview" in the left-side navigation menu and copy your Project ID + for use in step 3.3 below. + +1. Authentication instructions to run the sample (on your local machine or on a Compute Engine VM): + * Running the sample locally on your development machine: + 1. Install [Google Cloud SDK](https://cloud.google.com/sdk/) + 1. Run the following command to authorize the Cloud SDK and configure your project: +
gcloud init
+ 1. Add your authenticated email account as <user-email> and <writer-email> elements to the queue.xml file of the App Engine app that created the pull queue task you're trying to access. For more details, please see + [Defining Pull Queues](https://cloud.google.com/appengine/docs/java/config/queue#Defining_Pull_Queues) + on the Task Queue configuration page. + * Running the sample on a Google Compute Engine VM using Default Application Credentials: + 1. Create a service account and add it to queue.xml + 1. In the API Manager > [Credentials](https://pantheon.corp.google.com/apis/credentials) + section click "Create credentials" and choose "Service account key". + 1. On the "Create service account key" page, select "Compute Engine default service account" from the "Service Account" drop-down menu. Leave the Key type set to JSON and click the "Create" button. + 1. Once the service account is created, click the "Manage service accounts" link and copy the "Service account ID" of the "Compute Engine default service account". + 1. Add the "Service account ID" as <user-email> and <writer-email> elements to the queue.xml file of the App Engine app that created the pull queue task you're trying to access. For more details, please see + [Defining Pull Queues](https://cloud.google.com/appengine/docs/java/config/queue#Defining_Pull_Queues) + on the Task Queue configuration page. + + 1. Create a Compute Engine VM Instance. + 1. In the [Cloud Console](https://console.cloud.google.com/project) + go to the Compute > Compute Engine section. + 1. Click the "Create instance" button. + 1. For the 'Boot Disk' select a Linux machine image like Debian or Ubuntu. + 1. In the "Indentity API and Access" section, select "Set access for each API" under the "Access Scopes" subsection and then select the + "Task queue" drop-down menu to set its scope. + * Set the "Task queue" access scope to be "Enabled". + 1. Click the "Create" button. + 1. Once the VM is created click the VM instance's "SSH" button to ssh in to the newly created VM instance. + +1. Code checkout instructions: + 1. Prerequisites: install [Java 7 or Java 8 JDK][1], [Git][3], and [Maven][4]. +You may need to set your `JAVA_HOME` environment variable as well. + * To install these prerequisites on a Linux (Debian or Ubuntu) based Compute Engine VM + instance, run these commands: +
+    sudo apt-get update
+    sudo apt-get install git maven openjdk-7-jdk -y
+    
+ 1. Download the sample code by running the following commands: +
mkdir some_directory
+  cd some_directory
+  git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
+  cd java-docs-samples/taskqueue/cmdline
+  
+ In a text editor open the `TaskQueueSample.java` file. For example, to edit the file with nano: +
nano src/main/java/TaskQueueSample.java
+ + 1. Specify an 'Application Name' for your app by updating the following line of code: +
private static final String APPLICATION_NAME = "";
+ 1. Save the changes to the file and exit the text editor. + +1. Compile and run the sample: + 1. Compile the sample code using Maven by running the following command: +
mvn compile
+ 1. Execute the sample code using Maven by running the following command, + entering your specific values for the placeholder arguments: +
mvn -q exec:java -Dexec.args="<ProjectId> <TaskQueueName> <LeaseSeconds> <NumberOfTasksToLease>"
+ 1. Running the sample will first list the pull task queue details and then it will lease, process and delete the number of tasks you specified. + + You can verify the details of the pull task queue and the leased tasks by visiting the [App Engine > Task queues](https://pantheon.corp.google.com/appengine/taskqueues) + section of the Developers Console. +1. Importing the code into Eclipse and running it from there: + 1. Prerequisites: install [Eclipse][5] and the [Maven plugin for Eclipse][6]. + 1. Download code as specified above. + 1. File -> Import -> Maven -> Existing Maven Projects -> Next. + 1. Select your project directory as your "Root Directory," and click "Finish." + 1. Right-click on project task-queue-rest-sample. + 1. Run As > Java Application. + 1. If asked, type or select "TaskQueueSample" and click OK. + 1. Application output will display in the Eclipse Console. + +[1]: http://java.com/en/download/faq/develop.xml +[2]: https://console.cloud.google.com/project +[3]: http://git-scm.com/downloads +[4]: http://maven.apache.org/download.html +[5]: http://www.eclipse.org/downloads/ +[6]: http://download.eclipse.org/technology/m2e/releases/ + diff --git a/taskqueue/pull/TaskQueueSample.launch b/taskqueue/pull/TaskQueueSample.launch new file mode 100644 index 00000000000..999114392c4 --- /dev/null +++ b/taskqueue/pull/TaskQueueSample.launch @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + diff --git a/taskqueue/pull/logging.properties b/taskqueue/pull/logging.properties new file mode 100644 index 00000000000..4d8490a7c71 --- /dev/null +++ b/taskqueue/pull/logging.properties @@ -0,0 +1,9 @@ + +# Properties file which configures the operation of the JDK logging facility. +# The system will look for this config file to be specified as a system property: +# -Djava.util.logging.config.file=${project_loc:task-queue-rest-sample}/logging.properties +# Set up the console handler (uncomment "level" to show more fine-grained messages) +handlers = java.util.logging.ConsoleHandler +#java.util.logging.ConsoleHandler.level = CONFIG +# Set up logging of HTTP requests and responses (uncomment "level" to show) +#com.google.api.client.http.level = CONFIG diff --git a/taskqueue/pull/pom.xml b/taskqueue/pull/pom.xml new file mode 100644 index 00000000000..dffcdf88190 --- /dev/null +++ b/taskqueue/pull/pom.xml @@ -0,0 +1,95 @@ + + + 4.0.0 + com.google.cloud.samples + taskqueue + 1 + + + com.google.cloud + doc-samples + 1.0.0 + ../.. + + + + + maven-compiler-plugin + 3.5.1 + + 1.7 + 1.7 + + + + org.codehaus.mojo + exec-maven-plugin + 1.4.0 + + + + java + + + + + TaskQueueSample + + + java.util.logging.config.file + logging.properties + + + + + + org.codehaus.mojo + findbugs-maven-plugin + 3.0.3 + + false + + + + + check + + + + + + ${project.artifactId}-${project.version} + + + + com.google.api-client + google-api-client + 1.21.0 + + + com.google.apis + google-api-services-taskqueue + v1beta2-rev40-1.21.0 + + + commons-codec + commons-codec + 1.10 + + + + v1-rev105-1.21.0 + UTF-8 + + diff --git a/taskqueue/pull/src/main/java/TaskQueueSample.java b/taskqueue/pull/src/main/java/TaskQueueSample.java new file mode 100644 index 00000000000..a13f03f62b4 --- /dev/null +++ b/taskqueue/pull/src/main/java/TaskQueueSample.java @@ -0,0 +1,195 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [START import_libraries] +import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.http.HttpTransport; +// [END import_libraries] +import com.google.api.client.json.JsonFactory; +import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.services.taskqueue.Taskqueue; +import com.google.api.services.taskqueue.TaskqueueRequest; +import com.google.api.services.taskqueue.TaskqueueRequestInitializer; +import com.google.api.services.taskqueue.TaskqueueScopes; +import com.google.api.services.taskqueue.model.Task; +import com.google.api.services.taskqueue.model.Tasks; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import org.apache.commons.codec.binary.Base64; +/** + * Command-line sample which leases tasks from a pull task queue, processes the payload + * of the task and then deletes the task. + */ +public class TaskQueueSample { + /** + * Be sure to specify the name of your application. If the application name is {@code null} or + * blank, the application will log a warning. Suggested format is "MyCompany-ProductName/1.0". + */ + private static final String APPLICATION_NAME = ""; + private static String projectId; + private static String taskQueueName; + private static int leaseSecs; + private static int numTasks; + /** Global instance of the HTTP transport. */ + // [START intialize_transport] + private static HttpTransport httpTransport; + // [END intialize_transport] + /** Global instance of the JSON factory. */ + private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); + private static void run() throws Exception { + httpTransport = GoogleNetHttpTransport.newTrustedTransport(); + // [START auth_and_intitalize_client] + // Authenticate using Google Application Default Credentials. + GoogleCredential credential = GoogleCredential.getApplicationDefault(); + if (credential.createScopedRequired()) { + List scopes = new ArrayList<>(); + // Set TaskQueue Scopes + scopes.add(TaskqueueScopes.TASKQUEUE); + scopes.add(TaskqueueScopes.TASKQUEUE_CONSUMER); + credential = credential.createScoped(scopes); + } + // Intialize Taskqueue object. + Taskqueue taskQueue = + new Taskqueue.Builder(httpTransport, JSON_FACTORY, credential) + .setApplicationName(APPLICATION_NAME) + .build(); + // [END auth_and_intitalize_client] + + /* Get the task queue using the name of an existing task queue + * listed in the App Engine Task Queue section of the Developers console. + * See the following sample for an example App Engine app that creates a + * pull task queue: + * https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/appengine/taskqueue + */ + com.google.api.services.taskqueue.model.TaskQueue queue = getQueue(taskQueue); + System.out.println("================== Listing Task Queue details =================="); + System.out.println(queue); + // Lease, process and delete tasks + // [START lease_tasks] + Tasks tasks = getLeasedTasks(taskQueue); + if (tasks.getItems() == null || tasks.getItems().size() == 0) { + System.out.println("No tasks to lease, so now exiting"); + } else { + for (Task leasedTask : tasks.getItems()) { + processTask(leasedTask); + deleteTask(taskQueue, leasedTask); + } + } + // [END lease_tasks] + } + + public static boolean parseParams(String[] args) { + try { + projectId = args[0]; + taskQueueName = args[1]; + leaseSecs = Integer.parseInt(args[2]); + numTasks = Integer.parseInt(args[3]); + return true; + } catch (ArrayIndexOutOfBoundsException ae) { + System.out.println("Insufficient Arguments"); + return false; + } catch (NumberFormatException ae) { + System.out.println("Please specify lease seconds " + + "and Number of tasks to lease, in number " + + "format"); + return false; + } + } + public static void printUsage() { + System.out.println("mvn -q exec:java -Dexec.args=\"" + + " " + + " \""); + } + public static void main(String[] args) { + if (args.length != 4) { + System.out.println("Insuficient arguments"); + printUsage(); + System.exit(1); + } else if (!parseParams(args)) { + printUsage(); + System.exit(1); + } + try { + run(); + // success! + return; + } catch (IOException e) { + System.err.println(e.getMessage()); + } catch (Throwable t) { + t.printStackTrace(); + } + System.exit(1); + } + /** + * Method that sends a get request to get the queue. + * + * @param taskQueue The task queue that should be used to get the queue from. + * @return {@link com.google.api.services.taskqueue.model.TaskQueue} + * @throws IOException if the request fails. + */ + private static com.google.api.services.taskqueue.model.TaskQueue getQueue(Taskqueue taskQueue) + throws IOException { + //[START get_rest_queue] + Taskqueue.Taskqueues.Get request = taskQueue.taskqueues().get(projectId, taskQueueName); + request.setGetStats(true); + return request.execute(); + // [END get_rest_queue] + } + /** + * Method that sends a lease request to the specified task queue. + * + * @param taskQueue The task queue that should be used to lease tasks from. + * @return {@link Tasks} + * @throws IOException if the request fails. + */ + private static Tasks getLeasedTasks(Taskqueue taskQueue) throws IOException { + Taskqueue.Tasks.Lease leaseRequest = + taskQueue.tasks().lease(projectId, taskQueueName, numTasks, leaseSecs); + return leaseRequest.execute(); + } + /** + * This method actually performs the desired work on tasks. It can make use of payload of the + * task. By default, we are just printing the payload of the leased task after converting + * it from Base64 encoding. + * + * @param task The task that should be executed. + */ + private static void processTask(Task task) { + byte[] payload = Base64.decodeBase64(task.getPayloadBase64()); + System.out.println("Payload for the task:"); + System.out.println(new String(payload)); + } + /** + * Method that sends a delete request for the specified task object to the taskqueue service. + * + * @param taskQueue The task queue the specified task lies in. + * @param task The task that should be deleted. + * @throws IOException if the request fails + */ + private static void deleteTask(Taskqueue taskQueue, Task task) throws IOException { + System.out.println("Deleting task:" + task.getId()); + // [START delete_task] + String DeletePrefix = "s~"; + String projectIdFormattedForDelete = String.format("%s%s", DeletePrefix, projectId); + Taskqueue.Tasks.Delete request = + taskQueue.tasks().delete(projectIdFormattedForDelete, taskQueueName, task.getId()); + request.execute(); + // [END delete_task] + } +} From 5e3de3930470caf7e881870cf3f57a3fa76071fb Mon Sep 17 00:00:00 2001 From: Jonathan Simon Date: Wed, 27 Apr 2016 13:52:16 -0700 Subject: [PATCH 5/7] Updating pom.xml and adding licenses --- .../DevAppServer.launch | 14 ----- .../UpdateApplication.launch | 14 ----- appengine/taskqueue/pull/nbactions.xml | 55 ------------------- 3 files changed, 83 deletions(-) delete mode 100644 appengine/taskqueue/pull/eclipse-launch-profiles/DevAppServer.launch delete mode 100644 appengine/taskqueue/pull/eclipse-launch-profiles/UpdateApplication.launch delete mode 100644 appengine/taskqueue/pull/nbactions.xml diff --git a/appengine/taskqueue/pull/eclipse-launch-profiles/DevAppServer.launch b/appengine/taskqueue/pull/eclipse-launch-profiles/DevAppServer.launch deleted file mode 100644 index 2290cd0cea4..00000000000 --- a/appengine/taskqueue/pull/eclipse-launch-profiles/DevAppServer.launch +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/appengine/taskqueue/pull/eclipse-launch-profiles/UpdateApplication.launch b/appengine/taskqueue/pull/eclipse-launch-profiles/UpdateApplication.launch deleted file mode 100644 index caa7ec79dbd..00000000000 --- a/appengine/taskqueue/pull/eclipse-launch-profiles/UpdateApplication.launch +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/appengine/taskqueue/pull/nbactions.xml b/appengine/taskqueue/pull/nbactions.xml deleted file mode 100644 index cd6020b1ee3..00000000000 --- a/appengine/taskqueue/pull/nbactions.xml +++ /dev/null @@ -1,55 +0,0 @@ -#set( $symbol_pound = '#' ) -#set( $symbol_dollar = '$' ) -#set( $symbol_escape = '\' ) - - - - CUSTOM-appengine:devserver - appengine:devserver - - appengine:devserver - - - - CUSTOM-appengine:update - appengine:update - - appengine:update - - - - CUSTOM-appengine:rollback - appengine:rollback - - appengine:rollback - - - - CUSTOM-appengine:update_cron - appengine:update_cron - - appengine:update_cron - - - - CUSTOM-appengine:update_dos - appengine:update_dos - - appengine:update_dos - - - - CUSTOM-appengine:update_indexes - appengine:update_indexes - - appengine:update_indexes - - - - CUSTOM-appengine:update_queues - appengine:update_queues - - appengine:update_queues - - - From 5de75754a6a8404d9936dcc8651b2e36c3123e71 Mon Sep 17 00:00:00 2001 From: Jonathan Simon Date: Wed, 27 Apr 2016 13:56:36 -0700 Subject: [PATCH 6/7] Updates to pom.xml and more licences added --- appengine/taskqueue/pull/pom.xml | 58 +++---------------- .../src/main/webapp/WEB-INF/appengine-web.xml | 14 +++++ .../main/webapp/WEB-INF/logging.properties | 14 +++++ .../pull/src/main/webapp/WEB-INF/web.xml | 14 +++++ .../taskqueue/pull/src/main/webapp/tasks.jsp | 16 ++++- 5 files changed, 66 insertions(+), 50 deletions(-) diff --git a/appengine/taskqueue/pull/pom.xml b/appengine/taskqueue/pull/pom.xml index aec2c54b0c0..62e2cf70090 100644 --- a/appengine/taskqueue/pull/pom.xml +++ b/appengine/taskqueue/pull/pom.xml @@ -1,6 +1,6 @@ com.google.appengine appengine-api-1.0-sdk - ${appengine.version} javax.servlet @@ -56,13 +50,11 @@ Copyright 2015 Google Inc. All Rights Reserved. com.google.appengine appengine-testing - ${appengine.version} test com.google.appengine appengine-api-stubs - ${appengine.version} test @@ -74,7 +66,6 @@ Copyright 2015 Google Inc. All Rights Reserved. org.codehaus.mojo versions-maven-plugin - 2.1 compile @@ -87,51 +78,21 @@ Copyright 2015 Google Inc. All Rights Reserved. org.apache.maven.plugins - 3.1 maven-compiler-plugin 1.7 1.7 - - org.apache.maven.plugins - maven-war-plugin - 2.4 - - true - - - - ${basedir}/src/main/webapp/WEB-INF - true - WEB-INF - - - - - com.google.appengine appengine-maven-plugin - ${appengine.version} - - false - ${app.version} - - - - - + ${appengine.sdk.version} com.google.appengine gcloud-maven-plugin - ${gcloud.plugin.version} + ${appengine.sdk.version} true @@ -139,4 +100,3 @@ Copyright 2015 Google Inc. All Rights Reserved. - diff --git a/appengine/taskqueue/pull/src/main/webapp/WEB-INF/appengine-web.xml b/appengine/taskqueue/pull/src/main/webapp/WEB-INF/appengine-web.xml index c9e245399bf..c322e7d016e 100644 --- a/appengine/taskqueue/pull/src/main/webapp/WEB-INF/appengine-web.xml +++ b/appengine/taskqueue/pull/src/main/webapp/WEB-INF/appengine-web.xml @@ -1,4 +1,18 @@ + + + YOUR-PROJECT-ID YOUR-VERSION-ID diff --git a/appengine/taskqueue/pull/src/main/webapp/WEB-INF/logging.properties b/appengine/taskqueue/pull/src/main/webapp/WEB-INF/logging.properties index 19e9bf064c1..972c03f5da9 100644 --- a/appengine/taskqueue/pull/src/main/webapp/WEB-INF/logging.properties +++ b/appengine/taskqueue/pull/src/main/webapp/WEB-INF/logging.properties @@ -1,3 +1,17 @@ +# Copyright 2016 Google Inc. All Rights Reserved. + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + # A default java.util.logging configuration. # (All App Engine logging is through java.util.logging by default). # diff --git a/appengine/taskqueue/pull/src/main/webapp/WEB-INF/web.xml b/appengine/taskqueue/pull/src/main/webapp/WEB-INF/web.xml index 1ed2ec81871..8bd4347131c 100644 --- a/appengine/taskqueue/pull/src/main/webapp/WEB-INF/web.xml +++ b/appengine/taskqueue/pull/src/main/webapp/WEB-INF/web.xml @@ -1,4 +1,18 @@ + + + <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page import="java.util.List" %> <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> @@ -18,10 +31,11 @@
+
- +
${message}

From 8cb9c172596377a909a186962ddfd91fed5b8afd Mon Sep 17 00:00:00 2001 From: Jonathan Simon Date: Wed, 27 Apr 2016 14:18:15 -0700 Subject: [PATCH 7/7] Removed launch file. --- taskqueue/pull/TaskQueueSample.launch | 14 -------------- 1 file changed, 14 deletions(-) delete mode 100644 taskqueue/pull/TaskQueueSample.launch diff --git a/taskqueue/pull/TaskQueueSample.launch b/taskqueue/pull/TaskQueueSample.launch deleted file mode 100644 index 999114392c4..00000000000 --- a/taskqueue/pull/TaskQueueSample.launch +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - - - - -