Skip to content

Commit ef3bafd

Browse files
authored
add Java snippets for creating, listing, and deleting queues (#3176)
* add Java snippets for creating, listing, and deleting queues * update licenses * styling fixes for lint * fix test comments and remove oldout * have createhttptask use env var * Change System.getProperty to System.getenv and require LOCATION_ID environment variable as well * specify exception type in throws * add main methods and remove javadoc style comments to adhere with style guide
1 parent f5d2334 commit ef3bafd

File tree

9 files changed

+463
-21
lines changed

9 files changed

+463
-21
lines changed

tasks/src/main/java/com/example/task/CreateHttpTask.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,22 @@
2323
import com.google.cloud.tasks.v2.QueueName;
2424
import com.google.cloud.tasks.v2.Task;
2525
import com.google.protobuf.ByteString;
26+
import java.io.IOException;
2627
import java.nio.charset.Charset;
2728

2829
public class CreateHttpTask {
29-
/**
30-
* Create a task with a HTTP target using the Cloud Tasks client.
31-
*
32-
* @param projectId the Id of the project.
33-
* @param queueId the name of your Queue.
34-
* @param locationId the GCP region of your queue.
35-
* @throws Exception on Cloud Tasks Client errors.
36-
*/
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.
3740
public static void createTask(String projectId, String locationId, String queueId)
38-
throws Exception {
41+
throws IOException {
3942

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

tasks/src/main/java/com/example/task/CreateHttpTaskWithToken.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,25 @@
2424
import com.google.cloud.tasks.v2.QueueName;
2525
import com.google.cloud.tasks.v2.Task;
2626
import com.google.protobuf.ByteString;
27+
import java.io.IOException;
2728
import java.nio.charset.Charset;
2829

2930
public class CreateHttpTaskWithToken {
30-
/**
31-
* Create a task with a HTTP target and authorization token using the Cloud Tasks client.
32-
*
33-
* @param projectId the Id of the project.
34-
* @param queueId the name of your Queue.
35-
* @param locationId the GCP region of your queue.
36-
* @param serviceAccountEmail your Cloud IAM service account
37-
* @throws Exception on Cloud Tasks Client errors.
38-
*/
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.
3943
public static void createTask(
4044
String projectId, String locationId, String queueId, String serviceAccountEmail)
41-
throws Exception {
45+
throws IOException {
4246

4347
// Instantiates a client.
4448
try (CloudTasksClient client = CloudTasksClient.create()) {
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]
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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_list_queues]
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 java.io.IOException;
22+
23+
public class ListQueues {
24+
25+
public static void main(String[] args) throws IOException {
26+
// TODO(developer): Replace these variables before running the sample.
27+
String projectId = "my-project-id";
28+
String locationId = "us-central1";
29+
listQueues(projectId, locationId);
30+
}
31+
32+
// List queues using the Cloud Tasks client.
33+
public static void listQueues(String projectId, String locationId)
34+
throws IOException {
35+
36+
// Instantiates a client.
37+
try (CloudTasksClient client = CloudTasksClient.create()) {
38+
39+
// Construct the fully qualified location path.
40+
String parent = LocationName.of(projectId, locationId).toString();
41+
42+
// Send list queues request.
43+
CloudTasksClient.ListQueuesPagedResponse response = client.listQueues(parent);
44+
45+
// Iterate over results and print queue names
46+
int total = 0;
47+
for (Queue queue : response.iterateAll()) {
48+
System.out.println(queue.getName());
49+
total++;
50+
}
51+
52+
if (total == 0) {
53+
System.out.println("No queues found!");
54+
}
55+
}
56+
}
57+
}
58+
// [END cloud_tasks_list_queues]

tasks/src/test/java/com/example/task/CreateHttpTaskIT.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,42 @@
1717
package com.example.task;
1818

1919
import static com.google.common.truth.Truth.assertThat;
20+
import static org.junit.Assert.assertNotNull;
2021

2122
import com.google.cloud.tasks.v2.CloudTasksClient;
2223
import com.google.cloud.tasks.v2.QueueName;
2324
import java.io.ByteArrayOutputStream;
2425
import java.io.PrintStream;
2526
import org.junit.After;
2627
import org.junit.Before;
28+
import org.junit.BeforeClass;
2729
import org.junit.Test;
2830
import org.junit.runner.RunWith;
2931
import org.junit.runners.JUnit4;
3032

3133
/** Tests for creating Tasks with HTTP targets. */
3234
@RunWith(JUnit4.class)
3335
public class CreateHttpTaskIT {
34-
private static final String PROJECT_ID = "java-docs-samples-testing";
35-
private static final String LOCATION_ID = "us-east1";
36+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
37+
private static final String LOCATION_ID = System.getenv("LOCATION_ID");
3638
private static final String QUEUE_ID = "default";
3739
private static final String EMAIL =
3840
"java-docs-samples-testing@java-docs-samples-testing.iam.gserviceaccount.com";
3941
private ByteArrayOutputStream bout;
4042
private PrintStream out;
4143

44+
private static void requireEnvVar(String varName) {
45+
assertNotNull(
46+
String.format("Environment variable '%s' must be set to perform these tests.", varName),
47+
System.getenv(varName));
48+
}
49+
50+
@BeforeClass
51+
public static void checkRequirements() {
52+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
53+
requireEnvVar("LOCATION_ID");
54+
}
55+
4256
@Before
4357
public void setUp() {
4458
bout = new ByteArrayOutputStream();
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
import static com.google.common.truth.Truth.assertThat;
18+
import static org.junit.Assert.assertNotNull;
19+
20+
import com.google.cloud.tasks.v2.CloudTasksClient;
21+
import com.google.cloud.tasks.v2.QueueName;
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.PrintStream;
24+
import java.util.UUID;
25+
import org.junit.After;
26+
import org.junit.Before;
27+
import org.junit.BeforeClass;
28+
import org.junit.Test;
29+
import org.junit.runner.RunWith;
30+
import org.junit.runners.JUnit4;
31+
32+
/** Tests for creating queues. */
33+
@RunWith(JUnit4.class)
34+
public class CreateQueueIT {
35+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
36+
private static final String LOCATION_ID = System.getenv("LOCATION_ID");
37+
private static final String QUEUE_ID = "test-queue-" + UUID.randomUUID();
38+
39+
private ByteArrayOutputStream bout;
40+
private PrintStream out;
41+
42+
private static void requireEnvVar(String varName) {
43+
assertNotNull(
44+
String.format("Environment variable '%s' must be set to perform these tests.", varName),
45+
System.getenv(varName));
46+
}
47+
48+
@BeforeClass
49+
public static void checkRequirements() {
50+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
51+
requireEnvVar("LOCATION_ID");
52+
}
53+
54+
@Before
55+
public void setUp() {
56+
bout = new ByteArrayOutputStream();
57+
out = new PrintStream(bout);
58+
System.setOut(out);
59+
}
60+
61+
@After
62+
public void tearDown() {
63+
try (CloudTasksClient client = CloudTasksClient.create()) {
64+
String queuePath = QueueName.of(PROJECT_ID, LOCATION_ID, QUEUE_ID).toString();
65+
client.deleteQueue(queuePath);
66+
} catch (Exception e) {
67+
System.out.println("Error with queue deletion.");
68+
}
69+
System.setOut(null);
70+
}
71+
72+
@Test
73+
public void testCreateQueue() throws Exception {
74+
CreateQueue.createQueue(PROJECT_ID, LOCATION_ID, QUEUE_ID);
75+
String got = bout.toString();
76+
assertThat(got).contains("Queue created:");
77+
}
78+
}

0 commit comments

Comments
 (0)