Skip to content

Commit

Permalink
Merge remote-tracking branch 'migration/main' into java-tasks-migration
Browse files Browse the repository at this point in the history
  • Loading branch information
averikitsch committed Oct 28, 2022
2 parents f6055b4 + ef8e09b commit 26f77bf
Show file tree
Hide file tree
Showing 11 changed files with 768 additions and 0 deletions.
57 changes: 57 additions & 0 deletions tasks/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version='1.0' encoding='UTF-8'?>
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-cloudtasks-samples</artifactId>
<version>0.0.1-SNAPSHOT</version><!-- This artifact should not be released -->
<packaging>pom</packaging>
<name>Google Cloud Tasks Samples Parent</name>
<url>https://github.com/googleapis/java-tasks</url>
<description>
Java idiomatic client for Google Cloud Platform services.
</description>

<!--
The parent pom defines common style checks and testing strategies for our samples.
Removing or replacing it should not affect the execution of the samples in anyway.
-->
<parent>
<groupId>com.google.cloud.samples</groupId>
<artifactId>shared-configuration</artifactId>
<version>1.2.0</version>
</parent>

<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<modules>
<module>install-without-bom</module>
<module>snapshot</module>
<module>snippets</module>
<module>native-image-sample</module>
</modules>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.13</version>
<configuration>
<skipNexusStagingDeployMojo>true</skipNexusStagingDeployMojo>
</configuration>
</plugin>
</plugins>
</build>
</project>
60 changes: 60 additions & 0 deletions tasks/snippets/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?xml version='1.0' encoding='UTF-8'?>
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.google.cloud</groupId>
<artifactId>cloudtasks-snippets</artifactId>
<packaging>jar</packaging>
<name>Google Cloud Tasks Snippets</name>
<url>https://github.com/googleapis/java-tasks</url>

<!--
The parent pom defines common style checks and testing strategies for our samples.
Removing or replacing it should not affect the execution of the samples in anyway.
-->
<parent>
<groupId>com.google.cloud.samples</groupId>
<artifactId>shared-configuration</artifactId>
<version>1.2.0</version>
</parent>

<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>


<!-- [START cloud_tasks_install_with_bom] -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>libraries-bom</artifactId>
<version>26.1.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-tasks</artifactId>
</dependency>
<!-- [END cloud_tasks_install_with_bom] -->

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>1.1.3</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
67 changes: 67 additions & 0 deletions tasks/snippets/src/main/java/com/example/task/CreateHttpTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.task;

// [START cloud_tasks_create_http_task]
import com.google.cloud.tasks.v2.CloudTasksClient;
import com.google.cloud.tasks.v2.HttpMethod;
import com.google.cloud.tasks.v2.HttpRequest;
import com.google.cloud.tasks.v2.QueueName;
import com.google.cloud.tasks.v2.Task;
import com.google.protobuf.ByteString;
import java.io.IOException;
import java.nio.charset.Charset;

public class CreateHttpTask {

public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String locationId = "us-central1";
String queueId = "my-queue";
createTask(projectId, locationId, queueId);
}

// Create a task with a HTTP target using the Cloud Tasks client.
public static void createTask(String projectId, String locationId, String queueId)
throws IOException {

// Instantiates a client.
try (CloudTasksClient client = CloudTasksClient.create()) {
String url = "https://example.com/taskhandler";
String payload = "Hello, World!";

// Construct the fully qualified queue name.
String queuePath = QueueName.of(projectId, locationId, queueId).toString();

// Construct the task body.
Task.Builder taskBuilder =
Task.newBuilder()
.setHttpRequest(
HttpRequest.newBuilder()
.setBody(ByteString.copyFrom(payload, Charset.defaultCharset()))
.setUrl(url)
.setHttpMethod(HttpMethod.POST)
.build());

// Send create task request.
Task task = client.createTask(queuePath, taskBuilder.build());
System.out.println("Task created: " + task.getName());
}
}
}
// [END cloud_tasks_create_http_task]
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.task;

