-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Push Taskqueue sample program to Github
Some minor corrections to Requests testing and configuration.
- Loading branch information
Showing
11 changed files
with
300 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# A Java Task Queue example for Google App Engine | ||
|
||
This sample demonstrates how to use the [TaskQueue API][taskqueue-api] on [Google App | ||
Engine][ae-docs]. | ||
|
||
[taskqueue-api]: https://cloud.google.com/appengine/docs/java/javadoc/com/google/appengine/api/taskqueue/package-summary | ||
[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 | ||
|
||
Go to the site `localhost:8080` to add elements to the queue. They will appear in the log as the result of the Enqueue servlet transmitting the data to the Worker servlet. | ||
|
||
## Deploying | ||
In the following command, replace YOUR-PROJECT-ID with your | ||
[Google Cloud Project ID](https://support.google.com/cloud/answer/6158840). | ||
|
||
$ 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
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. | ||
--> | ||
<project> | ||
<modelVersion>4.0.0</modelVersion> | ||
<packaging>war</packaging> | ||
<version>1.0-SNAPSHOT</version> | ||
<groupId>com.example.appengine</groupId> | ||
<artifactId>taskqueue-push</artifactId> | ||
<!-- Parent POM defines ${appengine.sdk.version} (updates frequently). --> | ||
<parent> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>doc-samples</artifactId> | ||
<version>1.0.0</version> | ||
<relativePath>../..</relativePath> | ||
</parent> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.appengine</groupId> | ||
<artifactId>appengine-maven-plugin</artifactId> | ||
<version>${appengine.sdk.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>servlet-api</artifactId> | ||
<version>2.5</version> | ||
<type>jar</type> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.json</groupId> | ||
<artifactId>json</artifactId> | ||
<version>20151123</version> | ||
</dependency> | ||
<!-- Test Dependencies --> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.10</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-all</artifactId> | ||
<version>1.10.19</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.appengine</groupId> | ||
<artifactId>appengine-testing</artifactId> | ||
<version>${appengine.sdk.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.appengine</groupId> | ||
<artifactId>appengine-api-stubs</artifactId> | ||
<version>${appengine.sdk.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.appengine</groupId> | ||
<artifactId>appengine-tools-sdk</artifactId> | ||
<version>${appengine.sdk.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.truth</groupId> | ||
<artifactId>truth</artifactId> | ||
<version>0.28</version> | ||
<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>com.google.appengine</groupId> | ||
<artifactId>appengine-maven-plugin</artifactId> | ||
<version>${appengine.sdk.version}</version> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
28 changes: 28 additions & 0 deletions
28
appengine/taskqueue-push/src/main/java/com/example/appengine/taskqueue/push/Enqueue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.example.appengine.taskqueue.push; | ||
|
||
import com.google.appengine.api.taskqueue.Queue; | ||
import com.google.appengine.api.taskqueue.QueueFactory; | ||
import com.google.appengine.api.taskqueue.TaskOptions; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
// [START enqueue] | ||
// The Enqueue servlet should be mapped to the "/enqueue" URL. | ||
public class Enqueue extends HttpServlet { | ||
protected void doPost(HttpServletRequest request, HttpServletResponse response) | ||
throws ServletException, IOException { | ||
String key = request.getParameter("key"); | ||
|
||
// Add the task to the default queue. | ||
Queue queue = QueueFactory.getDefaultQueue(); | ||
queue.add(TaskOptions.Builder.withUrl("/worker").param("key", key)); | ||
|
||
response.sendRedirect("/"); | ||
} | ||
} | ||
// [END enqueue] |
27 changes: 27 additions & 0 deletions
27
appengine/taskqueue-push/src/main/java/com/example/appengine/taskqueue/push/Worker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.example.appengine.taskqueue.push; | ||
|
||
import java.io.IOException; | ||
import java.util.logging.Logger; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
// [START worker] | ||
// The Worker servlet should be mapped to the "/worker" URL. | ||
public class Worker extends HttpServlet { | ||
private static final Logger log = Logger.getLogger(Worker.class.getName()); | ||
|
||
protected void doPost(HttpServletRequest request, HttpServletResponse response) | ||
throws ServletException, IOException { | ||
String key = request.getParameter("key"); | ||
|
||
// Do something with key. | ||
// [START_EXCLUDE] | ||
log.info("Worker is processing " + key); | ||
// [END_EXCLUDE] | ||
|
||
} | ||
} | ||
// [END worker] |
6 changes: 6 additions & 0 deletions
6
appengine/taskqueue-push/src/main/webapp/WEB-INF/appengine-web.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" | ||
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" | ||
version="2.5"> | ||
<welcome-file-list> | ||
<welcome-file>index.html</welcome-file> | ||
</welcome-file-list> | ||
<servlet> | ||
<servlet-name>enqueue</servlet-name> | ||
<servlet-class>com.example.appengine.taskqueue.push.Enqueue</servlet-class> | ||
</servlet> | ||
<servlet> | ||
<servlet-name>worker</servlet-name> | ||
<servlet-class>com.example.appengine.taskqueue.push.Worker</servlet-class> | ||
</servlet> | ||
<servlet-mapping> | ||
<servlet-name>enqueue</servlet-name> | ||
<url-pattern>/enqueue</url-pattern> | ||
</servlet-mapping> | ||
<servlet-mapping> | ||
<servlet-name>worker</servlet-name> | ||
<url-pattern>/worker</url-pattern> | ||
</servlet-mapping> | ||
</web-app> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!--[START index_file] --> | ||
<!-- A basic index.html file served from the "/" URL. --> | ||
<html> | ||
<body> | ||
<p>Enqueue a value, to be processed by a worker.</p> | ||
<form action="/enqueue" method="post"> | ||
<input type="text" name="key"> | ||
<input type="submit"> | ||
</form> | ||
</body> | ||
</html> | ||
<!--[END index_file] --> |
65 changes: 65 additions & 0 deletions
65
appengine/taskqueue-push/src/test/java/com/example/appengine/taskqueue/push/WorkerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.example.appengine.taskqueue.push; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static org.mockito.Mockito.when; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.PrintStream; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
/** | ||
* Unit tests for {@link Worker}. | ||
*/ | ||
@RunWith(JUnit4.class) | ||
public class WorkerTest { | ||
private static final String FAKE_KEY_VALUE = "KEY"; | ||
|
||
// To capture and restore stderr | ||
private final ByteArrayOutputStream stderr = new ByteArrayOutputStream(); | ||
private static final PrintStream REAL_ERR = System.err; | ||
|
||
@Mock | ||
private HttpServletRequest mockRequest; | ||
@Mock | ||
private HttpServletResponse mockResponse; | ||
private Worker servletUnderTest; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
// Capture stderr to examine messages written to it | ||
System.setErr(new PrintStream(stderr)); | ||
|
||
MockitoAnnotations.initMocks(this); | ||
|
||
when(mockRequest.getParameter("key")).thenReturn(FAKE_KEY_VALUE); | ||
|
||
servletUnderTest = new Worker(); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
// Restore stderr | ||
System.setErr(WorkerTest.REAL_ERR); | ||
} | ||
|
||
@Test | ||
public void doPost_writesResponse() throws Exception { | ||
servletUnderTest.doPost(mockRequest, mockResponse); | ||
|
||
String out = stderr.toString(); | ||
// We expect a log message to be created | ||
// with the following message. | ||
assertThat(out).contains("Worker is processing " + FAKE_KEY_VALUE); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters