diff --git a/appengine/taskqueue/pull/README.md b/appengine/taskqueue/pull/README.md new file mode 100644 index 00000000000..c15d0363dc0 --- /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/pom.xml b/appengine/taskqueue/pull/pom.xml new file mode 100644 index 00000000000..62e2cf70090 --- /dev/null +++ b/appengine/taskqueue/pull/pom.xml @@ -0,0 +1,102 @@ + + + + 4.0.0 + war + 1.0-SNAPSHOT + com.example.taskqueue + taskqueue + + + doc-samples + com.google.cloud + 1.0.0 + ../../.. + + + + + com.google.appengine + appengine-api-1.0-sdk + + + javax.servlet + servlet-api + 2.5 + provided + + + jstl + jstl + 1.2 + + + + + com.google.appengine + appengine-testing + test + + + com.google.appengine + appengine-api-stubs + test + + + + + + ${project.build.directory}/${project.build.finalName}/WEB-INF/classes + + + org.codehaus.mojo + versions-maven-plugin + + + compile + + display-dependency-updates + display-plugin-updates + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.7 + 1.7 + + + + com.google.appengine + appengine-maven-plugin + ${appengine.sdk.version} + + + com.google.appengine + gcloud-maven-plugin + ${appengine.sdk.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/webapp/WEB-INF/appengine-web.xml b/appengine/taskqueue/pull/src/main/webapp/WEB-INF/appengine-web.xml new file mode 100644 index 00000000000..c322e7d016e --- /dev/null +++ b/appengine/taskqueue/pull/src/main/webapp/WEB-INF/appengine-web.xml @@ -0,0 +1,20 @@ + + + + + + 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..972c03f5da9 --- /dev/null +++ b/appengine/taskqueue/pull/src/main/webapp/WEB-INF/logging.properties @@ -0,0 +1,27 @@ +# 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). +# +# 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..8bd4347131c --- /dev/null +++ b/appengine/taskqueue/pull/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,33 @@ + + + + + + + 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..1e8263b17c4 --- /dev/null +++ b/appengine/taskqueue/pull/src/main/webapp/tasks.jsp @@ -0,0 +1,50 @@ + + +<%@ 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" %> + + + + + + + + +
+
+
+
+
+
+
+
+
+
+ + + + 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/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] + } +}