-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding App Engine pull task queue sample #203
Changes from all commits
554a57f
c65494a
b4e1c7b
442536d
5e3de39
5de7575
14beca4
8cb9c17
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <application> tag in src/main/webapp/WEB-INF/appengine-web.xml | ||
with your project name. | ||
|
||
2. Update the <version> 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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
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. | ||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<packaging>war</packaging> | ||
<version>1.0-SNAPSHOT</version> | ||
<groupId>com.example.taskqueue</groupId> | ||
<artifactId>taskqueue</artifactId> | ||
|
||
<parent> | ||
<artifactId>doc-samples</artifactId> | ||
<groupId>com.google.cloud</groupId> | ||
<version>1.0.0</version> | ||
<relativePath>../../..</relativePath> | ||
</parent> | ||
<dependencies> | ||
<!-- Compile/runtime dependencies --> | ||
<dependency> | ||
<groupId>com.google.appengine</groupId> | ||
<artifactId>appengine-api-1.0-sdk</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>servlet-api</artifactId> | ||
<version>2.5</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>jstl</groupId> | ||
<artifactId>jstl</artifactId> | ||
<version>1.2</version> | ||
</dependency> | ||
|
||
<!-- Test Dependencies --> | ||
<dependency> | ||
<groupId>com.google.appengine</groupId> | ||
<artifactId>appengine-testing</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.appengine</groupId> | ||
<artifactId>appengine-api-stubs</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<!-- for hot reload of the web application--> | ||
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<artifactId>versions-maven-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<phase>compile</phase> | ||
<goals> | ||
<goal>display-dependency-updates</goal> | ||
<goal>display-plugin-updates</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not required as it's set in the parent. |
||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>1.7</source> | ||
<target>1.7</target> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>com.google.appengine</groupId> | ||
<artifactId>appengine-maven-plugin</artifactId> | ||
<version>${appengine.sdk.version}</version> | ||
</plugin> | ||
<plugin> | ||
<groupId>com.google.appengine</groupId> | ||
<artifactId>gcloud-maven-plugin</artifactId> | ||
<version>${appengine.sdk.version}</version> | ||
<configuration> | ||
<set_default>true</set_default> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<TaskHandle> 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<TaskHandle> 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<TaskHandle> 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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. License here. |
||
<!-- [START_EXCLUDE] --> | ||
<!-- | ||
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. | ||
--> | ||
<!-- [END_EXCLUDE] --> | ||
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0"> | ||
<application>YOUR-PROJECT-ID</application> | ||
<version>YOUR-VERSION-ID</version> | ||
<threadsafe>true</threadsafe> | ||
</appengine-web-app> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. License Here |
||
# (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: | ||
# | ||
# <system-properties> | ||
# <property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/> | ||
# </system-properties> | ||
# | ||
|
||
# Set the default logging level for all loggers to WARNING | ||
.level = INFO |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<queue-entries> | ||
<queue> | ||
<name>pull-queue</name> | ||
<mode>pull</mode> | ||
<acl> | ||
<user-email>[email protected]</user-email> <!-- can list, get, lease, delete, and update tasks --> | ||
<writer-email>[email protected]</writer-email> <!-- can insert tasks, in addition to rights granted by being a user_email above --> | ||
</acl> | ||
</queue> | ||
</queue-entries> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. License here |
||
<!-- [START_EXCLUDE] --> | ||
<!-- | ||
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. | ||
--> | ||
<!-- [END_EXCLUDE] --> | ||
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee | ||
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> | ||
<servlet> | ||
<servlet-name>taskqueue</servlet-name> | ||
<servlet-class>com.example.taskqueue.TaskqueueServlet</servlet-class> | ||
</servlet> | ||
|
||
<servlet-mapping> | ||
<servlet-name>taskqueue</servlet-name> | ||
<url-pattern>/taskqueue</url-pattern> | ||
</servlet-mapping> | ||
|
||
<welcome-file-list> | ||
<welcome-file>tasks.jsp</welcome-file> | ||
</welcome-file-list> | ||
</web-app> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
versions is or should be in the parent.