Skip to content

Commit efc2c93

Browse files
committed
Merge pull request #203 from jsimonweb/master
Adding App Engine pull task queue sample
2 parents 3a8982f + 8cb9c17 commit efc2c93

File tree

12 files changed

+813
-0
lines changed

12 files changed

+813
-0
lines changed

appengine/taskqueue/pull/README.md

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Pull Task Queue sample for Google App Engine
2+
3+
This sample demonstrates how to use [pull task queues][appid] on [Google App
4+
Engine][ae-docs].
5+
6+
[appid]: https://cloud.google.com/appengine/docs/java/taskqueue/overview-pull
7+
[ae-docs]: https://cloud.google.com/appengine/docs/java/
8+
9+
## Running locally
10+
This example uses the
11+
[Maven gcloud plugin](https://cloud.google.com/appengine/docs/java/managed-vms/maven).
12+
To run this sample locally:
13+
14+
$ mvn appengine:devserver
15+
16+
## Deploying
17+
In the following command, replace YOUR-PROJECT-ID with your
18+
[Google Cloud Project ID](https://developers.google.com/console/help/new/#projectnumber).
19+
20+
$ mvn appengine:update -Dappengine.appId=YOUR-PROJECT-ID -Dappengine.version=SOME-VERSION
21+
22+
## Setup
23+
To save your project settings so that you don't need to enter the
24+
parameters, you can:
25+
26+
1. Update the <application> tag in src/main/webapp/WEB-INF/appengine-web.xml
27+
with your project name.
28+
29+
2. Update the <version> tag in src/main/webapp/WEB-INF/appengine-web.xml
30+
with a valid version number.
31+
32+
33+
You will now be able to run
34+
35+
$ mvn appengine:update
36+
37+
without the need for any additional parameters.

appengine/taskqueue/pull/pom.xml

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2016 Google Inc. All Rights Reserved.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
18+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
19+
<modelVersion>4.0.0</modelVersion>
20+
<packaging>war</packaging>
21+
<version>1.0-SNAPSHOT</version>
22+
<groupId>com.example.taskqueue</groupId>
23+
<artifactId>taskqueue</artifactId>
24+
25+
<parent>
26+
<artifactId>doc-samples</artifactId>
27+
<groupId>com.google.cloud</groupId>
28+
<version>1.0.0</version>
29+
<relativePath>../../..</relativePath>
30+
</parent>
31+
<dependencies>
32+
<!-- Compile/runtime dependencies -->
33+
<dependency>
34+
<groupId>com.google.appengine</groupId>
35+
<artifactId>appengine-api-1.0-sdk</artifactId>
36+
</dependency>
37+
<dependency>
38+
<groupId>javax.servlet</groupId>
39+
<artifactId>servlet-api</artifactId>
40+
<version>2.5</version>
41+
<scope>provided</scope>
42+
</dependency>
43+
<dependency>
44+
<groupId>jstl</groupId>
45+
<artifactId>jstl</artifactId>
46+
<version>1.2</version>
47+
</dependency>
48+
49+
<!-- Test Dependencies -->
50+
<dependency>
51+
<groupId>com.google.appengine</groupId>
52+
<artifactId>appengine-testing</artifactId>
53+
<scope>test</scope>
54+
</dependency>
55+
<dependency>
56+
<groupId>com.google.appengine</groupId>
57+
<artifactId>appengine-api-stubs</artifactId>
58+
<scope>test</scope>
59+
</dependency>
60+
</dependencies>
61+
62+
<build>
63+
<!-- for hot reload of the web application-->
64+
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
65+
<plugins>
66+
<plugin>
67+
<groupId>org.codehaus.mojo</groupId>
68+
<artifactId>versions-maven-plugin</artifactId>
69+
<executions>
70+
<execution>
71+
<phase>compile</phase>
72+
<goals>
73+
<goal>display-dependency-updates</goal>
74+
<goal>display-plugin-updates</goal>
75+
</goals>
76+
</execution>
77+
</executions>
78+
</plugin>
79+
<plugin>
80+
<groupId>org.apache.maven.plugins</groupId>
81+
<artifactId>maven-compiler-plugin</artifactId>
82+
<configuration>
83+
<source>1.7</source>
84+
<target>1.7</target>
85+
</configuration>
86+
</plugin>
87+
<plugin>
88+
<groupId>com.google.appengine</groupId>
89+
<artifactId>appengine-maven-plugin</artifactId>
90+
<version>${appengine.sdk.version}</version>
91+
</plugin>
92+
<plugin>
93+
<groupId>com.google.appengine</groupId>
94+
<artifactId>gcloud-maven-plugin</artifactId>
95+
<version>${appengine.sdk.version}</version>
96+
<configuration>
97+
<set_default>true</set_default>
98+
</configuration>
99+
</plugin>
100+
</plugins>
101+
</build>
102+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/**
2+
* Copyright 2015 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.taskqueue;
18+
19+
import com.google.appengine.api.taskqueue.TaskOptions;
20+
import com.google.appengine.api.taskqueue.Queue;
21+
import com.google.appengine.api.taskqueue.QueueFactory;
22+
import com.google.appengine.api.taskqueue.TaskHandle;
23+
24+
import java.io.IOException;
25+
import javax.servlet.ServletException;
26+
import javax.servlet.http.HttpServlet;
27+
import javax.servlet.http.HttpServletRequest;
28+
import javax.servlet.http.HttpServletResponse;
29+
30+
import java.util.Date;
31+
import java.util.List;
32+
import java.util.logging.Logger;
33+
import java.util.concurrent.TimeUnit;
34+
35+
/**
36+
* Form Handling Servlet
37+
* This servlet has one method
38+
* {@link #doPost(<#HttpServletRequest req#>, <#HttpServletResponse resp#>)} which takes the form
39+
* submisson from /src/main/webapp/tasks.jsp to add and delete tasks.
40+
*/
41+
public class TaskqueueServlet extends HttpServlet {
42+
43+
private static final Logger log = Logger.getLogger(TaskqueueServlet.class.getName());
44+
private static final int numberOfTasksToAdd = 100;
45+
private static final int numberOfTasksToLease = 100;
46+
private static boolean useTaggedTasks = true;
47+
private static String output;
48+
private static String message;
49+
50+
// Process the http POST of the form
51+
@Override
52+
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException,
53+
ServletException {
54+
if (req.getParameter("addTask") != null) {
55+
String content = req.getParameter("content");
56+
String output = String.format("Adding %d Tasks to the Task Queue with a payload of '%s'",
57+
numberOfTasksToAdd, content.toString());
58+
log.info(output.toString());
59+
60+
// Add Tasks to Task Queue
61+
// [START get_queue]
62+
Queue q = QueueFactory.getQueue("pull-queue");
63+
// [END get_queue]
64+
if (!useTaggedTasks) {
65+
for (int i = 0; i < numberOfTasksToAdd; i++) {
66+
// [START add_task]
67+
q.add(TaskOptions.Builder.withMethod(TaskOptions.Method.PULL)
68+
.payload(content.toString()));
69+
// [END add_task]
70+
}
71+
} else {
72+
for (int i = 0; i < numberOfTasksToAdd; i++) {
73+
// [START add_task_w_tag]
74+
q.add(TaskOptions.Builder.withMethod(TaskOptions.Method.PULL)
75+
.payload(content.toString())
76+
.tag("process".getBytes()));
77+
// [END add_task_w_tag]
78+
79+
}
80+
}
81+
try {
82+
message = "Added " + numberOfTasksToAdd + " tasks to the task queue.";
83+
req.setAttribute("message", message);
84+
req.getRequestDispatcher("tasks.jsp").forward(req,resp);
85+
} catch (ServletException e) {
86+
throw new ServletException("ServletException error: ", e);
87+
}
88+
} else if (req.getParameter("leaseTask") != null) {
89+
output = String.format("Pulling %d Tasks from the Task Queue", numberOfTasksToLease);
90+
log.info(output.toString());
91+
92+
// Pull tasks from the Task Queue and process them
93+
Queue q = QueueFactory.getQueue("pull-queue");
94+
if (!useTaggedTasks) {
95+
// [START lease_tasks]
96+
List<TaskHandle> tasks = q.leaseTasks(3600, TimeUnit.SECONDS, numberOfTasksToLease);
97+
// [END lease_tasks]
98+
message = processTasks(tasks, q);
99+
} else {
100+
// [START lease_tasks_by_tag]
101+
// Lease only tasks tagged with "process"
102+
List<TaskHandle> tasks = q.leaseTasksByTag(3600, TimeUnit.SECONDS, numberOfTasksToLease, "process");
103+
// You can also specify a tag to lease via LeaseOptions passed to leaseTasks.
104+
// [END lease_tasks_by_tag]
105+
message = processTasks(tasks, q);
106+
}
107+
req.setAttribute("message", message);
108+
req.getRequestDispatcher("tasks.jsp").forward(req,resp);
109+
} else {
110+
resp.sendRedirect("/");
111+
}
112+
}
113+
114+
//Method to process and delete tasks
115+
private static String processTasks(List<TaskHandle> tasks, Queue q) {
116+
String payload;
117+
int numberOfDeletedTasks = 0;
118+
for (TaskHandle task : tasks) {
119+
payload = new String(task.getPayload());
120+
output = String.format("Processing: taskName='%s' payload='%s'", task.getName()
121+
.toString(), payload.toString());
122+
log.info(output.toString());
123+
output = String.format("Deleting taskName='%s'", task.getName().toString());
124+
log.info(output.toString());
125+
// [START delete_task]
126+
q.deleteTask(task);
127+
// [END delete_task]
128+
numberOfDeletedTasks++;
129+
}
130+
if (numberOfDeletedTasks > 0) {
131+
message = "Processed and deleted " + numberOfTasksToLease + " tasks from the " +
132+
" task queue.";
133+
} else {
134+
message = "Task Queue has no tasks available for lease.";
135+
}
136+
return message;
137+
}
138+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- [START_EXCLUDE] -->
3+
<!--
4+
Copyright 2016 Google Inc. All Rights Reserved.
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
-->
15+
<!-- [END_EXCLUDE] -->
16+
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
17+
<application>YOUR-PROJECT-ID</application>
18+
<version>YOUR-VERSION-ID</version>
19+
<threadsafe>true</threadsafe>
20+
</appengine-web-app>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright 2016 Google Inc. All Rights Reserved.
2+
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# A default java.util.logging configuration.
16+
# (All App Engine logging is through java.util.logging by default).
17+
#
18+
# To use this configuration, copy it into your application's WEB-INF
19+
# folder and add the following to your appengine-web.xml:
20+
#
21+
# <system-properties>
22+
# <property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
23+
# </system-properties>
24+
#
25+
26+
# Set the default logging level for all loggers to WARNING
27+
.level = INFO
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<queue-entries>
2+
<queue>
3+
<name>pull-queue</name>
4+
<mode>pull</mode>
5+
<acl>
6+
<user-email>[email protected]</user-email> <!-- can list, get, lease, delete, and update tasks -->
7+
<writer-email>[email protected]</writer-email> <!-- can insert tasks, in addition to rights granted by being a user_email above -->
8+
</acl>
9+
</queue>
10+
</queue-entries>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- [START_EXCLUDE] -->
3+
<!--
4+
Copyright 2016 Google Inc. All Rights Reserved.
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
-->
15+
<!-- [END_EXCLUDE] -->
16+
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
17+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
18+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
19+
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
20+
<servlet>
21+
<servlet-name>taskqueue</servlet-name>
22+
<servlet-class>com.example.taskqueue.TaskqueueServlet</servlet-class>
23+
</servlet>
24+
25+
<servlet-mapping>
26+
<servlet-name>taskqueue</servlet-name>
27+
<url-pattern>/taskqueue</url-pattern>
28+
</servlet-mapping>
29+
30+
<welcome-file-list>
31+
<welcome-file>tasks.jsp</welcome-file>
32+
</welcome-file-list>
33+
</web-app>

0 commit comments

Comments
 (0)