Skip to content

Commit df8ea9a

Browse files
authored
[Cloud Tasks] Add HTTP push queue sample (GoogleCloudPlatform#1355)
* Add HTTP sample * Fix linting * remove CLI * Move http sample * Fix pom.xml * Update pom and linting * Update tasks version
1 parent 16387e3 commit df8ea9a

File tree

7 files changed

+221
-5
lines changed

7 files changed

+221
-5
lines changed

appengine-java8/tasks/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,15 @@ location is "us-central1").
6969
export LOCATION_ID=<YOUR_ZONE>
7070
```
7171

72+
### Using App Engine Queues
7273
Create a task, targeted at the `/tasks/create` endpoint, with a payload specified:
7374

7475
```
7576
mvn exec:java -Dexec.mainClass="com.example.task.CreateTask" \
7677
-Dexec.args="--project-id $GOOGLE_CLOUD_PROJECT \
77-
--queue $QUEUE_ID --location $LOCATION_ID --payload hello"
78+
--queue $QUEUE_ID \
79+
--location $LOCATION_ID \
80+
--payload hello"
7881
```
7982

8083
The App Engine app serves as a target for the push requests. It has an

appengine-java8/tasks/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Copyright 2018 Google LLC
5252
<dependency>
5353
<groupId>com.google.cloud</groupId>
5454
<artifactId>google-cloud-tasks</artifactId>
55-
<version>0.86.0-beta</version>
55+
<version>0.88.0-beta</version>
5656
</dependency>
5757
<dependency>
5858
<groupId>commons-cli</groupId>

appengine-java8/tasks/src/main/java/com/example/task/CreateTask.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ private static void printUsage(Options options) {
161161
HelpFormatter formatter = new HelpFormatter();
162162
formatter.printHelp(
163163
"client",
164-
"A simple Cloud Tasks command line client that triggers a call to an AppEngine "
165-
+ "endpoint.",
164+
"A simple Cloud Tasks command line client that creates a task with an "
165+
+ "App Engine endpoint.",
166166
options, "", true);
167167
throw new RuntimeException();
168168
}

appengine-java8/tasks/src/main/java/com/example/task/TaskServlet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOEx
3838
String body = req.getReader()
3939
.lines()
4040
.reduce("", (accumulator, actual) -> accumulator + actual);
41-
41+
4242
if (!body.isEmpty()) {
4343
log.info("Request payload: " + body);
4444
String output = String.format("Received task with payload %s", body);

tasks/README.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Google Cloud Tasks App Engine Queue Samples
2+
3+
Sample command-line program for interacting with the Cloud Tasks API
4+
using App Engine queues.
5+
6+
App Engine queues push tasks to an App Engine HTTP target. This directory
7+
contains both the App Engine app to deploy, as well as the snippets to run
8+
locally to push tasks to it, which could also be called on App Engine.
9+
10+
`CreateTask.java` is a simple command-line program to create
11+
tasks to be pushed to the App Engine app.
12+
13+
`TaskServlet.java` is the main App Engine app. This app serves as an endpoint to receive
14+
App Engine task attempts.
15+
16+
17+
## Initial Setup
18+
19+
* Set up a Google Cloud Project and enable billing.
20+
* Enable the
21+
[Cloud Tasks API](https://console.cloud.google.com/launcher/details/google/cloudtasks.googleapis.com).
22+
* Download and install the [Cloud SDK](https://cloud.google.com/sdk).
23+
* Download and install [Maven](http://maven.apache.org/install.html).
24+
* Set up [Google Application Credentials](https://cloud.google.com/docs/authentication/getting-started).
25+
26+
## Creating a queue
27+
28+
To create a queue using the Cloud SDK, use the following gcloud command:
29+
30+
```
31+
gcloud beta tasks queues create-app-engine-queue my-appengine-queue
32+
```
33+
34+
Note: A newly created queue will route to the default App Engine service and
35+
version unless configured to do otherwise.
36+
37+
## Deploying the App Engine app
38+
[Using Maven and the App Engine Plugin](https://cloud.google.com/appengine/docs/flexible/java/using-maven)
39+
& [Maven Plugin Goals and Parameters](https://cloud.google.com/appengine/docs/flexible/java/maven-reference)
40+
41+
```
42+
mvn appengine:deploy
43+
```
44+
45+
## Run the Sample Using the Command Line
46+
47+
Set environment variables:
48+
49+
First, your project ID:
50+
51+
```
52+
export GOOGLE_CLOUD_PROJECT=<YOUR_GOOGLE_CLOUD_PROJECT>
53+
```
54+
55+
Then the queue ID, as specified at queue creation time. Queue IDs already
56+
created can be listed with `gcloud beta tasks queues list`.
57+
58+
```
59+
export QUEUE_ID=my-appengine-queue
60+
```
61+
62+
And finally the location ID, which can be discovered with
63+
`gcloud beta tasks queues describe $QUEUE_ID`, with the location embedded in
64+
the "name" value (for instance, if the name is
65+
"projects/my-project/locations/us-central1/queues/my-appengine-queue", then the
66+
location is "us-central1").
67+
68+
```
69+
export LOCATION_ID=<YOUR_ZONE>
70+
```
71+
72+
### Using HTTP Push Queues
73+
74+
Set an environment variable for the endpoint to your task handler. This is an
75+
example url to send requests to the App Engine task handler:
76+
```
77+
export URL=https://${PROJECT_ID}.appspot.com/tasks/create
78+
```
79+
80+
Running the sample will create a task and send the task to the specific URL
81+
endpoint, with a payload specified:
82+
83+
```
84+
mvn exec:java -Dexec.mainClass="com.example.task.CreateHttpTask"
85+
```

tasks/pom.xml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2018 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>tasks-samples</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+
<relativePath></relativePath>
35+
</parent>
36+
37+
<properties>
38+
<maven.compiler.target>1.8</maven.compiler.target>
39+
<maven.compiler.source>1.8</maven.compiler.source>
40+
<failOnMissingWebXml>false</failOnMissingWebXml>
41+
</properties>
42+
43+
<dependencies>
44+
<!-- Compile/runtime dependencies -->
45+
<dependency>
46+
<groupId>com.google.cloud</groupId>
47+
<artifactId>google-cloud-tasks</artifactId>
48+
<version>0.88.0-beta</version>
49+
</dependency>
50+
</dependencies>
51+
<build>
52+
<plugins>
53+
<plugin>
54+
<groupId>org.codehaus.mojo</groupId>
55+
<artifactId>exec-maven-plugin</artifactId>
56+
<version>1.4.0</version>
57+
<configuration>
58+
<mainClass>com.example.task.CreateHttpTask</mainClass>
59+
<cleanupDaemonThreads>false</cleanupDaemonThreads>
60+
</configuration>
61+
</plugin>
62+
</plugins>
63+
</build>
64+
</project>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2018 Google LLC
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.task;
18+
19+
// [START cloud_tasks_create_http_task]
20+
import com.google.cloud.tasks.v2beta3.CloudTasksClient;
21+
import com.google.cloud.tasks.v2beta3.HttpMethod;
22+
import com.google.cloud.tasks.v2beta3.HttpRequest;
23+
import com.google.cloud.tasks.v2beta3.QueueName;
24+
import com.google.cloud.tasks.v2beta3.Task;
25+
import com.google.protobuf.ByteString;
26+
import java.nio.charset.Charset;
27+
28+
public class CreateHttpTask {
29+
30+
public static void main(String[] args) throws Exception {
31+
String projectId = System.getenv("PROJECT_ID");
32+
String queueName = System.getenv("QUEUE_ID");
33+
String location = System.getenv("LOCATION_ID");
34+
String url = System.getenv("URL");
35+
36+
// Instantiates a client.
37+
try (CloudTasksClient client = CloudTasksClient.create()) {
38+
// Variables provided by the system variables.
39+
// projectId = "my-project-id";
40+
// queueName = "my-appengine-queue";
41+
// location = "us-central1";
42+
// url = "https://<project-id>.appspot.com/tasks/create";
43+
String payload = "hello";
44+
45+
// Construct the fully qualified queue name.
46+
String queuePath = QueueName.of(projectId, location, queueName).toString();
47+
48+
// Construct the task body.
49+
Task.Builder taskBuilder =
50+
Task.newBuilder()
51+
.setHttpRequest(
52+
HttpRequest.newBuilder()
53+
.setBody(ByteString.copyFrom(payload, Charset.defaultCharset()))
54+
.setUrl(url)
55+
.setHttpMethod(HttpMethod.POST)
56+
.build());
57+
58+
// Send create task request.
59+
Task task = client.createTask(queuePath, taskBuilder.build());
60+
System.out.println("Task created: " + task.getName());
61+
}
62+
}
63+
}
64+
// [END cloud_tasks_create_http_task]

0 commit comments

Comments
 (0)