Search in sources :

Example 6 with CloudTasksClient

use of com.google.cloud.tasks.v2.CloudTasksClient in project java-docs-samples by GoogleCloudPlatform.

the class DeleteTask method deleteTask.

public static void deleteTask(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 = "queue1";
        // String taskId = "foo";
        // Construct the fully qualified queue name.
        String taskName = TaskName.of(projectId, locationId, queueId, taskId).toString();
        client.deleteTask(taskName);
        System.out.println("Task Deleted.");
    }
}
Also used : CloudTasksClient(com.google.cloud.tasks.v2.CloudTasksClient)

Example 7 with CloudTasksClient

use of com.google.cloud.tasks.v2.CloudTasksClient in project java-docs-samples by GoogleCloudPlatform.

the class PauseQueue method pauseQueue.

public static void pauseQueue(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.pauseQueue(queueName);
        System.out.println("Queue Paused.");
    }
}
Also used : CloudTasksClient(com.google.cloud.tasks.v2.CloudTasksClient)

Example 8 with CloudTasksClient

use of com.google.cloud.tasks.v2.CloudTasksClient in project java-docs-samples by GoogleCloudPlatform.

the class Enqueue method doPost.

// Function creates Cloud Tasks from form submissions.
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String key = request.getParameter("key");
    try (CloudTasksClient client = CloudTasksClient.create()) {
        // Construct the fully qualified queue name.
        String queueName = QueueName.of(projectId, locationId, "default").toString();
        // Construct the task body.
        Task task = Task.newBuilder().setAppEngineHttpRequest(AppEngineHttpRequest.newBuilder().setRelativeUri("/cloudtasks/worker?key=" + key).setHttpMethod(HttpMethod.POST).build()).build();
        // Add the task to the default queue.
        Task taskResponse = client.createTask(queueName, task);
        System.out.println("Task created: " + taskResponse.getName());
    }
    response.sendRedirect("/");
}
Also used : Task(com.google.cloud.tasks.v2.Task) CloudTasksClient(com.google.cloud.tasks.v2.CloudTasksClient)

Example 9 with CloudTasksClient

use of com.google.cloud.tasks.v2.CloudTasksClient in project java-tasks by googleapis.

the class CreateHttpTask method createTask.

// 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());
    }
}
Also used : Task(com.google.cloud.tasks.v2.Task) CloudTasksClient(com.google.cloud.tasks.v2.CloudTasksClient) ByteString(com.google.protobuf.ByteString)

Example 10 with CloudTasksClient

use of com.google.cloud.tasks.v2.CloudTasksClient in project java-tasks by googleapis.

the class CreateHttpTaskWithToken method createTask.

// 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 = // The full url path that the request will be sent to
        "https://example.com/taskhandler";
        // The task HTTP request body
        String payload = "Hello, World!";
        // 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());
    }
}
Also used : Task(com.google.cloud.tasks.v2.Task) CloudTasksClient(com.google.cloud.tasks.v2.CloudTasksClient) ByteString(com.google.protobuf.ByteString) OidcToken(com.google.cloud.tasks.v2.OidcToken)

Aggregations

CloudTasksClient (com.google.cloud.tasks.v2.CloudTasksClient)24 Task (com.google.cloud.tasks.v2.Task)10 Queue (com.google.cloud.tasks.v2.Queue)7 ByteString (com.google.protobuf.ByteString)6 LocationName (com.google.cloud.tasks.v2.LocationName)5 IOException (java.io.IOException)4 AppEngineHttpRequest (com.google.cloud.tasks.v2.AppEngineHttpRequest)2 CreateQueueRequest (com.google.cloud.tasks.v2.CreateQueueRequest)2 HttpRequest (com.google.cloud.tasks.v2.HttpRequest)2 QueueName (com.google.cloud.tasks.v2.QueueName)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 PrintStream (java.io.PrintStream)2 After (org.junit.After)2 Before (org.junit.Before)2 AppUrl (teammates.common.util.AppUrl)2 NotFoundException (com.google.api.gax.rpc.NotFoundException)1 OidcToken (com.google.cloud.tasks.v2.OidcToken)1 UpdateQueueRequest (com.google.cloud.tasks.v2.UpdateQueueRequest)1 Duration (com.google.protobuf.Duration)1 CommandLine (org.apache.commons.cli.CommandLine)1