1+ /**
2+ * Copyright 2017 Google Inc.
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 .cloudtasks ;
18+
19+ import com .google .api .services .cloudtasks .v2beta2 .CloudTasks ;
20+ import com .google .api .client .googleapis .auth .oauth2 .GoogleCredential ;
21+ import com .google .api .client .http .HttpTransport ;
22+ import com .google .api .client .http .javanet .NetHttpTransport ;
23+ import com .google .api .client .json .JsonFactory ;
24+ import com .google .api .client .json .jackson2 .JacksonFactory ;
25+ import com .google .api .services .cloudtasks .v2beta2 .CloudTasksScopes ;
26+ import com .google .api .services .cloudtasks .v2beta2 .model .AcknowledgeTaskRequest ;
27+ import com .google .api .services .cloudtasks .v2beta2 .model .CreateTaskRequest ;
28+ import com .google .api .services .cloudtasks .v2beta2 .model .PullTaskTarget ;
29+ import com .google .api .services .cloudtasks .v2beta2 .model .PullTasksRequest ;
30+ import com .google .api .services .cloudtasks .v2beta2 .model .PullTasksResponse ;
31+ import com .google .api .services .cloudtasks .v2beta2 .model .Task ;
32+ import com .google .common .io .BaseEncoding ;
33+ import java .io .IOException ;
34+ import net .sourceforge .argparse4j .ArgumentParsers ;
35+ import net .sourceforge .argparse4j .inf .ArgumentParser ;
36+ import net .sourceforge .argparse4j .inf .Namespace ;
37+ import net .sourceforge .argparse4j .inf .Subparsers ;
38+
39+
40+ public class PullQueue {
41+
42+ /**
43+ * Creates an authorized CloudTasks client service using Application Default Credentials.
44+ *
45+ * @return an authorized CloudTasks client
46+ * @throws IOException if there's an error getting the default credentials.
47+ */
48+ private static CloudTasks createAuthorizedClient () throws IOException {
49+ // Create the credential
50+ HttpTransport transport = new NetHttpTransport ();
51+ JsonFactory jsonFactory = new JacksonFactory ();
52+ // Authorize the client using Application Default Credentials
53+ // @see https://g.co/dv/identity/protocols/application-default-credentials
54+ GoogleCredential credential = GoogleCredential .getApplicationDefault (transport , jsonFactory );
55+
56+ // Depending on the environment that provides the default credentials (e.g. Compute Engine, App
57+ // Engine), the credentials may require us to specify the scopes we need explicitly.
58+ // Check for this case, and inject the scope if required.
59+ if (credential .createScopedRequired ()) {
60+ credential = credential .createScoped (CloudTasksScopes .all ());
61+ }
62+
63+ return new CloudTasks .Builder (transport , jsonFactory , credential )
64+ .setApplicationName ("Cloud Tasks Snippets" )
65+ .build ();
66+ }
67+
68+ /**
69+ * Create a task for a given queue with a given payload.
70+ */
71+ private static Task createTask (
72+ String project , String location , String queue ) throws IOException {
73+ // The name of the queue to use
74+ String queueName = String .format (
75+ "projects/%s/locations/%s/queues/%s" , project , location , queue );
76+
77+ // Create the Cloud Tasks Client
78+ CloudTasks client = createAuthorizedClient ();
79+
80+ // Create the Task to put in the Queue
81+ String message = "a message for the recipient" ;
82+ String payload = BaseEncoding .base64 ().encode (message .getBytes ());
83+ Task task = new Task ().setPullTaskTarget (new PullTaskTarget ().setPayload (payload ));
84+
85+ // Create the CreateTaskRequest
86+ CreateTaskRequest request = new CreateTaskRequest ().setTask (task );
87+
88+ //Execute the request and return the created Task
89+ Task result = client
90+ .projects ()
91+ .locations ()
92+ .queues ()
93+ .tasks ()
94+ .create (queueName , request )
95+ .execute ();
96+ System .out .println (String .format ("Created task %s" ,task .getName ()));
97+ return result ;
98+ }
99+
100+ /**
101+ * Pull a single task from a given queue and lease it for 10 minutes.
102+ */
103+ private static Task pullTask (
104+ String project , String location , String queue ) throws IOException {
105+ // The name of the queue to use
106+ String queueName = String .format (
107+ "projects/%s/locations/%s/queues/%s" , project , location , queue );
108+
109+ // Create the Cloud Tasks Client
110+ CloudTasks client = createAuthorizedClient ();
111+
112+ // Create the PullTasksRequest
113+ PullTasksRequest request = new PullTasksRequest ().setMaxTasks (1 ).setLeaseDuration ("600s" );
114+
115+ //Execute the request and return the pulled task
116+ PullTasksResponse response = client
117+ .projects ()
118+ .locations ()
119+ .queues ()
120+ .tasks ()
121+ .pull (queueName , request )
122+ .execute ();
123+ return response .getTasks ().get (0 );
124+ }
125+
126+ /**
127+ * Acknowledge a given task, which removes it from the queue.
128+ */
129+ private static void acknowledgeTask (Task task ) throws IOException {
130+ // Create the Cloud Tasks Client
131+ CloudTasks client = createAuthorizedClient ();
132+
133+ // Create the AcknowledgeTaskRequest
134+ AcknowledgeTaskRequest request = new AcknowledgeTaskRequest ()
135+ .setScheduleTime (task .getScheduleTime ());
136+
137+ //Execute the request
138+ client
139+ .projects ()
140+ .locations ()
141+ .queues ()
142+ .tasks ()
143+ .acknowledge (task .getName (), request )
144+ .execute ();
145+ System .out .println (String .format ("Acknowledged task %s" , task .getName ()));
146+ }
147+
148+ public static void main (String [] args ) throws Exception {
149+ ArgumentParser parser = ArgumentParsers .newFor ("PullQueue" ).build ()
150+ .defaultHelp (true )
151+ .description ("Sample command-line program for interacting with the Cloud Tasks API.\n \n "
152+ + "See README.md for instructions on setting up your development environment "
153+ + "and running the scripts." );
154+
155+ Subparsers subparsers = parser .addSubparsers ().dest ("command" );
156+
157+ // Create the parser for the command 'create-task'
158+ ArgumentParser createTaskParser = subparsers
159+ .addParser ("create-task" )
160+ .help ("Acknowledge a given task, which removes it from the queue." );
161+ createTaskParser
162+ .addArgument ("--project" )
163+ .help ("Project of the queue to add the task to." )
164+ .required (true );
165+ createTaskParser
166+ .addArgument ("--location" )
167+ .help ("Location of the queue to add the task to." )
168+ .required (true );
169+ createTaskParser
170+ .addArgument ("--queue" )
171+ .help ("ID (short name) of the queue to add the task to." )
172+ .required (true );
173+
174+ // Create the parser for the command 'pull-and-ack-task'
175+ ArgumentParser pullAndAckParser = subparsers
176+ .addParser ("pull-and-ack-task" )
177+ .help ("Create a task for a given queue with an arbitrary payload." );
178+ pullAndAckParser
179+ .addArgument ("--project" )
180+ .help ("Project of the queue to add the task to." )
181+ .required (true );
182+ pullAndAckParser
183+ .addArgument ("--location" )
184+ .help ("Location of the queue to add the task to." )
185+ .required (true );
186+ pullAndAckParser
187+ .addArgument ("--queue" )
188+ .help ("ID (short name) of the queue to add the task to." )
189+ .required (true );
190+
191+ // Parse commands
192+ Namespace cmd = parser .parseArgs (args );
193+
194+ String command = cmd .get ("command" );
195+ String project = cmd .get ("project" );
196+ String queue = cmd .get ("queue" );
197+ String location = cmd .get ("location" );
198+
199+ // Execute commands
200+ if (command .equals ("create-task" )){
201+ createTask (project , location , queue );
202+ }
203+ if (command .equals ("pull-and-ask-task" )){
204+ Task task = pullTask (project , location , queue );
205+ acknowledgeTask (task );
206+ }
207+ }
208+
209+ }
0 commit comments