8000 Add Cloud Tasks Readmes (#1156) · tuliocota/java-docs-samples@cd9fe5b · GitHub
[go: up one dir, main page]

Skip to content

Commit cd9fe5b

Browse files
averikitschdzlier-gcp
authored andcommitted
Add Cloud Tasks Readmes (GoogleCloudPlatform#1156)
* Add Cloud Tasks Readme * update endpoint and env var * remove app viewing command
1 parent b5cbe59 commit cd9fe5b

File tree

2 files changed

+167
-0
lines changed

2 files changed

+167
-0
lines changed

appengine-java8/tasks/README.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Google Cloud Tasks App Engine Queue Samples
2+
3+
Sample command-line program for interacting with the Cloud Tasks API
4+
using App Engine queues.
5+
6+
App Engine queues push tasks to an App Engine HTTP target. This directory
7+
contains both the App Engine app to deploy, as well as the snippets to run
8+
locally to push tasks to it, which could also be called on App Engine.
9+
10+
`CreateTask.java` is a simple command-line program to create
11+
tasks to be pushed to the App Engine app.
12+
13+
`TaskServlet.java` is the main App Engine app. This app serves as an endpoint to receive
14+
App Engine task attempts.
15+
16+
17+
## Initial Setup
18+
19+
* Set up a Google Cloud Project and enable billing.
20+
* Enable the
21+
[Cloud Tasks API](https://console.cloud.google.com/launcher/details/google/cloudtasks.googleapis.com).
22+
* Download and install the [Cloud SDK](https://cloud.google.com/sdk).
23+
* Download and install [Maven](http://maven.apache.org/install.html).
24+
25+
## Creating a queue
26+
27+
To create a queue using the Cloud SDK, use the following gcloud command:
28+
29+
```
30+
gcloud beta tasks queues create-app-engine-queue my-appengine-queue
31+
```
32+
33+
Note: A newly created queue will route to the default App Engine service and
34+
version unless configured to do otherwise.
35+
36+
## Deploying the App Engine app
37+
[Using Maven and the App Engine Plugin](https://cloud.google.com/appengine/docs/flexible/java/using-maven)
38+
& [Maven Plugin Goals and Parameters](https://cloud.google.com/appengine/docs/flexible/java/maven-reference)
39+
40+
### Running locally
41+
42+
```
43+
mvn appengine:run
44+
```
45+
### Deploying
46+
47+
```
48+
mvn appengine:deploy
49+
```
50+
51+
## Running the Sample
52+
53+
Set environment variables:
54+
55+
First, your project ID:
56+
57+
```
58+
export GOOGLE_CLOUD_PROJECT=<YOUR_PROJECT_ID>
59+
```
60+
61+
Then the queue ID, as specified at queue creation time. Queue IDs already
62+
created can be listed with `gcloud beta tasks queues list`.
63+
64+
```
65+
export QUEUE_ID=my-appengine-queue
66+
```
67+
68+
And finally the location ID, which can be discovered with
69+
`gcloud beta tasks queues describe $QUEUE_ID`, with the location embedded in
70+
the "name" value (for instance, if the name is
71+
"projects/my-project/locations/us-central1/queues/my-appengine-queue", then the
72+
location is "us-central1").
73+
74+
```
75+
export LOCATION_ID=<YOUR_ZONE>
76+
```
77+
78+
Create a task, targeted at the `/tasks/create` endpoint, with a payload specified:
79+
80+
```
81+
mvn exec:java -Dexec.mainClass="com.example.task.CreateTask" \
82+
-Dexec.args="--project-id $GOOGLE_CLOUD_PROJECT \
83+
--queue $QUEUE_ID --location $LOCATION_ID --payload hello"
84+
```
85+
86+
The App Engine app serves as a target for the push requests. It has an
87+
endpoint `/tasks/create` that reads the payload (i.e., the request body) of the
88+
HTTP POST request and logs it. The log output can be viewed with:
89+
90+
```
91+
gcloud app logs read
92+
```
93+
94+
Create a task that will be scheduled for a time in the future using the
95+
`--in-seconds` flag:
96+
97+
```
98+
mvn exec:java -Dexec.mainClass="com.example.task.CreateTask" \
99+
-Dexec.args="--project-id $GOOGLE_CLOUD_PROJECT \
100+
--queue $QUEUE_ID --location $LOCATION_ID --payload hello --in-seconds 30"
101+
```

tasks/README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Google Cloud Tasks Pull Queue Samples
2+
3+
<a href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&page=editor&open_in_editor=tasks/README.md">
4+
<img alt="Open in Cloud Shell" src ="http://gstatic.com/cloudssh/images/open-btn.png"></a>
5+
6+
Sample command-line program for interacting with the Google Cloud Tasks
7+
API using pull queues.
8+
9+
Pull queues let you add tasks to a queue, then programatically remove
10+
and interact with them. Tasks can be added or processed in any
11+
environment, such as on Google App Engine or Google Compute Engine.
12+
13+
`Quickstart.java` is a simple command-line program to demonstrate listing
14+
queues, creating tasks, and pulling and acknowledging tasks.
15+
16+
## Initial Setup
17+
18+
* Set up a Google Cloud Project and enable billing.
19+
* Enable the
20+
[Cloud Tasks API](https://console.cloud.google.com/launcher/details/google/cloudtasks.googleapis.com).
21+
* Download and install the [Cloud SDK](https://cloud.google.com/sdk).
22+
* Download and install [Maven](http://maven.apache.org/install.html).
23+
24+
25+
## Creating a queue
26+
27+
To create a queue using the Cloud SDK, use the following gcloud command:
28+
29+
```
30+
gcloud beta tasks queues create-pull-queue my-pull-queue
31+
```
32+
33+
In this example, the queue will be named `my-pull-queue`.
34+
35+
## Running the Samples
36+
37+
From the project folder, build your project with:
38+
39+
```
40+
mvn clean install
41+
```
42+
43+
Optionally, you can set up your settings as environment variables:
44+
45+
```
46+
export GOOGLE_CLOUD_PROJECT=<YOUR_PROJECT_ID>
47+
export LOCATION_ID=<YOUR_ZONE>
48+
export QUEUE_ID=my-pull-queue
49+
```
50+
51+
Next, create a task for a queue:
52+
53+
```
54+
mvn exec:java -Dexec.mainClass="com.example.Quickstart" \
55+
-Dexec.args="create-task \
56+
--project $GOOGLE_CLOUD_PROJECT --queue $QUEUE_ID --location $LOCATION_ID"
57+
```
58+
59+
Finally, pull and acknowledge a task:
60+
61+
```
62+
mvn exec:java -Dexec.mainClass="com.example.task.Quickstart" \
63+
-Dexec.args="lease-and-ack-task \
64+
--project $GOOGLE_CLOUD_PROJECT --queue $QUEUE_ID --location $LOCATION_ID"
65+
```
66+
Note that usually, there would be a processing step in between pulling a task and acknowledging it.

0 commit comments

Comments
 (0)
0