Skip to content

Commit d06fb85

Browse files
committed
Merge pull request #7 from GoogleCloudPlatform/defer
Add Example Of Defer API
2 parents 602b04a + 0585ed3 commit d06fb85

File tree

8 files changed

+399
-0
lines changed

8 files changed

+399
-0
lines changed

taskqueue/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Task Queue Java Snippets
2+
3+
These are Java samples for using the [Task Queue](https://cloud.google.com/appengine/docs/java/taskqueue/)
4+
5+

taskqueue/deferred/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
App Engine Java Guestbook
2+
Copyright (C) 2010-2012 Google Inc.
3+
4+
## Sample guestbook for use with App Engine Java.
5+
6+
Requires [Apache Maven](http://maven.apache.org) 3.1 or greater, and JDK 7+ in order to run.
7+
8+
To build, run
9+
10+
mvn package
11+
12+
Building will run the tests, but to explicitly run tests you can use the test target
13+
14+
mvn test
15+
16+
To start the app, use the [App Engine Maven Plugin](http://code.google.com/p/appengine-maven-plugin/) that is already included in this demo. Just run the command.
17+
18+
mvn appengine:devserver
19+
20+
For further information, consult the [Java App Engine](https://developers.google.com/appengine/docs/java/overview) documentation.
21+
22+
To see all the available goals for the App Engine plugin, run
23+
24+
mvn help:describe -Dplugin=appengine

taskqueue/deferred/pom.xml

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
3+
4+
<modelVersion>4.0.0</modelVersion>
5+
<packaging>war</packaging>
6+
<version>1.0-SNAPSHOT</version>
7+
8+
<groupId>com.google.cloud.taskqueue.samples</groupId>
9+
<artifactId>defer-sample</artifactId>
10+
11+
<properties>
12+
<appengine.version>1.9.19</appengine.version>
13+
<appengine.app.version>1</appengine.app.version>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<prerequisites>
18+
<maven>3.1.0</maven>
19+
</prerequisites>
20+
21+
<dependencies>
22+
<!-- Compile/runtime dependencies -->
23+
<dependency>
24+
<groupId>com.google.appengine</groupId>
25+
<artifactId>appengine-api-1.0-sdk</artifactId>
26+
<version>${appengine.version}</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>javax.servlet</groupId>
30+
<artifactId>servlet-api</artifactId>
31+
<version>2.5</version>
32+
<scope>provided</scope>
33+
</dependency>
34+
<dependency>
35+
<groupId>jstl</groupId>
36+
<artifactId>jstl</artifactId>
37+
<version>1.2</version>
38+
</dependency>
39+
40+
<!-- Test Dependencies -->
41+
<dependency>
42+
<groupId>junit</groupId>
43+
<artifactId>junit</artifactId>
44+
<version>4.12-beta-1</version>
45+
<scope>test</scope>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.mockito</groupId>
49+
<artifactId>mockito-all</artifactId>
50+
<version>1.9.5</version>
51+
<scope>test</scope>
52+
</dependency>
53+
<dependency>
54+
<groupId>com.google.appengine</groupId>
55+
<artifactId>appengine-testing</artifactId>
56+
<version>${appengine.version}</version>
57+
<scope>test</scope>
58+
</dependency>
59+
<dependency>
60+
<groupId>com.google.appengine</groupId>
61+
<artifactId>appengine-api-stubs</artifactId>
62+
<version>${appengine.version}</version>
63+
<scope>test</scope>
64+
</dependency>
65+
</dependencies>
66+
67+
<build>
68+
<!-- for hot reload of the web application-->
69+
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
70+
<plugins>
71+
<plugin>
72+
<groupId>org.codehaus.mojo</groupId>
73+
<artifactId>versions-maven-plugin</artifactId>
74+
<version>2.1</version>
75+
<executions>
76+
<execution>
77+
<phase>compile</phase>
78+
<goals>
79+
<goal>display-dependency-updates</goal>
80+
<goal>display-plugin-updates</goal>
81+
</goals>
82+
</execution>
83+
</executions>
84+
</plugin>
85+
<plugin>
86+
<groupId>org.apache.maven.plugins</groupId>
87+
<version>3.1</version>
88+
<artifactId>maven-compiler-plugin</artifactId>
89+
<configuration>
90+
<source>1.7</source>
91+
<target>1.7</target>
92+
</configuration>
93+
</plugin>
94+
95+
<plugin>
96+
<groupId>org.apache.maven.plugins</groupId>
97+
<artifactId>maven-war-plugin</artifactId>
98+
<version>2.4</version>
99+
<configuration>
100+
<archiveClasses>true</archiveClasses>
101+
<webResources>
102+
<!-- in order to interpolate version from pom into appengine-web.xml -->
103+
<resource>
104+
<directory>${basedir}/src/main/webapp/WEB-INF</directory>
105+
<filtering>true</filtering>
106+
<targetPath>WEB-INF</targetPath>
107+
</resource>
108+
</webResources>
109+
</configuration>
110+
</plugin>
111+
112+
<plugin>
113+
<groupId>com.google.appengine</groupId>
114+
<artifactId>appengine-maven-plugin</artifactId>
115+
<version>1.9.19</version>
116+
<configuration>
117+
<enableJarClasses>false</enableJarClasses>
118+
<!-- Comment in the below snippet to bind to all IPs instead of just localhost -->
119+
<!-- address>0.0.0.0</address>
120+
<port>8080</port -->
121+
<!-- Comment in the below snippet to enable local debugging with a remove debugger
122+
like those included with Eclipse or IntelliJ -->
123+
<!-- jvmFlags>
124+
<jvmFlag>-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n</jvmFlag>
125+
</jvmFlags -->
126+
</configuration>
127+
</plugin>
128+
</plugins>
129+
</build>
130+
131+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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.google.cloud.taskqueue.samples;
18+
19+
import javax.servlet.http.HttpServlet;
20+
import javax.servlet.http.HttpServletRequest;
21+
import javax.servlet.http.HttpServletResponse;
22+
import java.io.IOException;
23+
24+
import com.google.appengine.api.taskqueue.DeferredTask;
25+
import com.google.appengine.api.taskqueue.Queue;
26+
import com.google.appengine.api.taskqueue.QueueFactory;
27+
import com.google.appengine.api.taskqueue.TaskOptions;
28+
29+
/**
30+
* This small servlet demonstrates how to use the DeferredTask
31+
* interface to background a task on the AppEngine task queues,
32+
* without needing to create a separate URL handler.
33+
*/
34+
public class DeferSampleServlet extends HttpServlet {
35+
36+
//[START defer]
37+
public static class ExpensiveOperation implements DeferredTask {
38+
@Override
39+
public void run() {
40+
System.out.println("Doing an expensive operation...");
41+
// expensive operation to be backgrounded goes here
42+
}
43+
}
44+
45+
@Override
46+
public void doGet(HttpServletRequest request, HttpServletResponse
47+
resp) throws IOException {
48+
// Add the task to the default queue.
49+
Queue queue = QueueFactory.getDefaultQueue();
50+
51+
// Wait 5 seconds to run for demonstration purposes
52+
queue.add(TaskOptions.Builder.withPayload(new ExpensiveOperation())
53+
.etaMillis(System.currentTimeMillis() + 5000));
54+
55+
resp.setContentType("text/plain");
56+
resp.getWriter().println("Task is backgrounded on queue!");
57+
}
58+
//[END defer]
59+
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright 2015 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+
18+
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
19+
<application>your-app-id</application>
20+
<version>${appengine.app.version}</version>
21+
<threadsafe>true</threadsafe>
22+
23+
<system-properties>
24+
<property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
25+
</system-properties>
26+
</appengine-web-app>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# A default java.util.logging configuration.
2+
# (All App Engine logging is through java.util.logging by default).
3+
#
4+
# To use this configuration, copy it into your application's WEB-INF
5+
# folder and add the following to your appengine-web.xml:
6+
#
7+
# <system-properties>
8+
# <property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
9+
# </system-properties>
10+
#
11+
12+
# Set the default logging level for all loggers to WARNING
13+
.level = WARNING
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright 2015 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+
<web-app
18+
version="2.5"
19+
xmlns="http://java.sun.com/xml/ns/javaee"
20+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
21+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
22+
<servlet>
23+
<servlet-name>defer-sample-servlet</servlet-name>
24+
<servlet-class>com.google.cloud.taskqueue.samples.DeferSampleServlet</servlet-class>
25+
</servlet>
26+
<servlet-mapping>
27+
<servlet-name>defer-sample-servlet</servlet-name>
28+
<url-pattern>/defer</url-pattern>
29+
</servlet-mapping>
30+
</web-app>

0 commit comments

Comments
 (0)