Search in sources :

Example 1 with Task

use of com.google.api.services.cloudtasks.v2beta2.model.Task in project java-docs-samples by GoogleCloudPlatform.

the class PullQueue method main.

// [END cloud_tasks_lease_and_acknowledge_task]
public static void main(String[] args) throws Exception {
    ArgumentParser parser = ArgumentParsers.newFor("PullQueue").build().defaultHelp(true).description("Sample command-line program for interacting with the Cloud Tasks API.\n\n" + "See README.md for instructions on setting up your development environment " + "and running the scripts.");
    Subparsers subparsers = parser.addSubparsers().dest("command");
    // Create the parser for the command 'create-task'
    ArgumentParser createTaskParser = subparsers.addParser("create-task").help("Acknowledge a given task, which removes it from the queue.");
    createTaskParser.addArgument("--project").help("Project of the queue to add the task to.").required(true);
    createTaskParser.addArgument("--location").help("Location of the queue to add the task to.").required(true);
    createTaskParser.addArgument("--queue").help("ID (short name) of the queue to add the task to.").required(true);
    // Create the parser for the command 'pull-and-ack-task'
    ArgumentParser pullAndAckParser = subparsers.addParser("pull-and-ack-task").help("Create a task for a given queue with an arbitrary payload.");
    pullAndAckParser.addArgument("--project").help("Project of the queue to add the task to.").required(true);
    pullAndAckParser.addArgument("--location").help("Location of the queue to add the task to.").required(true);
    pullAndAckParser.addArgument("--queue").help("ID (short name) of the queue to add the task to.").required(true);
    // Parse commands
    Namespace cmd = parser.parseArgs(args);
    String command = cmd.get("command");
    String project = cmd.get("project");
    String queue = cmd.get("queue");
    String location = cmd.get("location");
    // Execute commands
    if (command.equals("create-task")) {
        createTask(project, location, queue);
    }
    if (command.equals("pull-and-ask-task")) {
        Task task = pullTask(project, location, queue);
        acknowledgeTask(task);
    }
}
Also used : Subparsers(net.sourceforge.argparse4j.inf.Subparsers) Task(com.google.api.services.cloudtasks.v2beta2.model.Task) ArgumentParser(net.sourceforge.argparse4j.inf.ArgumentParser) Namespace(net.sourceforge.argparse4j.inf.Namespace)

Example 2 with Task

use of com.google.api.services.cloudtasks.v2beta2.model.Task in project java-docs-samples by GoogleCloudPlatform.

the class PullQueue method createTask.

// [START cloud_tasks_create_task]
/**
 * Create a task for a given queue with a given payload.
 */
private static Task createTask(String project, String location, String queue) throws IOException {
    // The name of the queue to use
    String queueName = String.format("projects/%s/locations/%s/queues/%s", project, location, queue);
    // Create the Cloud Tasks Client
    CloudTasks client = createAuthorizedClient();
    // Create the Task to put in the Queue
    String message = "a message for the recipient";
    String payload = BaseEncoding.base64().encode(message.getBytes());
    Task task = new Task().setPullMessage(new PullMessage().setPayload(payload));
    // Create the CreateTaskRequest
    CreateTaskRequest request = new CreateTaskRequest().setTask(task);
    // Execute the request and return the created Task
    Task result = client.projects().locations().queues().tasks().create(queueName, request).execute();
    System.out.println(String.format("Created task %s", task.getName()));
    return result;
}
Also used : Task(com.google.api.services.cloudtasks.v2beta2.model.Task) CreateTaskRequest(com.google.api.services.cloudtasks.v2beta2.model.CreateTaskRequest) PullMessage(com.google.api.services.cloudtasks.v2beta2.model.PullMessage) CloudTasks(com.google.api.services.cloudtasks.v2beta2.CloudTasks)

Example 3 with Task

use of com.google.api.services.cloudtasks.v2beta2.model.Task in project java-docs-samples by GoogleCloudPlatform.

the class CreateTaskServlet method createTask.

// [START cloud_tasks_appengine_create_task]
/**
 * Create a task for a given queue with a given payload.
 */
