1616
1717package com .example .task ;
1818
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 ;
19+ import com .google .cloud .tasks .v2beta3 .AppEngineHttpRequest ;
20+ import com .google .cloud .tasks .v2beta3 .CloudTasksClient ;
21+ import com .google .cloud .tasks .v2beta3 .HttpMethod ;
22+ import com .google .cloud .tasks .v2beta3 .QueueName ;
23+ import com .google .cloud .tasks .v2beta3 .Task ;
2424import com .google .common .base .Strings ;
2525import com .google .protobuf .ByteString ;
2626import com .google .protobuf .Timestamp ;
3838import org .apache .commons .cli .ParseException ;
3939
4040public class CreateTask {
41- private static String GGOGLE_CLOUD_PROJECT_KEY = "GOOGLE_CLOUD_PROJECT" ;
41+ private static String GOOGLE_CLOUD_PROJECT_KEY = "GOOGLE_CLOUD_PROJECT" ;
4242
4343 private static Option PROJECT_ID_OPTION = Option .builder ("pid" )
4444 .longOpt ("project-id" )
@@ -109,7 +109,7 @@ public static void main(String... args) throws Exception {
109109 if (params .hasOption ("project-id" )) {
110110 projectId = params .getOptionValue ("project-id" );
111111 } else {
112- projectId = System .getenv (GGOGLE_CLOUD_PROJECT_KEY );
112+ projectId = System .getenv (GOOGLE_CLOUD_PROJECT_KEY );
113113 }
114114 if (Strings .isNullOrEmpty (projectId )) {
115115 printUsage (options );
@@ -121,22 +121,37 @@ public static void main(String... args) throws Exception {
121121 String payload = params .getOptionValue (PAYLOAD_OPTION .getOpt (), "default payload" );
122122
123123 // [START cloud_tasks_appengine_create_task]
124+ // Instantiates a client.
124125 try (CloudTasksClient client = CloudTasksClient .create ()) {
126+
127+ // Variables provided by the CLI.
128+ // projectId = "my-project-id";
129+ // queueName = "my-appengine-queue";
130+ // location = "us-central1";
131+ // payload = "hello";
132+
133+ // Construct the fully qualified queue name.
134+ String queuePath = QueueName .of (projectId , location , queueName ).toString ();
135+
136+ // Construct the task body.
125137 Task .Builder taskBuilder = Task
126138 .newBuilder ()
127139 .setAppEngineHttpRequest (AppEngineHttpRequest .newBuilder ()
128- .setPayload (ByteString .copyFrom (payload , Charset .defaultCharset ()))
129- .setRelativeUrl ("/tasks/create" )
140+ .setBody (ByteString .copyFrom (payload , Charset .defaultCharset ()))
141+ .setRelativeUri ("/tasks/create" )
130142 .setHttpMethod (HttpMethod .POST )
131143 .build ());
144+
132145 if (params .hasOption (IN_SECONDS_OPTION .getOpt ())) {
146+ // Add the scheduled time to the request.
133147 int seconds = Integer .parseInt (params .getOptionValue (IN_SECONDS_OPTION .getOpt ()));
134148 taskBuilder .setScheduleTime (Timestamp
135149 .newBuilder ()
136150 .setSeconds (Instant .now (Clock .systemUTC ()).plusSeconds (seconds ).getEpochSecond ()));
137151 }
138- Task task = client .createTask (
139- QueueName .of (projectId , location , queueName ).toString (), taskBuilder .build ());
152+
153+ // Send create task request.
154+ Task task = client .createTask (queuePath , taskBuilder .build ());
140155 System .out .println ("Task created: " + task .getName ());
141156 }
142157 // [END cloud_tasks_appengine_create_task]
0 commit comments