Skip to content

Commit 26f77bf

Browse files
committed
Merge remote-tracking branch 'migration/main' into java-tasks-migration
2 parents f6055b4 + ef8e09b commit 26f77bf

File tree

11 files changed

+768
-0
lines changed

11 files changed

+768
-0
lines changed

tasks/pom.xml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.google.cloud</groupId>
5+
<artifactId>google-cloud-cloudtasks-samples</artifactId>
6+
<version>0.0.1-SNAPSHOT</version><!-- This artifact should not be released -->
7+
<packaging>pom</packaging>
8+
<name>Google Cloud Tasks Samples Parent</name>
9+
<url>https://github.com/googleapis/java-tasks</url>
10+
<description>
11+
Java idiomatic client for Google Cloud Platform services.
12+
</description>
13+
14+
<!--
15+
The parent pom defines common style checks and testing strategies for our samples.
16+
Removing or replacing it should not affect the execution of the samples in anyway.
17+
-->
18+
<parent>
19+
<groupId>com.google.cloud.samples</groupId>
20+
<artifactId>shared-configuration</artifactId>
21+
<version>1.2.0</version>
22+
</parent>
23+
24+
<properties>
25+
<maven.compiler.target>1.8</maven.compiler.target>
26+
<maven.compiler.source>1.8</maven.compiler.source>
27+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
28+
</properties>
29+
30+
<modules>
31+
<module>install-without-bom</module>
32+
<module>snapshot</module>
33+
<module>snippets</module>
34+
<module>native-image-sample</module>
35+
</modules>
36+
37+
<build>
38+
<plugins>
39+
<plugin>
40+
<groupId>org.apache.maven.plugins</groupId>
41+
<artifactId>maven-deploy-plugin</artifactId>
42+
<version>3.0.0</version>
43+
<configuration>
44+
<skip>true</skip>
45+
</configuration>
46+
</plugin>
47+
<plugin>
48+
<groupId>org.sonatype.plugins</groupId>
49+
<artifactId>nexus-staging-maven-plugin</artifactId>
50+
<version>1.6.13</version>
51+
<configuration>
52+
<skipNexusStagingDeployMojo>true</skipNexusStagingDeployMojo>
53+
</configuration>
54+
</plugin>
55+
</plugins>
56+
</build>
57+
</project>

