Skip to content

Commit 633b9d6

Browse files
authored
Update Cloud tasks samples (#1211)
* update sample * remove pull queues * updates for beta * Change comment * revert env var name
1 parent 85ef6f2 commit 633b9d6

File tree

8 files changed

+40
-445
lines changed

8 files changed

+40
-445
lines changed

appengine-java8/tasks/README.md

+4-14
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ App Engine task attempts.
2121
[Cloud Tasks API](https://console.cloud.google.com/launcher/details/google/cloudtasks.googleapis.com).
2222
* Download and install the [Cloud SDK](https://cloud.google.com/sdk).
2323
* Download and install [Maven](http://maven.apache.org/install.html).
24+
* Set up [Google Application Credentials](https://cloud.google.com/docs/authentication/getting-started).
2425

2526
## Creating a queue
2627

@@ -37,25 +38,18 @@ version unless configured to do otherwise.
3738
[Using Maven and the App Engine Plugin](https://cloud.google.com/appengine/docs/flexible/java/using-maven)
3839
& [Maven Plugin Goals and Parameters](https://cloud.google.com/appengine/docs/flexible/java/maven-reference)
3940

40-
### Running locally
41-
42-
```
43-
mvn appengine:run
44-
```
45-
### Deploying
46-
4741
```
4842
mvn appengine:deploy
4943
```
5044

51-
## Running the Sample
45+
## Run the Sample Using the Command Line
5246

5347
Set environment variables:
5448

5549
First, your project ID:
5650

5751
```
58-
export GOOGLE_CLOUD_PROJECT=<YOUR_PROJECT_ID>
52+
export GOOGLE_CLOUD_PROJECT=<YOUR_GOOGLE_CLOUD_PROJECT>
5953
```
6054

6155
Then the queue ID, as specified at queue creation time. Queue IDs already
@@ -85,11 +79,7 @@ mvn exec:java -Dexec.mainClass="com.example.task.CreateTask" \
8579

8680
The App Engine app serves as a target for the push requests. It has an
8781
endpoint `/tasks/create` that reads the payload (i.e., the request body) of the
88-
HTTP POST request and logs it. The log output can be viewed with:
89-
90-
```
91-
gcloud app logs read
92-
```
82+
HTTP POST request and logs it. The log output can be viewed with [Stackdriver Logging](https://console.cloud.google.com/logs/viewer?minLogLevel=0).
9383

9484
Create a task that will be scheduled for a time in the future using the
9585
`--in-seconds` flag:

appengine-java8/tasks/pom.xml

+3-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Copyright 2018 Google LLC
3737
<properties>
3838
<maven.compiler.target>1.8</maven.compiler.target>
3939
<maven.compiler.source>1.8</maven.compiler.source>
40+
<failOnMissingWebXml>false</failOnMissingWebXml>
4041
</properties>
4142

4243
<dependencies>
@@ -51,7 +52,7 @@ Copyright 2018 Google LLC
5152
<dependency>
5253
<groupId>com.google.cloud</groupId>
5354
<artifactId>google-cloud-tasks</artifactId>
54-
<version>0.54.0-beta</version>
55+
<version>0.61.0-beta</version>
5556
</dependency>
5657
<dependency>
5758
<groupId>commons-cli</groupId>
@@ -69,7 +70,7 @@ Copyright 2018 Google LLC
6970
<plugin>
7071
<groupId>com.google.cloud.tools</groupId>
7172
<artifactId>appengine-maven-plugin</artifactId>
72-
<version>1.3.1</version>
73+
<version>1.3.2</version>
7374
<configuration>
7475
<deploy.promote>true</deploy.promote>
7576
<deploy.stopPreviousVersion>true</deploy.stopPreviousVersion>

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

+26-11
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616

1717
package com.example.task;
1818

19-
import com.google.cloud.tasks.v2beta2.AppEngineHttpRequest;
20-
import com.google.cloud.tasks.v2beta2.CloudTasksClient;
21-
import com.google.cloud.tasks.v2beta2.HttpMethod;
22-
import com.google.cloud.tasks.v2beta2.QueueName;
23-
import com.google.cloud.tasks.v2beta2.Task;
19+
import com.google.cloud.tasks.v2beta3.AppEngineHttpRequest;
20+
import com.google.cloud.tasks.v2beta3.CloudTasksClient;
21+
import com.google.cloud.tasks.v2beta3.HttpMethod;
22+
import com.google.cloud.tasks.v2beta3.QueueName;
23+
import com.google.cloud.tasks.v2beta3.Task;
2424
import com.google.common.base.Strings;
2525
import com.google.protobuf.ByteString;
2626
import com.google.protobuf.Timestamp;
@@ -38,7 +38,7 @@
3838
import org.apache.commons.cli.ParseException;
3939

4040
public class CreateTask {
41-
private static String GGOGLE_CLOUD_PROJECT_KEY = "GOOGLE_CLOUD_PROJECT";
41+
private static String GOOGLE_CLOUD_PROJECT_KEY = "GOOGLE_CLOUD_PROJECT";
4242

4343
private static Option PROJECT_ID_OPTION = Option.builder("pid")
4444
.longOpt("project-id")
@@ -109,7 +109,7 @@ public static void main(String... args) throws Exception {
109109
if (params.hasOption("project-id")) {
110110
projectId = params.getOptionValue("project-id");
111111
} else {
112-
projectId = System.getenv(GGOGLE_CLOUD_PROJECT_KEY);
112+
projectId = System.getenv(GOOGLE_CLOUD_PROJECT_KEY);
113113
}
114114
if (Strings.isNullOrEmpty(projectId)) {
115115
printUsage(options);
@@ -121,22 +121,37 @@ public static void main(String... args) throws Exception {
121121
String payload = params.getOptionValue(PAYLOAD_OPTION.getOpt(), "default payload");
122122

123123
// [START cloud_tasks_appengine_create_task]
124+
// Instantiates a client.
124125
try (CloudTasksClient client = CloudTasksClient.create()) {
126+
127+
// Variables provided by the CLI.
128+
// projectId = "my-project-id";
129+
// queueName = "my-appengine-queue";
130+
// location = "us-central1";
131+
// payload = "hello";
132+
133+
// Construct the fully qualified queue name.
134+
String queuePath = QueueName.of(projectId, location, queueName).toString();
135+
136+
// Construct the task body.
125137
Task.Builder taskBuilder = Task
126138
.newBuilder()
127139
.setAppEngineHttpRequest(AppEngineHttpRequest.newBuilder()
128-
.setPayload(ByteString.copyFrom(payload, Charset.defaultCharset()))
129-
.setRelativeUrl("/tasks/create")
140+
.setBody(ByteString.copyFrom(payload, Charset.defaultCharset()))
141+
.setRelativeUri("/tasks/create")
130142
.setHttpMethod(HttpMethod.POST)
131143
.build());
144+
132145
if (params.hasOption(IN_SECONDS_OPTION.getOpt())) {
146+
// Add the scheduled time to the request.
133147
int seconds = Integer.parseInt(params.getOptionValue(IN_SECONDS_OPTION.getOpt()));
134148
taskBuilder.setScheduleTime(Timestamp
135149
.newBuilder()
136150
.setSeconds(Instant.now(Clock.systemUTC()).plusSeconds(seconds).getEpochSecond()));
137151
}
138-
Task task = client.createTask(
139-
QueueName.of(projectId, location, queueName).toString(), taskBuilder.build());
152+
153+
// Send create task request.
154+
Task task = client.createTask(queuePath, taskBuilder.build());
140155
System.out.println("Task created: " + task.getName());
141156
}
142157
// [END cloud_tasks_appengine_create_task]

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

+7-4
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,13 @@ public class TaskServlet extends HttpServlet {
3535
@Override
3636
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
3737
log.info("Received task request: " + req.getServletPath());
38-
if (req.getParameter("payload") != null) {
39-
String payload = req.getParameter("payload");
40-
log.info("Request payload: " + payload);
41-
String output = String.format("Received task with payload %s", payload);
38+
String body = req.getReader()
39+
.lines()
40+
.reduce("", (accumulator, actual) -> accumulator + actual);
41+
42+
if (!body.isEmpty()) {
43+
log.info("Request payload: " + body);
44+
String output = String.format("Received task with payload %s", body);
4245
resp.getOutputStream().write(output.getBytes());
4346
log.info("Sending response: " + output);
4447
resp.setStatus(HttpServletResponse.SC_OK);

tasks/README.md

-66
This file was deleted.

tasks/pom.xml

-91
This file was deleted.

0 commit comments

Comments
 (0)