Skip to content

add Java snippets for creating, listing, and deleting queues #3176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions tasks/src/main/java/com/example/task/CreateHttpTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,22 @@
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 {
/**
* Create a task with a HTTP target using the Cloud Tasks client.
*
* @param projectId the Id of the project.
* @param queueId the name of your Queue.
* @param locationId the GCP region of your queue.
* @throws Exception on Cloud Tasks Client errors.
*/

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 Exception {
throws IOException {

// Instantiates a client.
try (CloudTasksClient client = CloudTasksClient.create()) {
Expand Down
24 changes: 14 additions & 10 deletions tasks/src/main/java/com/example/task/CreateHttpTaskWithToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,25 @@
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 {
/**
* Create a task with a HTTP target and authorization token using the Cloud Tasks client.
*
* @param projectId the Id of the project.
* @param queueId the name of your Queue.
* @param locationId the GCP region of your queue.
* @param serviceAccountEmail your Cloud IAM service account
* @throws Exception on Cloud Tasks Client errors.
*/

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 Exception {
throws IOException {

// Instantiates a client.
try (CloudTasksClient client = CloudTasksClient.create()) {
Expand Down
54 changes: 54 additions & 0 deletions tasks/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/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]
58 changes: 58 additions & 0 deletions tasks/src/main/java/com/example/task/ListQueues.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 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_list_queues]
import com.google.cloud.tasks.v2.CloudTasksClient;
import com.google.cloud.tasks.v2.LocationName;
import com.google.cloud.tasks.v2.Queue;
import java.io.IOException;

public class ListQueues {

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";
listQueues(projectId, locationId);
}

// List queues using the Cloud Tasks client.
public static void listQueues(String projectId, String locationId)
throws IOException {

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

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

// Send list queues request.
CloudTasksClient.ListQueuesPagedResponse response = client.listQueues(parent);

// Iterate over results and print queue names
int total = 0;
for (Queue queue : response.iterateAll()) {
System.out.println(queue.getName());
total++;
}

if (total == 0) {
System.out.println("No queues found!");
}
}
}
}
// [END cloud_tasks_list_queues]
18 changes: 16 additions & 2 deletions tasks/src/test/java/com/example/task/CreateHttpTaskIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,42 @@
package com.example.task;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertNotNull;

import com.google.cloud.tasks.v2.CloudTasksClient;
import com.google.cloud.tasks.v2.QueueName;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Tests for creating Tasks with HTTP targets. */
@RunWith(JUnit4.class)
public class CreateHttpTaskIT {
private static final String PROJECT_ID = "java-docs-samples-testing";
private static final String LOCATION_ID = "us-east1";
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
private static final String LOCATION_ID = System.getenv("LOCATION_ID");
private static final String QUEUE_ID = "default";
private static final String EMAIL =
"java-docs-samples-testing@java-docs-samples-testing.iam.gserviceaccount.com";
private ByteArrayOutputStream bout;
private PrintStream out;

private static void requireEnvVar(String varName) {
assertNotNull(
String.format("Environment variable '%s' must be set to perform these tests.", varName),
System.getenv(varName));
}

@BeforeClass
public static void checkRequirements() {
requireEnvVar("GOOGLE_CLOUD_PROJECT");
requireEnvVar("LOCATION_ID");
}

@Before
public void setUp() {
bout = new ByteArrayOutputStream();
Expand Down
78 changes: 78 additions & 0 deletions tasks/src/test/java/com/example/task/CreateQueueIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// 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;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertNotNull;

import com.google.cloud.tasks.v2.CloudTasksClient;
import com.google.cloud.tasks.v2.QueueName;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.UUID;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Tests for creating queues. */
@RunWith(JUnit4.class)
public class CreateQueueIT {
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
private static final String LOCATION_ID = System.getenv("LOCATION_ID");
private static final String QUEUE_ID = "test-queue-" + UUID.randomUUID();

private ByteArrayOutputStream bout;
private PrintStream out;

private static void requireEnvVar(String varName) {
assertNotNull(
String.format("Environment variable '%s' must be set to perform these tests.", varName),
System.getenv(varName));
}

@BeforeClass
public static void checkRequirements() {
requireEnvVar("GOOGLE_CLOUD_PROJECT");
requireEnvVar("LOCATION_ID");
}

@Before
public void setUp() {
bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
System.setOut(out);
}

@After
public void tearDown() {
try (CloudTasksClient client = CloudTasksClient.create()) {
String queuePath = QueueName.of(PROJECT_ID, LOCATION_ID, QUEUE_ID).toString();
client.deleteQueue(queuePath);
} catch (Exception e) {
System.out.println("Error with queue deletion.");
}
System.setOut(null);
}

@Test
public void testCreateQueue() throws Exception {
CreateQueue.createQueue(PROJECT_ID, LOCATION_ID, QUEUE_ID);
String got = bout.toString();
assertThat(got).contains("Queue created:");
}
}
Loading