private static Task createTask(String project, String location, String queue, String payload) throws IOException {
    // The name of the queue to use
    String queueName = String.format("projects/%s/locations/%s/queues/%s", project, location, queue);
    // Create the Cloud Tasks Client
    CloudTasks client = createAuthorizedClient();
    // Create the Task to put in the Queue
    payload = BaseEncoding.base64().encode(payload.getBytes());
    AppEngineHttpRequest postRequest = new AppEngineHttpRequest().setHttpMethod("POST").setRelativeUrl("/example_task_handler").setPayload(payload);
    Task task = new Task().setAppEngineHttpRequest(postRequest);
    // Create the CreateTaskRequest
    CreateTaskRequest request = new CreateTaskRequest().setTask(task);
    // Execute the request and return the created Task
    Task result = client.projects().locations().queues().tasks().create(queueName, request).execute();
    System.out.println(String.format("Created task %s", task.getName()));
    return result;
}
Also used : AppEngineHttpRequest(com.google.api.services.cloudtasks.v2beta2.model.AppEngineHttpRequest) Task(com.google.api.services.cloudtasks.v2beta2.model.Task) CreateTaskRequest(com.google.api.services.cloudtasks.v2beta2.model.CreateTaskRequest) CloudTasks(com.google.api.services.cloudtasks.v2beta2.CloudTasks)

Example 4 with Task

use of com.google.api.services.cloudtasks.v2beta2.model.Task in project java-docs-samples by GoogleCloudPlatform.

the class PullQueue method acknowledgeTask.

/**
 * Acknowledge a given task, which removes it from the queue.
 */
private static void acknowledgeTask(Task task) throws IOException {
    // Create the Cloud Tasks Client
    CloudTasks client = createAuthorizedClient();
    // Create the AcknowledgeTaskRequest
    AcknowledgeTaskRequest request = new AcknowledgeTaskRequest().setScheduleTime(task.getScheduleTime());
    // Execute the request
    client.projects().locations().queues().tasks().acknowledge(task.getName(), request).execute();
    System.out.println(String.format("Acknowledged task %s", task.getName()));
}
Also used : AcknowledgeTaskRequest(com.google.api.services.cloudtasks.v2beta2.model.AcknowledgeTaskRequest) CloudTasks(com.google.api.services.cloudtasks.v2beta2.CloudTasks)

Example 5 with Task

use of com.google.api.services.cloudtasks.v2beta2.model.Task in project java-docs-samples by GoogleCloudPlatform.

the class PullQueue method pullTask.

// [END cloud_tasks_create_task]
// [START cloud_tasks_lease_and_acknowledge_task]
/**
 * Pull a single task from a given queue and lease it for 10 minutes.
 */
private static Task pullTask(String project, String location, String queue) throws IOException {
    // The name of the queue to use
    String queueName = String.format("projects/%s/locations/%s/queues/%s", project, location, queue);
    // Create the Cloud Tasks Client
    CloudTasks client = createAuthorizedClient();
    // Create the LeaseTasksRequest
    LeaseTasksRequest request = new LeaseTasksRequest().setMaxTasks(1).setLeaseDuration("600s").setResponseView("FULL");
    // Execute the request and return the pulled task
    LeaseTasksResponse response = client.projects().locations().queues().tasks().lease(queueName, request).execute();
    return response.getTasks().get(0);
}
Also used : LeaseTasksResponse(com.google.api.services.cloudtasks.v2beta2.model.LeaseTasksResponse) LeaseTasksRequest(com.google.api.services.cloudtasks.v2beta2.model.LeaseTasksRequest) CloudTasks(com.google.api.services.cloudtasks.v2beta2.CloudTasks)

Aggregations

CloudTasks (com.google.api.services.cloudtasks.v2beta2.CloudTasks)4 Task (com.google.api.services.cloudtasks.v2beta2.model.Task)3 CreateTaskRequest (com.google.api.services.cloudtasks.v2beta2.model.CreateTaskRequest)2 AcknowledgeTaskRequest (com.google.api.services.cloudtasks.v2beta2.model.AcknowledgeTaskRequest)1 AppEngineHttpRequest (com.google.api.services.cloudtasks.v2beta2.model.AppEngineHttpRequest)1 LeaseTasksRequest (com.google.api.services.cloudtasks.v2beta2.model.LeaseTasksRequest)1 LeaseTasksResponse (com.google.api.services.cloudtasks.v2beta2.model.LeaseTasksResponse)1 PullMessage (com.google.api.services.cloudtasks.v2beta2.model.PullMessage)1 ArgumentParser (net.sourceforge.argparse4j.inf.ArgumentParser)1 Namespace (net.sourceforge.argparse4j.inf.Namespace)1 Subparsers (net.sourceforge.argparse4j.inf.Subparsers)1