Search in sources :

Example 11 with CloudTasksClient

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

the class CreateTask method main.

public static void main(String... args) throws Exception {
    Options options = new Options();
    options.addOption(PROJECT_ID_OPTION);
    options.addOption(QUEUE_OPTION);
    options.addOption(LOCATION_OPTION);
    options.addOption(PAYLOAD_OPTION);
    options.addOption(IN_SECONDS_OPTION);
    if (args.length == 0) {
        printUsage(options);
        return;
    }
    CommandLineParser parser = new DefaultParser();
    CommandLine params = null;
    try {
        params = parser.parse(options, args);
    } catch (ParseException e) {
        System.err.println("Invalid command line: " + e.getMessage());
        printUsage(options);
        return;
    }
    String projectId;
    if (params.hasOption("project-id")) {
        projectId = params.getOptionValue("project-id");
    } else {
        projectId = System.getenv(GOOGLE_CLOUD_PROJECT_KEY);
    }
    if (Strings.isNullOrEmpty(projectId)) {
        printUsage(options);
        return;
    }
    String queueName = params.getOptionValue(QUEUE_OPTION.getOpt());
    String location = params.getOptionValue(LOCATION_OPTION.getOpt());
    String payload = params.getOptionValue(PAYLOAD_OPTION.getOpt(), "default payload");
    // Instantiates a client.
    try (CloudTasksClient client = CloudTasksClient.create()) {
        // Variables provided by the CLI.
        // projectId = "my-project-id";
        // queueName = "my-appengine-queue";
        // location = "us-central1";
        // payload = "hello";
        // Construct the fully qualified queue name.
        String queuePath = QueueName.of(projectId, location, queueName).toString();
        // Construct the task body.
        Task.Builder taskBuilder = Task.newBuilder().setAppEngineHttpRequest(AppEngineHttpRequest.newBuilder().setBody(ByteString.copyFrom(payload, Charset.defaultCharset())).setRelativeUri("/tasks/create").setHttpMethod(HttpMethod.POST).build());
        if (params.hasOption(IN_SECONDS_OPTION.getOpt())) {
            // Add the scheduled time to the request.
            int seconds = Integer.parseInt(params.getOptionValue(IN_SECONDS_OPTION.getOpt()));
            taskBuilder.setScheduleTime(Timestamp.newBuilder().setSeconds(Instant.now(Clock.systemUTC()).plusSeconds(seconds).getEpochSecond()));
        }
        // Send create task request.
        Task task = client.createTask(queuePath, taskBuilder.build());
        System.out.println("Task created: " + task.getName());
    }
// [END cloud_tasks_appengine_create_task]
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) Task(com.google.cloud.tasks.v2.Task) CloudTasksClient(com.google.cloud.tasks.v2.CloudTasksClient) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) ByteString(com.google.protobuf.ByteString) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 12 with CloudTasksClient

use of com.google.cloud.tasks.v2.CloudTasksClient in project teammates by TEAMMATES.

the class GoogleCloudTasksService method addDeferredTask.

@Override
public void addDeferredTask(TaskWrapper task, long countdownTime) {
    try (CloudTasksClient client = CloudTasksClient.create()) {
        String queuePath = QueueName.of(Config.APP_ID, Config.APP_REGION, task.getQueueName()).toString();
        AppEngineHttpRequest.Builder requestBuilder = AppEngineHttpRequest.newBuilder().setAppEngineRouting(AppEngineRouting.newBuilder().setVersion(Config.APP_VERSION).build()).setHttpMethod(HttpMethod.POST);
        if (task.getRequestBody() == null) {
            // the value is not important
            String relativeUrl = "http://place.holder";
            AppUrl url = new AppUrl(relativeUrl + task.getWorkerUrl());
            task.getParamMap().forEach((key, value) -> url.withParam(key, value));
            requestBuilder.setRelativeUri(url.toString());
        } else {
            String requestBody = JsonUtils.toCompactJson(task.getRequestBody());
            requestBuilder.putHeaders("Content-Type", "application/json; charset=UTF-8").setRelativeUri(task.getWorkerUrl()).setBody(ByteString.copyFrom(requestBody, Const.ENCODING));
        }
        Task.Builder taskBuilder = Task.newBuilder().setAppEngineHttpRequest(requestBuilder.build());
        if (countdownTime > 0) {
            taskBuilder.setScheduleTime(Timestamp.newBuilder().setSeconds(Instant.now().plusMillis(countdownTime).getEpochSecond()));
        }
        client.createTask(queuePath, taskBuilder.build());
    } catch (IOException e) {
        log.severe("Cannot create Cloud Tasks client", e);
    }
}
Also used : AppEngineHttpRequest(com.google.cloud.tasks.v2.AppEngineHttpRequest) Task(com.google.cloud.tasks.v2.Task) AppUrl(teammates.common.util.AppUrl) CloudTasksClient(com.google.cloud.tasks.v2.CloudTasksClient) ByteString(com.google.protobuf.ByteString) IOException(java.io.IOException)

Aggregations

CloudTasksClient (com.google.cloud.tasks.v2.CloudTasksClient)12 Task (com.google.cloud.tasks.v2.Task)5 LocationName (com.google.cloud.tasks.v2.LocationName)3 Queue (com.google.cloud.tasks.v2.Queue)3 ByteString (com.google.protobuf.ByteString)3 AppEngineHttpRequest (com.google.cloud.tasks.v2.AppEngineHttpRequest)1 UpdateQueueRequest (com.google.cloud.tasks.v2.UpdateQueueRequest)1 Duration (com.google.protobuf.Duration)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 AppUrl (teammates.common.util.AppUrl)1