Search in sources :

Example 16 with Task

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

the class ITSystemTest method tearDown.

@AfterClass
public static void tearDown() {
    /* delete task */
    DeleteTaskRequest taskRequest = DeleteTaskRequest.newBuilder().setName(TASK_NAME).build();
    client.deleteTask(taskRequest);
    /* delete queue */
    DeleteQueueRequest queueRequest = DeleteQueueRequest.newBuilder().setName(QUEUE_NAME).build();
    client.deleteQueue(queueRequest);
    client.close();
}
Also used : DeleteQueueRequest(com.google.cloud.tasks.v2.DeleteQueueRequest) DeleteTaskRequest(com.google.cloud.tasks.v2.DeleteTaskRequest) AfterClass(org.junit.AfterClass)

Example 17 with Task

use of com.google.cloud.tasks.v2.Task 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 18 with Task

use of com.google.cloud.tasks.v2.Task 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)

Example 19 with Task

use of com.google.cloud.tasks.v2.Task in project nomulus by google.

the class CloudTasksUtilsTest method testSuccess_enqueueTasks_varargs.

@Test
void testSuccess_enqueueTasks_varargs() {
    Task task1 = cloudTasksUtils.createGetTask("/the/path", "myservice", params);
    Task task2 = cloudTasksUtils.createGetTask("/other/path", "yourservice", params);
    cloudTasksUtils.enqueue("test-queue", task1, task2);
    verify(mockClient).enqueue("project", "location", "test-queue", task1);
    verify(mockClient).enqueue("project", "location", "test-queue", task2);
}
Also used : Task(com.google.cloud.tasks.v2.Task) Test(org.junit.jupiter.api.Test)

Example 20 with Task

use of com.google.cloud.tasks.v2.Task in project nomulus by google.

the class CloudTasksUtilsTest method testSuccess_createGetTasks_withZeroDelay.

@Test
void testSuccess_createGetTasks_withZeroDelay() {
    Task task = cloudTasksUtils.createGetTaskWithDelay("/the/path", "myservice", params, Duration.ZERO);
    assertThat(task.getAppEngineHttpRequest().getHttpMethod()).isEqualTo(HttpMethod.GET);
    assertThat(task.getAppEngineHttpRequest().getRelativeUri()).isEqualTo("/the/path?key1=val1&key2=val2&key1=val3");
    assertThat(task.getAppEngineHttpRequest().getAppEngineRouting().getService()).isEqualTo("myservice");
    assertThat(task.getScheduleTime().getSeconds()).isEqualTo(0);
}
Also used : Task(com.google.cloud.tasks.v2.Task) Test(org.junit.jupiter.api.Test)

Aggregations

Task (com.google.cloud.tasks.v2.Task)33 Test (org.junit.jupiter.api.Test)23 Task (org.eclipse.bpmn2.Task)17 Test (org.junit.Test)14 CloudTasksClient (com.google.cloud.tasks.v2.CloudTasksClient)11 Task (io.fabric8.tekton.pipeline.v1beta1.Task)8 ByteString (com.google.protobuf.ByteString)7 IOException (java.io.IOException)6 Optional (java.util.Optional)5 FlowElement (org.eclipse.bpmn2.FlowElement)5 InputSet (org.eclipse.bpmn2.InputSet)5 AssignmentsInfo (org.kie.workbench.common.stunner.bpmn.definition.property.dataio.AssignmentsInfo)5 Queue (com.google.cloud.tasks.v2.Queue)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 AdHocSubProcess (org.eclipse.bpmn2.AdHocSubProcess)4 BusinessRuleTask (org.eclipse.bpmn2.BusinessRuleTask)4 GlobalTask (org.eclipse.bpmn2.GlobalTask)4 OutputSet (org.eclipse.bpmn2.OutputSet)4 Process (org.eclipse.bpmn2.Process)4