|
| 1 | +# Pull Task Queue REST API sample |
| 2 | +This sample command line application demonstrates how to access App Engine pull task queues using the Task |
| 3 | +Queue REST API and the Google Java API Client Library. |
| 4 | + |
| 5 | +<strong>Important Note:</strong> This sample requires an existing App Engine Pull Task Queue. |
| 6 | +Deploy this |
| 7 | +[sample App Engine pull task queue app](https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/appengine/taskqueue/pull) |
| 8 | +to create one first and then come back and run this sample. |
| 9 | + |
| 10 | +## Setup Instructions |
| 11 | +1. Create or select a project in the Google Cloud Console: |
| 12 | + 1. Visit the [Cloud Console][2] |
| 13 | + 1. If this is your first time then click "Create Project," otherwise you can |
| 14 | +reuse an existing project by clicking on it. |
| 15 | + 1. Note: You will need to enable billing for the project to use Compute |
| 16 | + Engine. |
| 17 | + 1. Click "Overview" in the left-side navigation menu and copy your Project ID |
| 18 | + for use in step 3.3 below. |
| 19 | + |
| 20 | +1. Authentication instructions to run the sample (on your local machine or on a Compute Engine VM): |
| 21 | + * Running the sample locally on your development machine: |
| 22 | + 1. Install [Google Cloud SDK](https://cloud.google.com/sdk/) |
| 23 | + 1. Run the following command to authorize the Cloud SDK and configure your project: |
| 24 | + <pre>gcloud init</pre> |
| 25 | + 1. 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 |
| 26 | + [Defining Pull Queues](https://cloud.google.com/appengine/docs/java/config/queue#Defining_Pull_Queues) |
| 27 | + on the Task Queue configuration page. |
| 28 | + * Running the sample on a Google Compute Engine VM using Default Application Credentials: |
| 29 | + 1. Create a service account and add it to queue.xml |
| 30 | + 1. In the API Manager > [Credentials](https://pantheon.corp.google.com/apis/credentials) |
| 31 | + section click "Create credentials" and choose "Service account key". |
| 32 | + 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. |
| 33 | + 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". |
| 34 | + 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 |
| 35 | + [Defining Pull Queues](https://cloud.google.com/appengine/docs/java/config/queue#Defining_Pull_Queues) |
| 36 | + on the Task Queue configuration page. |
| 37 | + |
| 38 | + 1. Create a Compute Engine VM Instance. |
| 39 | + 1. In the [Cloud Console](https://console.cloud.google.com/project) |
| 40 | + go to the Compute > Compute Engine section. |
| 41 | + 1. Click the "Create instance" button. |
| 42 | + 1. For the 'Boot Disk' select a Linux machine image like Debian or Ubuntu. |
| 43 | + 1. In the "Indentity API and Access" section, select "Set access for each API" under the "Access Scopes" subsection and then select the |
| 44 | + "Task queue" drop-down menu to set its scope. |
| 45 | + * Set the "Task queue" access scope to be "Enabled". |
| 46 | + 1. Click the "Create" button. |
| 47 | + 1. Once the VM is created click the VM instance's "SSH" button to ssh in to the newly created VM instance. |
| 48 | + |
| 49 | +1. Code checkout instructions: |
| 50 | + 1. Prerequisites: install [Java 7 or Java 8 JDK][1], [Git][3], and [Maven][4]. |
| 51 | +You may need to set your `JAVA_HOME` environment variable as well. |
| 52 | + * To install these prerequisites on a Linux (Debian or Ubuntu) based Compute Engine VM |
| 53 | + instance, run these commands: |
| 54 | + <pre> |
| 55 | + sudo apt-get update |
| 56 | + sudo apt-get install git maven openjdk-7-jdk -y |
| 57 | + </pre> |
| 58 | + 1. Download the sample code by running the following commands: |
| 59 | + <pre>mkdir some_directory |
| 60 | + cd some_directory |
| 61 | + git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git |
| 62 | + cd java-docs-samples/taskqueue/cmdline |
| 63 | + </pre> |
| 64 | + In a text editor open the `TaskQueueSample.java` file. For example, to edit the file with nano: |
| 65 | + <pre>nano src/main/java/TaskQueueSample.java</pre> |
| 66 | + |
| 67 | + 1. Specify an 'Application Name' for your app by updating the following line of code: |
| 68 | + <pre>private static final String APPLICATION_NAME = "";</pre> |
| 69 | + 1. Save the changes to the file and exit the text editor. |
| 70 | + |
| 71 | +1. Compile and run the sample: |
| 72 | + 1. Compile the sample code using Maven by running the following command: |
| 73 | + <pre>mvn compile</pre> |
| 74 | + 1. Execute the sample code using Maven by running the following command, |
| 75 | + entering your specific values for the placeholder arguments: |
| 76 | + <pre>mvn -q exec:java -Dexec.args="<ProjectId> <TaskQueueName> <LeaseSeconds> <NumberOfTasksToLease>"</pre> |
| 77 | + 1. 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. |
| 78 | + |
| 79 | + You can verify the details of the pull task queue and the leased tasks by visiting the [App Engine > Task queues](https://pantheon.corp.google.com/appengine/taskqueues) |
| 80 | + section of the Developers Console. |
| 81 | +1. Importing the code into Eclipse and running it from there: |
| 82 | + 1. Prerequisites: install [Eclipse][5] and the [Maven plugin for Eclipse][6]. |
| 83 | + 1. Download code as specified above. |
| 84 | + 1. File -> Import -> Maven -> Existing Maven Projects -> Next. |
| 85 | + 1. Select your project directory as your "Root Directory," and click "Finish." |
| 86 | + 1. Right-click on project task-queue-rest-sample. |
| 87 | + 1. Run As > Java Application. |
| 88 | + 1. If asked, type or select "TaskQueueSample" and click OK. |
| 89 | + 1. Application output will display in the Eclipse Console. |
| 90 | + |
| 91 | +[1]: http://java.com/en/download/faq/develop.xml |
| 92 | +[2]: https://console.cloud.google.com/project |
| 93 | +[3]: http://git-scm.com/downloads |
| 94 | +[4]: http://maven.apache.org/download.html |
| 95 | +[5]: http://www.eclipse.org/downloads/ |
| 96 | +[6]: http://download.eclipse.org/technology/m2e/releases/ |
| 97 | + |
0 commit comments