Search in sources :

Example 1 with QueueName

use of com.google.cloud.tasks.v2.QueueName in project workbench by all-of-us.

the class ForwardingCloudTasksStub method createTaskCallable.

@Override
public UnaryCallable<CreateTaskRequest, Task> createTaskCallable() {
    return new UnaryCallable<CreateTaskRequest, Task>() {

        @Override
        public ApiFuture<Task> futureCall(CreateTaskRequest request, ApiCallContext context) {
            final QueueName queueName = QueueName.parse(request.getParent());
            final AppEngineHttpRequest gaeReq = request.getTask().getAppEngineHttpRequest();
            final Request apiReq = new Request.Builder().url(baseUrl + gaeReq.getRelativeUri()).headers(Headers.of(gaeReq.getHeadersMap())).addHeader("X-AppEngine-QueueName", queueName.getQueue()).post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), gaeReq.getBody().toStringUtf8())).build();
            log.info(String.format("asynchronously forwarding task request for queue '%s', to handler '%s'", queueName.getQueue(), apiReq.url()));
            OkHttpClient client = new OkHttpClient();
            client.setReadTimeout(10, TimeUnit.MINUTES);
            client.newCall(apiReq).enqueue(new Callback() {

                @Override
                public void onFailure(Request request, IOException e) {
                    log.log(Level.SEVERE, "task execution failed", e);
                }

                @Override
                public void onResponse(Response response) {
                }
            });
            return ApiFutures.immediateFuture(request.getTask());
        }
    };
}
Also used : Task(com.google.cloud.tasks.v2.Task) OkHttpClient(com.squareup.okhttp.OkHttpClient) UnaryCallable(com.google.api.gax.rpc.UnaryCallable) CreateTaskRequest(com.google.cloud.tasks.v2.CreateTaskRequest) AppEngineHttpRequest(com.google.cloud.tasks.v2.AppEngineHttpRequest) Request(com.squareup.okhttp.Request) ApiCallContext(com.google.api.gax.rpc.ApiCallContext) IOException(java.io.IOException) AppEngineHttpRequest(com.google.cloud.tasks.v2.AppEngineHttpRequest) Response(com.squareup.okhttp.Response) Callback(com.squareup.okhttp.Callback) CreateTaskRequest(com.google.cloud.tasks.v2.CreateTaskRequest) QueueName(com.google.cloud.tasks.v2.QueueName)

Example 2 with QueueName

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

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

the class CreateTask method createTask.

public static void createTask(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 = "default";
        String key = "key";
        // Construct the fully qualified queue name.
        String queueName = QueueName.of(projectId, locationId, queueId).toString();
        // Construct the task body.
        Task taskParam = Task.newBuilder().setAppEngineHttpRequest(AppEngineHttpRequest.newBuilder().setRelativeUri("/worker?key=" + key).setHttpMethod(HttpMethod.GET).build()).build();
        Task taskPayload = Task.newBuilder().setAppEngineHttpRequest(AppEngineHttpRequest.newBuilder().setBody(ByteString.copyFrom(key, Charset.defaultCharset())).setRelativeUri("/worker").setHttpMethod(HttpMethod.POST).build()).build();
        // Send create task request.
        Task[] tasks = new Task[] { taskParam, taskPayload };
        for (Task task : tasks) {
            Task response = client.createTask(queueName, task);
            System.out.println(response);
        }
    }
}
Also used : Task(com.google.cloud.tasks.v2.Task) CloudTasksClient(com.google.cloud.tasks.v2.CloudTasksClient) ByteString(com.google.protobuf.ByteString)

Example 4 with QueueName

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

the class DeleteQueue method deleteQueue.

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

Example 5 with QueueName

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

Aggregations

CloudTasksClient (com.google.cloud.tasks.v2.CloudTasksClient)7 Task (com.google.cloud.tasks.v2.Task)5 ByteString (com.google.protobuf.ByteString)2 ApiCallContext (com.google.api.gax.rpc.ApiCallContext)1 UnaryCallable (com.google.api.gax.rpc.UnaryCallable)1 AppEngineHttpRequest (com.google.cloud.tasks.v2.AppEngineHttpRequest)1 CreateTaskRequest (com.google.cloud.tasks.v2.CreateTaskRequest)1 QueueName (com.google.cloud.tasks.v2.QueueName)1 Callback (com.squareup.okhttp.Callback)1 OkHttpClient (com.squareup.okhttp.OkHttpClient)1 Request (com.squareup.okhttp.Request)1 Response (com.squareup.okhttp.Response)1 IOException (java.io.IOException)1 CommandLine (org.apache.commons.cli.CommandLine)1 CommandLineParser (org.apache.commons.cli.CommandLineParser)1 DefaultParser (org.apache.commons.cli.DefaultParser)1 Options (org.apache.commons.cli.Options)1 ParseException (org.apache.commons.cli.ParseException)1