16
16
17
17
package com .example .task ;
18
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 ;
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 ;
24
24
import com .google .common .base .Strings ;
25
25
import com .google .protobuf .ByteString ;
26
26
import com .google .protobuf .Timestamp ;
38
38
import org .apache .commons .cli .ParseException ;
39
39
40
40
public class CreateTask {
41
- private static String GGOGLE_CLOUD_PROJECT_KEY = "GOOGLE_CLOUD_PROJECT" ;
41
+ private static String GOOGLE_CLOUD_PROJECT_KEY = "GOOGLE_CLOUD_PROJECT" ;
42
42
43
43
private static Option PROJECT_ID_OPTION = Option .builder ("pid" )
44
44
.longOpt ("project-id" )
@@ -109,7 +109,7 @@ public static void main(String... args) throws Exception {
109
109
if (params .hasOption ("project-id" )) {
110
110
projectId = params .getOptionValue ("project-id" );
111
111
} else {
112
- projectId = System .getenv (GGOGLE_CLOUD_PROJECT_KEY );
112
+ projectId = System .getenv (GOOGLE_CLOUD_PROJECT_KEY );
113
113
}
114
114
if (Strings .isNullOrEmpty (projectId )) {
115
115
printUsage (options );
@@ -121,22 +121,37 @@ public static void main(String... args) throws Exception {
121
121
String payload = params .getOptionValue (PAYLOAD_OPTION .getOpt (), "default payload" );
122
122
123
123
// [START cloud_tasks_appengine_create_task]
124
+ // Instantiates a client.
124
125
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.
125
137
Task .Builder taskBuilder = Task
126
138
.newBuilder ()
127
139
.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" )
130
142
.setHttpMethod (HttpMethod .POST )
131
143
.build ());
144
+
132
145
if (params .hasOption (IN_SECONDS_OPTION .getOpt ())) {
146
+ // Add the scheduled time to the request.
133
147
int seconds = Integer .parseInt (params .getOptionValue (IN_SECONDS_OPTION .getOpt ()));
134
148
taskBuilder .setScheduleTime (Timestamp
135
149
.newBuilder ()
136
150
.setSeconds (Instant .now (Clock .systemUTC ()).plusSeconds (seconds ).getEpochSecond ()));
137
151
}
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 ());
140
155
System .out .println ("Task created: " + task .getName ());
141
156
}
142
157
// [END cloud_tasks_appengine_create_task]
0 commit comments