|
| 1 | +/* |
| 2 | + * Copyright 2018 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.task; |
| 18 | + |
| 19 | +import com.google.cloud.tasks.v2beta2.AppEngineHttpRequest; |
| 20 | +import com.google.cloud.tasks.v2beta2.CloudTasksClient; |
| 21 | +import com.google.cloud.tasks.v2beta2.HttpMethod; |
| 22 | +import com.google.cloud.tasks.v2beta2.QueueName; |
| 23 | +import com.google.cloud.tasks.v2beta2.Task; |
| 24 | +import com.google.common.base.Strings; |
| 25 | +import com.google.protobuf.ByteString; |
| 26 | +import com.google.protobuf.Timestamp; |
| 27 | + |
| 28 | +import java.nio.charset.Charset; |
| 29 | +import java.time.Clock; |
| 30 | +import java.time.Instant; |
| 31 | + |
| 32 | +import org.apache.commons.cli.CommandLine; |
| 33 | +import org.apache.commons.cli.CommandLineParser; |
| 34 | +import org.apache.commons.cli.DefaultParser; |
| 35 | +import org.apache.commons.cli.HelpFormatter; |
| 36 | +import org.apache.commons.cli.Option; |
| 37 | +import org.apache.commons.cli.Options; |
| 38 | +import org.apache.commons.cli.ParseException; |
| 39 | + |
| 40 | +public class CreateTask { |
| 41 | + private static String GGOGLE_CLOUD_PROJECT_KEY = "GOOGLE_CLOUD_PROJECT"; |
| 42 | + |
| 43 | + private static Option PROJECT_ID_OPTION = Option.builder("pid") |
| 44 | + .longOpt("project-id") |
| 45 | + .desc("The Google Cloud Project, if not set as GOOGLE_CLOUD_PROJECT env var.") |
| 46 | + .hasArg() |
| 47 | + .argName("project-id") |
| 48 | + .type(String.class) |
| 49 | + .build(); |
| 50 | + |
| 51 | + private static Option QUEUE_OPTION = Option.builder("q") |
| 52 | + .required() |
| 53 | + .longOpt("queue") |
| 54 | + .desc("The Cloud Tasks queue.") |
| 55 | + .hasArg() |
| 56 | + .argName("queue") |
| 57 | + .type(String.class) |
| 58 | + .build(); |
| 59 | + |
| 60 | + private static Option LOCATION_OPTION = Option.builder("l") |
| 61 | + .required() |
| 62 | + .longOpt("location") |
| 63 | + .desc("The region in which your queue is running.") |
| 64 | + .hasArg() |
| 65 | + .argName("location") |
| 66 | + .type(String.class) |
| 67 | + .build(); |
| 68 | + |
| 69 | + private static Option PAYLOAD_OPTION = Option.builder("p") |
| 70 | + .longOpt("payload") |
| 71 | + .desc("The payload string for the task.") |
| 72 | + .hasArg() |
| 73 | + .argName("payload") |
| 74 | + .type(String.class) |
| 75 | + .build(); |
| 76 | + |
| 77 | + private static Option IN_SECONDS_OPTION = Option.builder("s") |
| 78 | + .longOpt("in-seconds") |
| 79 | + .desc("Schedule time for the task to create.") |
| 80 | + .hasArg() |
| 81 | + .argName("in-seconds") |
| 82 | + .type(int.class) |
| 83 | + .build(); |
| 84 | + |
| 85 | + public static void main(String... args) throws Exception { |
| 86 | + Options options = new Options(); |
| 87 | + options.addOption(PROJECT_ID_OPTION); |
| 88 | + options.addOption(QUEUE_OPTION); |
| 89 | + options.addOption(LOCATION_OPTION); |
| 90 | + options.addOption(PAYLOAD_OPTION); |
| 91 | + options.addOption(IN_SECONDS_OPTION); |
| 92 | + |
| 93 | + if (args.length == 0) { |
| 94 | + printUsage(options); |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + CommandLineParser parser = new DefaultParser(); |
| 99 | + CommandLine params = null; |
| 100 | + try { |
| 101 | + params = parser.parse(options, args); |
| 102 | + } catch (ParseException e) { |
| 103 | + System.err.println("Invalid command line: " + e.getMessage()); |
| 104 | + printUsage(options); |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + String projectId; |
| 109 | + if (params.hasOption("project-id")) { |
| 110 | + projectId = params.getOptionValue("project-id"); |
| 111 | + } else { |
| 112 | + projectId = System.getenv(GGOGLE_CLOUD_PROJECT_KEY); |
| 113 | + } |
| 114 | + if (Strings.isNullOrEmpty(projectId)) { |
| 115 | + printUsage(options); |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + String queueName = params.getOptionValue(QUEUE_OPTION.getOpt()); |
| 120 | + String location = params.getOptionValue(LOCATION_OPTION.getOpt()); |
| 121 | + String payload = params.getOptionValue(PAYLOAD_OPTION.getOpt(), "default payload"); |
| 122 | + |
| 123 | + // [START cloud_tasks_appengine_create_task] |
| 124 | + try (CloudTasksClient client = CloudTasksClient.create()) { |
| 125 | + Task.Builder taskBuilder = Task |
| 126 | + .newBuilder() |
| 127 | + .setAppEngineHttpRequest(AppEngineHttpRequest.newBuilder() |
| 128 | + .setPayload(ByteString.copyFrom(payload, Charset.defaultCharset())) |
| 129 | + .setRelativeUrl("/tasks/create") |
| 130 | + .setHttpMethod(HttpMethod.POST) |
| 131 | + .build()); |
| 132 | + if (params.hasOption(IN_SECONDS_OPTION.getOpt())) { |
| 133 | + int seconds = Integer.parseInt(params.getOptionValue(IN_SECONDS_OPTION.getOpt())); |
| 134 | + taskBuilder.setScheduleTime(Timestamp |
| 135 | + .newBuilder() |
| 136 | + .setSeconds(Instant.now(Clock.systemUTC()).plusSeconds(seconds).getEpochSecond())); |
| 137 | + } |
| 138 | + Task task = client.createTask( |
| 139 | + QueueName.of(projectId, location, queueName).toString(), taskBuilder.build()); |
| 140 | + System.out.println("Task created: " + task.getName()); |
| 141 | + } |
| 142 | + // [END cloud_tasks_appengine_create_task] |
| 143 | + } |
| 144 | + |
| 145 | + private static void printUsage(Options options) { |
| 146 | + HelpFormatter formatter = new HelpFormatter(); |
| 147 | + formatter.printHelp( |
| 148 | + "client", |
| 149 | + "A simple Cloud Tasks command line client that triggers a call to an AppEngine " |
| 150 | + + "endpoint.", |
| 151 | + options, "", true); |
| 152 | + throw new RuntimeException(); |
| 153 | + } |
| 154 | + |
| 155 | +} |
0 commit comments