use of com.google.cloud.tasks.v2.QueueName in project java-docs-samples by GoogleCloudPlatform.
the class CreateTaskWithName method createTaskWithName.
public static void createTaskWithName(String projectId, String locationId, String queueId, String taskId) throws Exception {
try (CloudTasksClient client = CloudTasksClient.create()) {
// TODO(developer): Uncomment these lines and replace with your values.
// String projectId = "your-project-id";
// String locationId = "us-central1";
// String queueId = "default";
// String taskId = "first-try"
String queueName = QueueName.of(projectId, locationId, queueId).toString();
Task.Builder taskBuilder = Task.newBuilder().setName(TaskName.of(projectId, locationId, queueId, taskId).toString()).setAppEngineHttpRequest(AppEngineHttpRequest.newBuilder().setRelativeUri("/worker").setHttpMethod(HttpMethod.GET).build());
// Send create task request.
Task response = client.createTask(queueName, taskBuilder.build());
System.out.println(response);
}
}
use of com.google.cloud.tasks.v2.QueueName in project java-docs-samples by GoogleCloudPlatform.
the class PurgeQueue method purgeQueue.
public static void purgeQueue(String projectId, String locationId, String queueId) throws Exception {
try (CloudTasksClient client = CloudTasksClient.create()) {
// TODO(developer): Uncomment these lines and replace with your values.
// String projectId = "your-project-id";
// String locationId = "us-central1";
// String queueId = "foo";
// Construct the fully qualified queue name.
String queueName = QueueName.of(projectId, locationId, queueId).toString();
client.purgeQueue(queueName);
System.out.println("Queue Purged.");
}
}
use of com.google.cloud.tasks.v2.QueueName in project java-docs-samples by GoogleCloudPlatform.
the class CreateTask method main.
public static void main(String... args) throws Exception {
Options options = new Options();
options.addOption(PROJECT_ID_OPTION);
options.addOption(QUEUE_OPTION);
options.addOption(LOCATION_OPTION);
options.addOption(PAYLOAD_OPTION);
options.addOption(IN_SECONDS_OPTION);
if (args.length == 0) {
printUsage(options);
return;
}
CommandLineParser parser = new DefaultParser();
CommandLine params = null;
try {
params = parser.parse(options, args);
} catch (ParseException e) {
System.err.println("Invalid command line: " + e.getMessage());
printUsage(options);
return;
}
String projectId;
if (params.hasOption("project-id")) {
projectId = params.getOptionValue("project-id");
} else {
projectId = System.getenv(GOOGLE_CLOUD_PROJECT_KEY);
}
if (Strings.isNullOrEmpty(projectId)) {
printUsage(options);
return;
}
String queueName = params.getOptionValue(QUEUE_OPTION.getOpt());
String location = params.getOptionValue(LOCATION_OPTION.getOpt());
String payload = params.getOptionValue(PAYLOAD_OPTION.getOpt(), "default payload");
// Instantiates a client.
try (CloudTasksClient client = CloudTasksClient.create()) {
// Variables provided by the CLI.
// projectId = "my-project-id";
// queueName = "my-appengine-queue";
// location = "us-central1";
// payload = "hello";
// Construct the fully qualified queue name.
String queuePath = QueueName.of(projectId, location, queueName).toString();
// Construct the task body.
Task.Builder taskBuilder = Task.newBuilder().setAppEngineHttpRequest(AppEngineHttpRequest.newBuilder().setBody(ByteString.copyFrom(payload, Charset.defaultCharset())).setRelativeUri("/tasks/create").setHttpMethod(HttpMethod.POST).build());
if (params.hasOption(IN_SECONDS_OPTION.getOpt())) {
// Add the scheduled time to the request.
int seconds = Integer.parseInt(params.getOptionValue(IN_SECONDS_OPTION.getOpt()));
taskBuilder.setScheduleTime(Timestamp.newBuilder().setSeconds(Instant.now(Clock.systemUTC()).plusSeconds(seconds).getEpochSecond()));
}
// Send create task request.
Task task = client.createTask(queuePath, taskBuilder.build());
System.out.println("Task created: " + task.getName());
}
// [END cloud_tasks_appengine_create_task]
}
use of com.google.cloud.tasks.v2.QueueName in project java-tasks by googleapis.
the class CloudTasksClientTest method listTasksTest.
@Test
public void listTasksTest() throws Exception {
Task responsesElement = Task.newBuilder().build();
ListTasksResponse expectedResponse = ListTasksResponse.newBuilder().setNextPageToken("").addAllTasks(Arrays.asList(responsesElement)).build();
mockCloudTasks.addResponse(expectedResponse);
QueueName parent = QueueName.of("[PROJECT]", "[LOCATION]", "[QUEUE]");
ListTasksPagedResponse pagedListResponse = client.listTasks(parent);
List<Task> resources = Lists.newArrayList(pagedListResponse.iterateAll());
Assert.assertEquals(1, resources.size());
Assert.assertEquals(expectedResponse.getTasksList().get(0), resources.get(0));
List<AbstractMessage> actualRequests = mockCloudTasks.getRequests();
Assert.assertEquals(1, actualRequests.size());
ListTasksRequest actualRequest = ((ListTasksRequest) actualRequests.get(0));
Assert.assertEquals(parent.toString(), actualRequest.getParent());
Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
use of com.google.cloud.tasks.v2.QueueName in project java-tasks by googleapis.
the class TasksSampleApplication method main.
/**
* Runs the Cloud Tasks sample application.
*/
public static void main(String[] args) throws IOException {
String projectId = ServiceOptions.getDefaultProjectId();
LocationName parent = LocationName.of(projectId, LOCATION_ID);
QueueName queueName = QueueName.of(parent.getProject(), parent.getLocation(), GRAALVM_TEST_QUEUE_NAME + UUID.randomUUID().toString());
try (CloudTasksClient client = CloudTasksClient.create()) {
// Create queue
Queue queue = Queue.newBuilder().setName(queueName.toString()).setRateLimits(RateLimits.newBuilder().setMaxConcurrentDispatches(1).build()).build();
CreateQueueRequest createQueueRequest = CreateQueueRequest.newBuilder().setParent(parent.toString()).setQueue(queue).build();
Queue createdQueue = client.createQueue(createQueueRequest);
System.out.println("Test queue ready: " + createdQueue);
// Create task
HttpRequest taskTarget = HttpRequest.newBuilder().setUrl("https://google.com").setHttpMethod(HttpMethod.GET).build();
Task taskRequest = Task.newBuilder().setHttpRequest(taskTarget).build();
Task task = client.createTask(queueName, taskRequest);
System.out.println("Created task: " + task);
// Cleanup
client.purgeQueue(queueName);
System.out.println("Queue purged");
client.deleteQueue(queueName);
System.out.println("Queue deleted");
}
}
Aggregations