This sample command line application demonstrates how to access App Engine pull task queues using the Task Queue REST API and the Google Java API Client Library.
Important Note: This sample requires an existing App Engine Pull Task Queue. Deploy this sample App Engine pull task queue app to create one first and then come back and run this sample.
-
Create or select a project in the Google Cloud Console:
-
Visit the Cloud Console
-
If this is your first time then click "Create Project," otherwise you can reuse an existing project by clicking on it. 1. Note: You will need to enable billing for the project to use Compute Engine.
-
Authentication instructions to run the sample (on your local machine or on a Compute Engine VM):
- Running the sample locally on your development machine:
- Install Google Cloud SDK
- Run the following command to authorize the Cloud SDK and configure your project:
gcloud init
- Add your authenticated email account as '<user-email>' and '<writer-email>' elements to the queue.xml file of the App Engine app that created the pull queue task you're trying to access. For more details, please see Defining Pull Queues.
- Running the sample on a Google Compute Engine VM using Default Application Credentials:
-
Create a service account and add it to queue.xml 1. In the API Manager > Credentials section click "Create credentials" and choose "Service account key". 1. On the "Create service account key" page, select "Compute Engine default service account" from the "Service Account" drop-down menu. Leave the Key type set to JSON and click the "Create" button. 1. Once the service account is created, click the "Manage service accounts" link and copy the "Service account ID" of the "Compute Engine default service account". 1. Add the "Service account ID" as '<user-email>' and '<writer-email>' elements to the queue.xml file of the App Engine app that created the pull queue task you're trying to access. For more details, please see Defining Pull Queues.
-
Create a Compute Engine VM Instance. 1. In the Cloud Console go to the Compute > Compute Engine section. 1. Click the "Create instance" button. 1. For the 'Boot Disk' select a Linux machine image like Debian or Ubuntu. 1. In the "Indentity API and Access" section, select "Set access for each API" under the "Access Scopes" subsection and then select the "Task queue" drop-down menu to set its scope.
- Set the "Task queue" access scope to be "Enabled". 1. Click the "Create" button. 1. Once the VM is created click the VM instance's "SSH" button to ssh in to the newly created VM instance.
-
- Code checkout instructions:
- Prerequisites: install Java 7 or Java 8 JDK, Git, and Maven.
You may need to set your
JAVA_HOME
environment variable as well. * To install these prerequisites on a Linux (Debian or Ubuntu) based Compute Engine VM instance, run these commands:sudo apt-get update sudo apt-get install git maven openjdk-7-jdk -y
- Download the sample code by running the following commands:
mkdir some_directory cd some_directory git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git cd java-docs-samples/taskqueue/cmdline
In a text editor open the TaskQueueSample.java
file. For example, to edit the file with nano:
nano src/main/java/TaskQueueSample.java
- Specify an 'Application Name' for your app by updating the following line of code:
private static final String APPLICATION_NAME = "";
-
Save the changes to the file and exit the text editor.
-
Compile and run the sample:
-
Compile the sample code using Maven by running the following command:
mvn compile
- Execute the sample code using Maven by running the following command, entering your specific values for the placeholder arguments:
mvn -q exec:java -Dexec.args="<ProjectId> <TaskQueueName> <LeaseSeconds> <NumberOfTasksToLease>"
-
Running the sample will first list the pull task queue details and then it will lease, process and delete the number of tasks you specified.
You can verify the details of the pull task queue and the leased tasks by visiting the App Engine > Task queues section of the Developers Console.
-
Importing the code into Eclipse and running it from there:
-
Prerequisites: install Eclipse and the Maven plugin for Eclipse.
-
Download code as specified above.
-
File -> Import -> Maven -> Existing Maven Projects -> Next.
-
Select your project directory as your "Root Directory," and click "Finish."
-
Right-click on project task-queue-rest-sample.
-
Run As > Java Application.
-
If asked, type or select "TaskQueueSample" and click OK.
-
Application output will display in the Eclipse Console.