Skip to content

Commit 2a69ab7

Browse files
authored
Cloud Tasks app (GoogleCloudPlatform#1585)
* Task app * Add region tags * Update comments * Update function comment * Update function comment
1 parent 762ca60 commit 2a69ab7

File tree

8 files changed

+403
-0
lines changed

8 files changed

+403
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Google Cloud Tasks and App Engine Sample
2+
3+
<a href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&page=editor&open_in_editor=appengine-java8/tasks/app/README.md">
4+
<img alt="Open in Cloud Shell" src ="http://gstatic.com/cloudssh/images/open-btn.png"></a>
5+
6+
This sample demonstrates how to use the [Cloud Tasks API][task-api] on [Google App
7+
Engine][ae-docs].
8+
9+
[task-api]: https://cloud.google.com/tasks/docs/
10+
[ae-docs]: https://cloud.google.com/appengine/docs/java/
11+
12+
## Initial Setup
13+
14+
* Set up a Google Cloud Project and enable billing.
15+
* Enable the
16+
[Cloud Tasks API](https://console.cloud.google.com/launcher/details/google/cloudtasks.googleapis.com).
17+
* Download and install the [Cloud SDK](https://cloud.google.com/sdk).
18+
* Download and install [Maven](http://maven.apache.org/install.html).
19+
* Set up [Google Application Credentials](https://cloud.google.com/docs/authentication/getting-started).
20+
21+
## Creating a queue
22+
23+
To create a queue using the Cloud SDK, use the following gcloud command:
24+
25+
```
26+
gcloud tasks queues create default
27+
```
28+
29+
## Deploying the App Engine app
30+
31+
First, update the `projectId` and `locationId` variables to match the values of
32+
your queue in file `Enqueue.java`. To find these values use the following gcloud
33+
command:
34+
35+
```
36+
gcloud tasks queues describe default
37+
```
38+
39+
Second, [Using Maven and the App Engine Plugin](https://cloud.google.com/appengine/docs/flexible/java/using-maven)
40+
deploy to Google App Engine:
41+
42+
```
43+
mvn appengine:deploy
44+
```

appengine-java8/tasks/app/pom.xml

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2019 Google LLC
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"
18+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
20+
<modelVersion>4.0.0</modelVersion>
21+
<packaging>war</packaging>
22+
<version>1.0-SNAPSHOT</version>
23+
<groupId>com.example.task</groupId>
24+
<artifactId>cloud-tasks-app</artifactId>
25+
26+
<!--
27+
The parent pom defines common style checks and testing strategies for our samples.
28+
Removing or replacing it should not affect the execution of the samples in anyway.
29+
-->
30+
<parent>
31+
<groupId>com.google.cloud.samples</groupId>
32+
<artifactId>shared-configuration</artifactId>
33+
<version>1.0.11</version>
34+
</parent>
35+
36+
<properties>
37+
<maven.compiler.target>1.8</maven.compiler.target>
38+
<maven.compiler.source>1.8</maven.compiler.source>
39+
</properties>
40+
41+
<dependencies>
42+
<dependency>
43+
<groupId>javax.servlet</groupId>
44+
<artifactId>javax.servlet-api</artifactId>
45+
<version>3.1.0</version>
46+
<type>jar</type>
47+
<scope>provided</scope>
48+
</dependency>
49+
50+
<dependency>
51+
<groupId>com.google.cloud</groupId>
52+
<artifactId>google-cloud-tasks</artifactId>
53+
<version>1.3.0</version>
54+
</dependency>
55+
56+
<!-- Test Dependencies -->
57+
<dependency>
58+
<groupId>junit</groupId>
59+
<artifactId>junit</artifactId>
60+
<version>4.13-beta-3</version>
61+
<scope>test</scope>
62+
</dependency>
63+
<dependency>
64+
<groupId>org.mockito</groupId>
65+
<artifactId>mockito-all</artifactId>
66+
<version>1.10.19</version>
67+
<scope>test</scope>
68+
</dependency>
69+
<dependency>
70+
<groupId>com.google.truth</groupId>
71+
<artifactId>truth</artifactId>
72+
<version>1.0</version>
73+
<scope>test</scope>
74+
</dependency>
75+
</dependencies>
76+
<build>
77+
<!-- for hot reload of the web application -->
78+
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
79+
<plugins>
80+
<plugin>
81+
<groupId>com.google.cloud.tools</groupId>
82+
<artifactId>appengine-maven-plugin</artifactId>
83+
<version>1.3.2</version>
84+
<configuration>
85+
<deploy.promote>true</deploy.promote>
86+
<deploy.stopPreviousVersion>true</deploy.stopPreviousVersion>
87+
</configuration>
88+
</plugin>
89+
90+
<plugin>
91+
<groupId>org.apache.maven.plugins</groupId>
92+
<artifactId>maven-war-plugin</artifactId>
93+
<version>3.1.0</version>
94+
</plugin>
95+
96+
</plugins>
97+
</build>
98+
</project>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/* Copyright 2019 Google LLC
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+
16+
package com.example.task;
17+
18+
// [START cloud_tasks_enqueue]
19+
import com.google.cloud.tasks.v2.AppEngineHttpRequest;
20+
import com.google.cloud.tasks.v2.CloudTasksClient;
21+
import com.google.cloud.tasks.v2.HttpMethod;
22+
import com.google.cloud.tasks.v2.QueueName;
23+
import com.google.cloud.tasks.v2.Task;
24+
import java.io.IOException;
25+
import javax.servlet.ServletException;
26+
import javax.servlet.annotation.WebServlet;
27+
import javax.servlet.http.HttpServlet;
28+
import javax.servlet.http.HttpServletRequest;
29+
import javax.servlet.http.HttpServletResponse;
30+
31+
@WebServlet(
32+
name = "TaskEnqueue",
33+
description = "Enqueue a task targeted at endpoint '/taskqueues/worker'",
34+
urlPatterns = "/taskqueues/enqueue")
35+
public class Enqueue extends HttpServlet {
36+
37+
// TODO(developer): Replace these variables before running the sample.
38+
static final String projectId = "my-project-id";
39+
static final String locationId = "us-central1";
40+
41+
// Function creates Cloud Tasks from form submissions.
42+
protected void doPost(HttpServletRequest request, HttpServletResponse response)
43+
throws ServletException, IOException {
44+
String key = request.getParameter("key");
45+
46+
try (CloudTasksClient client = CloudTasksClient.create()) {
47+
// Construct the fully qualified queue name.
48+
String queueName = QueueName.of(projectId, locationId, "default").toString();
49+
50+
// Construct the task body.
51+
Task task =
52+
Task.newBuilder()
53+
.setAppEngineHttpRequest(
54+
AppEngineHttpRequest.newBuilder()
55+
.setRelativeUri("/taskqueues/worker?key=" + key)
56+
.setHttpMethod(HttpMethod.POST)
57+
.build())
58+
.build();
59+
60+
// Add the task to the default queue.
61+
Task taskResponse = client.createTask(queueName, task);
62+
System.out.println("Task created: " + taskResponse.getName());
63+
}
64+
65+
response.sendRedirect("/");
66+
}
67+
}
68+
// [END cloud_tasks_enqueue]
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* Copyright 2019 Google LLC
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+
16+
package com.example.task;
17+
18+
// [START cloud_tasks_worker]
19+
import java.io.IOException;
20+
import java.util.logging.Logger;
21+
import javax.servlet.ServletException;
22+
import javax.servlet.annotation.WebServlet;
23+
import javax.servlet.http.HttpServlet;
24+
import javax.servlet.http.HttpServletRequest;
25+
import javax.servlet.http.HttpServletResponse;
26+
27+
@WebServlet(
28+
name = "TaskWorker",
29+
description = "Endpoint to process Cloud Task requests",
30+
urlPatterns = "/taskqueues/worker"
31+
)
32+
public class Worker extends HttpServlet {
33+
34+
private static final Logger log = Logger.getLogger(Worker.class.getName());
35+
36+
// Worker function to process POST requests from Cloud Tasks targeted at the
37+
// '/taskqueues/worker' endpoint.
38+
protected void doPost(HttpServletRequest request, HttpServletResponse response)
39+
throws ServletException, IOException {
40+
String key = request.getParameter("key");
41+
log.info("Worker is processing " + key);
42+
}
43+
}
44+
// [END cloud_tasks_worker]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2019 Google LLC
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+
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
18+
<runtime>java8</runtime>
19+
<threadsafe>true</threadsafe>
20+
</appengine-web-app>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2019 Google LLC
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 xmlns="http://xmlns.jcp.org/xml/ns/javaee"
18+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
20+
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
21+
version="3.1">
22+
<welcome-file-list>
23+
<welcome-file>taskqueue-push.html</welcome-file>
24+
</welcome-file-list>
25+
</web-app>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!DOCTYPE html>
2+
<!--
3+
Copyright 2019 Google LLC
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+
<!-- A basic taskqueue-push.html file served from the "/" URL. -->
18+
<html>
19+
<body>
20+
<p>Enqueue a value, to be processed by a worker.</p>
21+
<form action="/taskqueues/enqueue" method="post">
22+
<input type="text" name="key">
23+
<input type="submit">
24+
</form>
25+
</body>
26+
</html>

0 commit comments

Comments
 (0)