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