tasks/snippets/pom.xml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.google.cloud</groupId>
5+
<artifactId>cloudtasks-snippets</artifactId>
6+
<packaging>jar</packaging>
7+
<name>Google Cloud Tasks Snippets</name>
8+
<url>https://github.com/googleapis/java-tasks</url>
9+
10+
<!--
11+
The parent pom defines common style checks and testing strategies for our samples.
12+
Removing or replacing it should not affect the execution of the samples in anyway.
13+
-->
14+
<parent>
15+
<groupId>com.google.cloud.samples</groupId>
16+
<artifactId>shared-configuration</artifactId>
17+
<version>1.2.0</version>
18+
</parent>
19+
20+
<properties>
21+
<maven.compiler.target>1.8</maven.compiler.target>
22+
<maven.compiler.source>1.8</maven.compiler.source>
23+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
24+
</properties>
25+
26+
27+
<!-- [START cloud_tasks_install_with_bom] -->
28+
<dependencyManagement>
29+
<dependencies>
30+
<dependency>
31+
<groupId>com.google.cloud</groupId>
32+
<artifactId>libraries-bom</artifactId>
33+
<version>26.1.3</version>
34+
<type>pom</type>
35+
<scope>import</scope>
36+
</dependency>
37+
</dependencies>
38+
</dependencyManagement>
39+
40+
<dependencies>
41+
<dependency>
42+
<groupId>com.google.cloud</groupId>
43+
<artifactId>google-cloud-tasks</artifactId>
44+
</dependency>
45+
<!-- [END cloud_tasks_install_with_bom] -->
46+
47+
<dependency>
48+
<groupId>junit</groupId>
49+
<artifactId>junit</artifactId>
50+
<version>4.13.2</version>
51+
<scope>test</scope>
52+
</dependency>
53+
<dependency>
54+
<groupId>com.google.truth</groupId>
55+
<artifactId>truth</artifactId>
56+
<version>1.1.3</version>
57+
<scope>test</scope>
58+
</dependency>
59+
</dependencies>
60+
</project>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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.v2.CloudTasksClient;
21+
import com.google.cloud.tasks.v2.HttpMethod;
22+
import com.google.cloud.tasks.v2.HttpRequest;
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.io.IOException;
27+
import java.nio.charset.Charset;
28+
29+
public class CreateHttpTask {
30+
31+
public static void main(String[] args) throws IOException {
32+
// TODO(developer): Replace these variables before running the sample.
33+
String projectId = "my-project-id";
34+
String locationId = "us-central1";
35+
String queueId = "my-queue";
36+
createTask(projectId, locationId, queueId);
37+
}
38+
39+
// Create a task with a HTTP target using the Cloud Tasks client.
40+
public static void createTask(String projectId, String locationId, String queueId)
41+
throws IOException {
42+
43+
// Instantiates a client.
44+
try (CloudTasksClient client = CloudTasksClient.create()) {
45+
String url = "https://example.com/taskhandler";
46+
String payload = "Hello, World!";
47+
48+
// Construct the fully qualified queue name.
49+
String queuePath = QueueName.of(projectId, locationId, queueId).toString();
50+
51+
// Construct the task body.
52+
Task.Builder taskBuilder =
53+
Task.newBuilder()
54+
.setHttpRequest(
55+
HttpRequest.newBuilder()
56+
.setBody(ByteString.copyFrom(payload, Charset.defaultCharset()))
57+
.setUrl(url)
58+
.setHttpMethod(HttpMethod.POST)
59+
.build());
60+
61+
// Send create task request.
62+
Task task = client.createTask(queuePath, taskBuilder.build());
63+
System.out.println("Task created: " + task.getName());
64+
}
65+
}
66+
}
67+
// [END cloud_tasks_create_http_task]
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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 cloud_tasks_create_http_task_with_token]
20+
import com.google.cloud.tasks.v2.CloudTasksClient;
21+
import com.google.cloud.tasks.v2.HttpMethod;
22+
import com.google.cloud.tasks.v2.HttpRequest;
23+
import com.google.cloud.tasks.v2.OidcToken;
24+
import com.google.cloud.tasks.v2.QueueName;
25+
import com.google.cloud.tasks.v2.Task;
26+
import com.google.protobuf.ByteString;
27+
import java.io.IOException;
28+
import java.nio.charset.Charset;
29+
30+
public class CreateHttpTaskWithToken {
31+
32+
public static void main(String[] args) throws IOException {
33+
// TODO(developer): Replace these variables before running the sample.
34+
String projectId = "my-project-id";
35+
String locationId = "us-central1";
36+
String queueId = "my-queue";
37+
String serviceAccountEmail =
38+
"java-docs-samples-testing@java-docs-samples-testing.iam.gserviceaccount.com";
39+
createTask(projectId, locationId, queueId, serviceAccountEmail);
40+
}
41+
42+
// Create a task with a HTTP target and authorization token using the Cloud Tasks client.
43+
public static void createTask(
44+
String projectId, String locationId, String queueId, String serviceAccountEmail)
45+
throws IOException {
46+
47+
// Instantiates a client.
48+
try (CloudTasksClient client = CloudTasksClient.create()) {
49+
String url =
50+
"https://example.com/taskhandler"; // The full url path that the request will be sent to
51+
String payload = "Hello, World!"; // The task HTTP request body
52+
53+
// Construct the fully qualified queue name.
54+
String queuePath = QueueName.of(projectId, locationId, queueId).toString();
55+
56+
// Add your service account email to construct the OIDC token.
57+
// in order to add an authentication header to the request.
58+
OidcToken.Builder oidcTokenBuilder =
59+
OidcToken.newBuilder().setServiceAccountEmail(serviceAccountEmail);
60+
61+
// Construct the task body.
62+
Task.Builder taskBuilder =
63+
Task.newBuilder()
64+
.setHttpRequest(
65+
HttpRequest.newBuilder()
66+
.setBody(ByteString.copyFrom(payload, Charset.defaultCharset()))
67+
.setHttpMethod(HttpMethod.POST)
68+
.setUrl(url)
69+
.setOidcToken(oidcTokenBuilder)
70+
.build());
71+
72+
// Send create task request.
73+
Task task = client.createTask(queuePath, taskBuilder.build());
74+
System.out.println("Task created: " + task.getName());
75+
}
76+
}
77+
}
78+
// [END cloud_tasks_create_http_task_with_token]
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2020 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+
package com.example.task;
16+
17+
// [START cloud_tasks_create_queue]
18+
import com.google.cloud.tasks.v2.CloudTasksClient;
19+
import com.google.cloud.tasks.v2.LocationName;
20+
import com.google.cloud.tasks.v2.Queue;
21+
import com.google.cloud.tasks.v2.QueueName;
22+
import java.io.IOException;
23+
24+
public class CreateQueue {
25+
26+
public static void main(String[] args) throws IOException {
27+
// TODO(developer): Replace these variables before running the sample.
28+
String projectId = "my-project-id";
29+
String locationId = "us-central1";
30+
String queueId = "my-queue";
31+
createQueue(projectId, locationId, queueId);
32+
}
33+
34+
// Create a queue using the Cloud Tasks client.
35+
public static void createQueue(String projectId, String locationId, String queueId)
36+
throws IOException {
37+
38+
// Instantiates a client.
39+
try (CloudTasksClient client = CloudTasksClient.create()) {
40+
41+
// Construct the fully qualified location.
42+
String parent = LocationName.of(projectId, locationId).toString();
43+
44+
// Construct the fully qualified queue path.
45+
String queuePath = QueueName.of(projectId, locationId, queueId).toString();
46+
47+
// Send create queue request.
48+
Queue queue = client.createQueue(parent, Queue.newBuilder().setName(queuePath).build());
49+
50+
System.out.println("Queue created: " + queue.getName());
51+
}
52+
}
53+
}
54+
// [END cloud_tasks_create_queue]
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2020 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+
package com.example.task;
16+
17+
// [START cloud_tasks_delete_queue]
18+
import com.google.cloud.tasks.v2.CloudTasksClient;
19+
import com.google.cloud.tasks.v2.QueueName;
20+
import java.io.IOException;
21+
22+
public class DeleteQueue {
23+
24+
public static void main(String[] args) throws IOException {
25+
// TODO(developer): Replace these variables before running the sample.
26+
String projectId = "my-project-id";
27+
String locationId = "us-central1";
28+
String queueId = "my-queue";
29+
deleteQueue(projectId, locationId, queueId);
30+
}
31+
32+
// Delete a queue using the Cloud Tasks client.
33+
public static void deleteQueue(String projectId, String locationId, String queueId)
34+
throws IOException {
35+
36+
// Instantiates a client.
37+
try (CloudTasksClient client = CloudTasksClient.create()) {
38+
39+
// Construct the fully qualified queue path.
40+
String queuePath = QueueName.of(projectId, locationId, queueId).toString();
41+
42+
// Send delete queue request.
43+
client.deleteQueue(queuePath);
44+
45+
System.out.println("Queue deleted: " + queueId);
46+
}
47+
}
48+
}
49+
// [END cloud_tasks_delete_queue]

0 commit comments

Comments
 (0)