Skip to content

Commit b407ebf

Browse files
authored
Add Cloud Tasks snippets (GoogleCloudPlatform#1537)
* Taskqueue migration * Update java example * Add task snippets * Delete old location * Move snippets to separate files * linting * fix project * Remove changes
1 parent e3ca6d6 commit b407ebf

File tree

18 files changed

+723
-1
lines changed

18 files changed

+723
-1
lines changed

appengine-java8/tasks/pom.xml renamed to appengine-java8/tasks/quickstart/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Copyright 2018 Google LLC
2020
<modelVersion>4.0.0</modelVersion>
2121
<packaging>war</packaging>
2222
<version>1.0-SNAPSHOT</version>
23-
<groupId>com.example.appengine</groupId>
23+
<groupId>com.example.task</groupId>
2424
<artifactId>appengine-tasks-j8</artifactId>
2525

2626
<!--
+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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-snippets</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+
<failOnMissingWebXml>false</failOnMissingWebXml>
40+
</properties>
41+
42+
<dependencies>
43+
<!-- Compile/runtime dependencies -->
44+
<dependency>
45+
<groupId>com.google.cloud</groupId>
46+
<artifactId>google-cloud-tasks</artifactId>
47+
<version>1.3.0</version>
48+
</dependency>
49+
<dependency>
50+
<groupId>com.google.protobuf</groupId>
51+
<artifactId>protobuf-java</artifactId>
52+
<version>3.9.0</version>
53+
</dependency>
54+
55+
<!-- Test dependencies -->
56+
<dependency>
57+
<groupId>junit</groupId>
58+
<artifactId>junit</artifactId>
59+
<version>4.13-beta-2</version>
60+
</dependency>
61+
<dependency>
62+
<groupId>com.google.truth</groupId>
63+
<artifactId>truth</artifactId>
64+
<version>0.44</version>
65+
<scope>test</scope>
66+
</dependency>
67+
</dependencies>
68+
69+
<build>
70+
<plugins>
71+
<plugin>
72+
<artifactId>maven-assembly-plugin</artifactId>
73+
<version>3.0.0</version>
74+
<configuration>
75+
<descriptorRefs>
76+
<descriptorRef>jar-with-dependencies</descriptorRef>
77+
</descriptorRefs>
78+
</configuration>
79+
<executions>
80+
<execution>
81+
<id>make-assembly</id> <!-- this is used for inheritance merges -->
82+
<phase>package</phase> <!-- bind to the packaging phase -->
83+
<goals>
84+
<goal>single</goal>
85+
</goals>
86+
</execution>
87+
</executions>
88+
</plugin>
89+
</plugins>
90+
</build>
91+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2019 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 taskqueues_using_yaml]
20+
import com.google.cloud.tasks.v2.AppEngineRouting;
21+
import com.google.cloud.tasks.v2.CloudTasksClient;
22+
import com.google.cloud.tasks.v2.LocationName;
23+
import com.google.cloud.tasks.v2.Queue;
24+
import com.google.cloud.tasks.v2.QueueName;
25+
import com.google.cloud.tasks.v2.RateLimits;
26+
27+
public class CreateQueue {
28+
public static void createQueue(
29+
String projectId, String locationId, String queueBlueName, String queueRedName)
30+
throws Exception {
31+
try (CloudTasksClient client = CloudTasksClient.create()) {
32+
// TODO(developer): Uncomment these lines and replace with your values.
33+
// String projectId = "your-project-id";
34+
// String locationId = "us-central1";
35+
// String queueBlueName = "queue-blue";
36+
// String queueRedName = "queue-red";
37+
38+
LocationName parent = LocationName.of(projectId, locationId);
39+
40+
Queue queueBlue =
41+
Queue.newBuilder()
42+
.setName(QueueName.of(projectId, locationId, queueBlueName).toString())
43+
.setRateLimits(RateLimits.newBuilder().setMaxDispatchesPerSecond(5.0))
44+
.setAppEngineRoutingOverride(
45+
AppEngineRouting.newBuilder().setVersion("v2").setService("task-module"))
46+
.build();
47+
48+
Queue queueRed =
49+
Queue.newBuilder()
50+
.setName(QueueName.of(projectId, locationId, queueRedName).toString())
51+
.setRateLimits(RateLimits.newBuilder().setMaxDispatchesPerSecond(1.0))
52+
.build();
53+
54+
Queue[] queues = new Queue[] {queueBlue, queueRed};
55+
for (Queue queue : queues) {
56+
Queue response = client.createQueue(parent, queue);
57+
System.out.println(response);
58+
}
59+
}
60+
}
61+
}
62+
// [END taskqueues_using_yaml]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2019 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 taskqueues_new_task]
20+
import com.google.cloud.tasks.v2.AppEngineHttpRequest;
21+
import com.google.cloud.tasks.v2.CloudTasksClient;
22+
import com.google.cloud.tasks.v2.HttpMethod;
23+
import com.google.cloud.tasks.v2.QueueName;
24+
import com.google.cloud.tasks.v2.Task;
25+
import com.google.protobuf.ByteString;
26+
import java.nio.charset.Charset;
27+
28+
public class CreateTask {
29+
public static void createTask(String projectId, String locationId, String queueId)
30+
throws Exception {
31+
try (CloudTasksClient client = CloudTasksClient.create()) {
32+
// TODO(developer): Uncomment these lines and replace with your values.
33+
// String projectId = "your-project-id";
34+
// String locationId = "us-central1";
35+
// String queueId = "default";
36+
String key = "key";
37+
38+
// Construct the fully qualified queue name.
39+
String queueName = QueueName.of(projectId, locationId, queueId).toString();
40+
41+
// Construct the task body.
42+
Task taskParam =
43+
Task.newBuilder()
44+
.setAppEngineHttpRequest(
45+
AppEngineHttpRequest.newBuilder()
46+
.setRelativeUri("/worker?key=" + key)
47+
.setHttpMethod(HttpMethod.GET)
48+
.build())
49+
.build();
50+
51+
Task taskPayload =
52+
Task.newBuilder()
53+
.setAppEngineHttpRequest(
54+
AppEngineHttpRequest.newBuilder()
55+
.setBody(ByteString.copyFrom(key, Charset.defaultCharset()))
56+
.setRelativeUri("/worker")
57+
.setHttpMethod(HttpMethod.POST)
58+
.build())
59+
.build();
60+
61+
// Send create task request.
62+
Task[] tasks = new Task[] {taskParam, taskPayload};
63+
for (Task task : tasks) {
64+
Task response = client.createTask(queueName, task);
65+
System.out.println(response);
66+
}
67+
}
68+
}
69+
}
70+
// [END taskqueues_new_task]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2019 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 taskqueues_naming_tasks]
20+
import com.google.cloud.tasks.v2.AppEngineHttpRequest;
21+
import com.google.cloud.tasks.v2.CloudTasksClient;
22+
import com.google.cloud.tasks.v2.HttpMethod;
23+
import com.google.cloud.tasks.v2.QueueName;
24+
import com.google.cloud.tasks.v2.Task;
25+
import com.google.cloud.tasks.v2.TaskName;
26+
27+
public class CreateTaskWithName {
28+
public static void createTaskWithName(
29+
String projectId, String locationId, String queueId, String taskId) throws Exception {
30+
try (CloudTasksClient client = CloudTasksClient.create()) {
31+
// TODO(developer): Uncomment these lines and replace with your values.
32+
// String projectId = "your-project-id";
33+
// String locationId = "us-central1";
34+
// String queueId = "default";
35+
// String taskId = "first-try"
36+
37+
String queueName = QueueName.of(projectId, locationId, queueId).toString();
38+
39+
Task.Builder taskBuilder =
40+
Task.newBuilder()
41+
.setName(TaskName.of(projectId, locationId, queueId, taskId).toString())
42+
.setAppEngineHttpRequest(
43+
AppEngineHttpRequest.newBuilder()
44+
.setRelativeUri("/worker")
45+
.setHttpMethod(HttpMethod.GET)
46+
.build());
47+
48+
// Send create task request.
49+
Task response = client.createTask(queueName, taskBuilder.build());
50+
System.out.println(response);
51+
}
52+
}
53+
}
54+
// [END taskqueues_naming_tasks]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2019 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 taskqueues_delete_queue]
20+
import com.google.cloud.tasks.v2.CloudTasksClient;
21+
import com.google.cloud.tasks.v2.QueueName;
22+
23+
public class DeleteQueue {
24+
public static void deleteQueue(String projectId, String locationId, String queueId)
25+
throws Exception {
26+
try (CloudTasksClient client = CloudTasksClient.create()) {
27+
// TODO(developer): Uncomment these lines and replace with your values.
28+
// String projectId = "your-project-id";
29+
// String locationId = "us-central1";
30+
// String queueId = "foo";
31+
32+
// Construct the fully qualified queue name.
33+
String queueName = QueueName.of(projectId, locationId, queueId).toString();
34+
35+
client.deleteQueue(queueName);
36+
System.out.println("Queue Deleted.");
37+
}
38+
}
39+
}
40+
// [END taskqueues_delete_queue]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2019 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 taskqueues_deleting_tasks]
20+
import com.google.cloud.tasks.v2.CloudTasksClient;
21+
import com.google.cloud.tasks.v2.TaskName;
22+
23+
public class DeleteTask {
24+
public static void deleteTask(String projectId, String locationId, String queueId, String taskId)
25+
throws Exception {
26+
try (CloudTasksClient client = CloudTasksClient.create()) {
27+
// TODO(developer): Uncomment these lines and replace with your values.
28+
// String projectId = "your-project-id";
29+
// String locationId = "us-central1";
30+
// String queueId = "queue1";
31+
// String taskId = "foo";
32+
33+
// Construct the fully qualified queue name.
34+
String taskName = TaskName.of(projectId, locationId, queueId, taskId).toString();
35+
36+
client.deleteTask(taskName);
37+
System.out.println("Task Deleted.");
38+
}
39+
}
40+
}
41+
// [END taskqueues_deleting_tasks]

0 commit comments

Comments
 (0)