// [START cloud_tasks_create_http_task_with_token]
import com.google.cloud.tasks.v2.CloudTasksClient;
import com.google.cloud.tasks.v2.HttpMethod;
import com.google.cloud.tasks.v2.HttpRequest;
import com.google.cloud.tasks.v2.OidcToken;
import com.google.cloud.tasks.v2.QueueName;
import com.google.cloud.tasks.v2.Task;
import com.google.protobuf.ByteString;
import java.io.IOException;
import java.nio.charset.Charset;

public class CreateHttpTaskWithToken {

public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String locationId = "us-central1";
String queueId = "my-queue";
String serviceAccountEmail =
"java-docs-samples-testing@java-docs-samples-testing.iam.gserviceaccount.com";
createTask(projectId, locationId, queueId, serviceAccountEmail);
}

// Create a task with a HTTP target and authorization token using the Cloud Tasks client.
public static void createTask(
String projectId, String locationId, String queueId, String serviceAccountEmail)
throws IOException {

// Instantiates a client.
try (CloudTasksClient client = CloudTasksClient.create()) {
String url =
"https://example.com/taskhandler"; // The full url path that the request will be sent to
String payload = "Hello, World!"; // The task HTTP request body

// Construct the fully qualified queue name.
String queuePath = QueueName.of(projectId, locationId, queueId).toString();

// Add your service account email to construct the OIDC token.
// in order to add an authentication header to the request.
OidcToken.Builder oidcTokenBuilder =
OidcToken.newBuilder().setServiceAccountEmail(serviceAccountEmail);

// Construct the task body.
Task.Builder taskBuilder =
Task.newBuilder()
.setHttpRequest(
HttpRequest.newBuilder()
.setBody(ByteString.copyFrom(payload, Charset.defaultCharset()))
.setHttpMethod(HttpMethod.POST)
.setUrl(url)
.setOidcToken(oidcTokenBuilder)
.build());

// Send create task request.
Task task = client.createTask(queuePath, taskBuilder.build());
System.out.println("Task created: " + task.getName());
}
}
}
// [END cloud_tasks_create_http_task_with_token]
54 changes: 54 additions & 0 deletions tasks/snippets/src/main/java/com/example/task/CreateQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.example.task;

// [START cloud_tasks_create_queue]
import com.google.cloud.tasks.v2.CloudTasksClient;
import com.google.cloud.tasks.v2.LocationName;
import com.google.cloud.tasks.v2.Queue;
import com.google.cloud.tasks.v2.QueueName;
import java.io.IOException;

public class CreateQueue {

public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String locationId = "us-central1";
String queueId = "my-queue";
createQueue(projectId, locationId, queueId);
}

// Create a queue using the Cloud Tasks client.
public static void createQueue(String projectId, String locationId, String queueId)
throws IOException {

// Instantiates a client.
try (CloudTasksClient client = CloudTasksClient.create()) {

// Construct the fully qualified location.
String parent = LocationName.of(projectId, locationId).toString();

// Construct the fully qualified queue path.
String queuePath = QueueName.of(projectId, locationId, queueId).toString();

// Send create queue request.
Queue queue = client.createQueue(parent, Queue.newBuilder().setName(queuePath).build());

System.out.println("Queue created: " + queue.getName());
}
}
}
// [END cloud_tasks_create_queue]
49 changes: 49 additions & 0 deletions tasks/snippets/src/main/java/com/example/task/DeleteQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.example.task;

// [START cloud_tasks_delete_queue]
import com.google.cloud.tasks.v2.CloudTasksClient;
import com.google.cloud.tasks.v2.QueueName;
import java.io.IOException;

public class DeleteQueue {

public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String locationId = "us-central1";
String queueId = "my-queue";
deleteQueue(projectId, locationId, queueId);
}

// Delete a queue using the Cloud Tasks client.
public static void deleteQueue(String projectId, String locationId, String queueId)
throws IOException {

// Instantiates a client.
try (CloudTasksClient client = CloudTasksClient.create()) {

// Construct the fully qualified queue path.
String queuePath = QueueName.of(projectId, locationId, queueId).toString();

// Send delete queue request.
client.deleteQueue(queuePath);

System.out.println("Queue deleted: " + queueId);
}
}
}
// [END cloud_tasks_delete_queue]
Loading

0 comments on commit 26f77bf

Please sign in to comment.