Skip to content

Commit 597cf1f

Browse files
committed
Added AppEngine Flexible App for Cloud Tasks API.
1 parent d7ad1fb commit 597cf1f

File tree

11 files changed

+401
-17
lines changed

11 files changed

+401
-17
lines changed

cloud-tasks/README.md

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@ queues, creating tasks, and pulling and acknowledging tasks.
1919
* Download and install [Maven](http://maven.apache.org/install.html).
2020

2121

22-
## Authentication
23-
24-
To authenticate locally, download the
25-
26-
and run the following command
27-
2822
## Creating a queue
2923

3024
To create a queue using the Cloud SDK, use the following gcloud command:
@@ -37,28 +31,32 @@ In this example, the queue will be named `my-pull-queue`.
3731

3832
## Running the Samples
3933

40-
First, build your project with:
34+
From the project folder, build your project with:
35+
4136
```
42-
mvn clean package
37+
mvn clean assembly:single
4338
```
4439

4540
Optionally, you can set up your settings as environment variables:
41+
4642
```
4743
export PROJECT_ID=<YOUR_PROJECT_ID>
48-
export QUEUE_ID=<YOUR_QUEUE_NAME>
4944
export LOCATION_ID=<YOUR_ZONE>
45+
export QUEUE_ID=<YOUR_QUEUE_NAME>
5046
```
5147

5248
Next, create a task for a queue:
49+
5350
```
5451
java -cp target/cloudtasks-1.0.0-jar-with-dependencies.jar \
55-
com.example.PullQueue create-task --project=$PROJECT_ID --queue=$QUEUE_ID --location=$LOCATION_ID
52+
com.example.PullQueue create-task --project=$PROJECT_ID --location=$LOCATION_ID --queue=$QUEUE_ID
5653
```
54+
5755
Finally, pull and acknowledge a task:
5856

5957
```
6058
java -cp target/cloudtasks-1.0.0-jar-with-dependencies.jar \
61-
com.example.PullQueue pull-and-ack-task --project=$PROJECT_ID --queue=$QUEUE_ID --location=$LOCATION_ID
59+
com.example.PullQueue pull-and-ack-task --project=$PROJECT_ID --location=$LOCATION_ID --queue=$QUEUE_ID
6260
```
6361
Note that usually, there would be a processing step in between pulling a task and acknowledging it.
6462

cloud-tasks/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
<project>
1818
<modelVersion>4.0.0</modelVersion>
1919
<groupId>com.example</groupId>
20-
<artifactId>cloud-tasks</artifactId>
21-
<version>1.0</version>
20+
<artifactId>cloudtasks</artifactId>
21+
<version>1.0.0</version>
2222

2323
<parent>
2424
<artifactId>doc-samples</artifactId>

cloud-tasks/src/main/java/com.example.cloudtasks/PullQueue.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import com.google.api.services.cloudtasks.v2beta2.CloudTasksScopes;
2626
import com.google.api.services.cloudtasks.v2beta2.model.AcknowledgeTaskRequest;
2727
import com.google.api.services.cloudtasks.v2beta2.model.CreateTaskRequest;
28-
import com.google.api.services.cloudtasks.v2beta2.model.PullTaskTarget;
28+
import com.google.api.services.cloudtasks.v2beta2.model.PullMessage;
2929
import com.google.api.services.cloudtasks.v2beta2.model.PullTasksRequest;
3030
import com.google.api.services.cloudtasks.v2beta2.model.PullTasksResponse;
3131
import com.google.api.services.cloudtasks.v2beta2.model.Task;
@@ -80,7 +80,7 @@ private static Task createTask(
8080
// Create the Task to put in the Queue
8181
String message = "a message for the recipient";
8282
String payload = BaseEncoding.base64().encode(message.getBytes());
83-
Task task = new Task().setPullTaskTarget(new PullTaskTarget().setPayload(payload));
83+
Task task = new Task().setPullMessage(new PullMessage().setPayload(payload));
8484

8585
// Create the CreateTaskRequest
8686
CreateTaskRequest request = new CreateTaskRequest().setTask(task);

cloud-tasks/src/test/java/com/example/cloudtasks/PullQueueIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class PullQueueIT {
3434
private ByteArrayOutputStream bout;
3535
private PrintStream out;
3636

37-
static final private String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
37+
static final private String PROJECT_ID = "java-docs-samples-cloud-tasks";
3838
static final private String LOCATION_ID = "us-central1";
3939
static final private String QUEUE_ID = "test-queue";
4040

@@ -47,7 +47,7 @@ public void setUp() {
4747
out = new PrintStream(bout);
4848
System.setOut(out);
4949

50-
assertNotNull(System.getenv("GOOGLE_CLOUD_PROJECT"));
50+
assertNotNull(PROJECT_ID);
5151
}
5252

5353
/**

flexible/cloud-tasks/README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Google Cloud Tasks App Engine Queue Samples
2+
3+
This is a sample AppEngine app demonstrating use of the Cloud Tasks API
4+
using App Engine Queues.
5+
6+
App Engine queues push tasks to an App Engine HTTP target. This
7+
application can both create and receive Cloud Tasks.
8+
9+
## Initial Setup
10+
11+
* Set up a Google Cloud Project and enable billing.
12+
* Enable the
13+
[Cloud Tasks API](https://console.cloud.google.com/launcher/details/google/cloudtasks.googleapis.com).
14+
* Download and install the [Cloud SDK](https://cloud.google.com/sdk).
15+
* Download and install [Maven](http://maven.apache.org/install.html).
16+
* Add an [AppEngine](https://pantheon.corp.google.com/appengine) App to the project
17+
18+
## Creating a queue
19+
20+
To create a queue using the Cloud SDK, use the following gcloud command:
21+
22+
```bash
23+
gcloud alpha tasks queues create-appengine-queue my-appengine-queue
24+
```
25+
26+
In this example, the queue will be named `my-appengine-queue`.
27+
28+
### Deploy Sample
29+
30+
To deploy this sample to your AppEngine Project, use the following
31+
command:
32+
33+
```bash
34+
gcloud config set project $YOUR_PROJECT_ID
35+
mvn appengine:deploy
36+
```
37+
38+
You can open a browser to your your application with:
39+
40+
```bash
41+
gcloud app browse
42+
```
43+
44+
You can also stream the logs for the application with:
45+
46+
```bash
47+
gcloud app logs tail -s default
48+
```
49+
50+
## Cloud Tasks in Action
51+
52+
You can create a Cloud Task by sending a POST request to the
53+
application.
54+
55+
Optionally, you can set up your settings as environment variables:
56+
```
57+
export PROJECT_ID=<YOUR_PROJECT_ID>
58+
export LOCATION_ID=<YOUR_ZONE>
59+
export QUEUE_ID=<YOUR_QUEUE_NAME>
60+
```
61+
62+
Next, you can send a POST request to trigger the /create_task endpoint:
63+
```bash
64+
curl -d "project=$PROJECT_ID" \
65+
-d "location=$LOCATION_ID" \
66+
-d "queue=$QUEUE_ID" \
67+
-d "message=Hello World!" \
68+
--request POST https://<YOUR_PROJECT_URL>.appspot.com/
69+
```
70+
71+
This endpoint will create a Cloud Tasks to trigger the `/log_payload`
72+
endpoint, which will be visible the application's logs:
73+
```
74+
Received task with payload: Hello World!
75+
```

flexible/cloud-tasks/pom.xml

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<!--
2+
Copyright 2016 Google Inc.
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+
<!-- [START project] -->
17+
<project>
18+
<modelVersion>4.0.0</modelVersion>
19+
<packaging>war</packaging>
20+
<version>1.0-SNAPSHOT</version>
21+
<groupId>com.example.cloudtasks</groupId>
22+
<artifactId>cloudtasks</artifactId>
23+
24+
<parent>
25+
<artifactId>appengine-flexible</artifactId>
26+
<groupId>com.google.cloud</groupId>
27+
<version>1.0.0</version>
28+
<relativePath>..</relativePath>
29+
</parent>
30+
31+
<properties>
32+
<maven.compiler.target>1.8</maven.compiler.target>
33+
<maven.compiler.source>1.8</maven.compiler.source>
34+
35+
<failOnMissingWebXml>false</failOnMissingWebXml> <!-- REQUIRED -->
36+
37+
<appengine.maven.plugin>1.3.2</appengine.maven.plugin>
38+
<jetty>9.4.4.v20170414</jetty>
39+
40+
</properties>
41+
42+
<dependencies>
43+
<dependency>
44+
<groupId>com.google.apis</groupId>
45+
<artifactId>google-api-services-cloudtasks</artifactId>
46+
<version>v2beta2-rev16-1.23.0</version>
47+
</dependency>
48+
<dependency>
49+
<groupId>com.google.appengine</groupId>
50+
<artifactId>appengine-api-1.0-sdk</artifactId>
51+
<version>1.9.59</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>javax.servlet</groupId>
55+
<artifactId>javax.servlet-api</artifactId>
56+
<version>3.1.0</version>
57+
<type>jar</type>
58+
<scope>provided</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+
67+
<plugin>
68+
<groupId>com.google.cloud.tools</groupId>
69+
<artifactId>appengine-maven-plugin</artifactId>
70+
<version>${appengine.maven.plugin}</version>
71+
<configuration>
72+
<!-- deploy configuration -->
73+
</configuration>
74+
</plugin>
75+
76+
<plugin>
77+
<groupId>org.eclipse.jetty</groupId>
78+
<artifactId>jetty-maven-plugin</artifactId>
79+
<version>${jetty}</version>
80+
</plugin>
81+
82+
</plugins>
83+
</build>
84+
</project>
85+
<!-- [END project] -->
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright 2016 Google Inc.
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+
# [START appyaml]
15+
runtime: java
16+
env: flex
17+
18+
handlers:
19+
- url: /.*
20+
script: this field is required, but ignored
21+
# [END appyaml]

0 commit comments

Comments
 (0)