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();
}
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());
}
}
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());
}
}
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);
}
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);
}
Aggregations