-
Notifications
You must be signed in to change notification settings - Fork 2.9k
[Cloud Tasks] Add HTTP push queue sample #1355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9ce1c96
Add HTTP sample
averikitsch 6fcb414
Merge branch 'master' into tasks
averikitsch 8100299
Fix linting
averikitsch 2c674bb
Merge branch 'tasks' of https://github.com/GoogleCloudPlatform/java-d…
averikitsch 0384516
Merge branch 'master' into tasks
averikitsch 4960ec4
Merge branch 'master' into tasks
averikitsch cf91602
remove CLI
averikitsch 70b0fe6
Move http sample
averikitsch 8e8e534
Fix pom.xml
averikitsch 001a0ae
Merge branch 'master' into tasks
averikitsch 464f710
Update pom and linting
averikitsch d03f198
Merge branch 'tasks' of https://github.com/GoogleCloudPlatform/java-d…
averikitsch c88de0b
Update tasks version
averikitsch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| # Google Cloud Tasks App Engine Queue Samples | ||
|
|
||
| Sample command-line program for interacting with the Cloud Tasks API | ||
| using App Engine queues. | ||
|
|
||
| App Engine queues push tasks to an App Engine HTTP target. This directory | ||
| contains both the App Engine app to deploy, as well as the snippets to run | ||
| locally to push tasks to it, which could also be called on App Engine. | ||
|
|
||
| `CreateTask.java` is a simple command-line program to create | ||
| tasks to be pushed to the App Engine app. | ||
|
|
||
| `TaskServlet.java` is the main App Engine app. This app serves as an endpoint to receive | ||
| App Engine task attempts. | ||
|
|
||
|
|
||
| ## Initial Setup | ||
|
|
||
| * Set up a Google Cloud Project and enable billing. | ||
| * Enable the | ||
| [Cloud Tasks API](https://console.cloud.google.com/launcher/details/google/cloudtasks.googleapis.com). | ||
| * Download and install the [Cloud SDK](https://cloud.google.com/sdk). | ||
| * Download and install [Maven](http://maven.apache.org/install.html). | ||
| * Set up [Google Application Credentials](https://cloud.google.com/docs/authentication/getting-started). | ||
|
|
||
| ## Creating a queue | ||
|
|
||
| To create a queue using the Cloud SDK, use the following gcloud command: | ||
|
|
||
| ``` | ||
| gcloud beta tasks queues create-app-engine-queue my-appengine-queue | ||
| ``` | ||
|
|
||
| Note: A newly created queue will route to the default App Engine service and | ||
| version unless configured to do otherwise. | ||
|
|
||
| ## Deploying the App Engine app | ||
| [Using Maven and the App Engine Plugin](https://cloud.google.com/appengine/docs/flexible/java/using-maven) | ||
| & [Maven Plugin Goals and Parameters](https://cloud.google.com/appengine/docs/flexible/java/maven-reference) | ||
|
|
||
| ``` | ||
| mvn appengine:deploy | ||
| ``` | ||
|
|
||
| ## Run the Sample Using the Command Line | ||
|
|
||
| Set environment variables: | ||
|
|
||
| First, your project ID: | ||
|
|
||
| ``` | ||
| export GOOGLE_CLOUD_PROJECT=<YOUR_GOOGLE_CLOUD_PROJECT> | ||
| ``` | ||
|
|
||
| Then the queue ID, as specified at queue creation time. Queue IDs already | ||
| created can be listed with `gcloud beta tasks queues list`. | ||
|
|
||
| ``` | ||
| export QUEUE_ID=my-appengine-queue | ||
| ``` | ||
|
|
||
| And finally the location ID, which can be discovered with | ||
| `gcloud beta tasks queues describe $QUEUE_ID`, with the location embedded in | ||
| the "name" value (for instance, if the name is | ||
| "projects/my-project/locations/us-central1/queues/my-appengine-queue", then the | ||
| location is "us-central1"). | ||
|
|
||
| ``` | ||
| export LOCATION_ID=<YOUR_ZONE> | ||
| ``` | ||
|
|
||
| ### Using HTTP Push Queues | ||
|
|
||
| Set an environment variable for the endpoint to your task handler. This is an | ||
| example url to send requests to the App Engine task handler: | ||
| ``` | ||
| export URL=https://${PROJECT_ID}.appspot.com/tasks/create | ||
| ``` | ||
|
|
||
| Running the sample will create a task and send the task to the specific URL | ||
| endpoint, with a payload specified: | ||
|
|
||
| ``` | ||
| mvn exec:java -Dexec.mainClass="com.example.task.CreateHttpTask" | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!-- | ||
| Copyright 2018 Google LLC | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| --> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <packaging>war</packaging> | ||
| <version>1.0-SNAPSHOT</version> | ||
| <groupId>com.example.task</groupId> | ||
| <artifactId>tasks-samples</artifactId> | ||
|
|
||
| <!-- | ||
| The parent pom defines common style checks and testing strategies for our samples. | ||
| Removing or replacing it should not affect the execution of the samples in anyway. | ||
| --> | ||
| <parent> | ||
| <groupId>com.google.cloud.samples</groupId> | ||
| <artifactId>shared-configuration</artifactId> | ||
| <version>1.0.11</version> | ||
| <relativePath></relativePath> | ||
| </parent> | ||
|
|
||
| <properties> | ||
| <maven.compiler.target>1.8</maven.compiler.target> | ||
| <maven.compiler.source>1.8</maven.compiler.source> | ||
| <failOnMissingWebXml>false</failOnMissingWebXml> | ||
| </properties> | ||
|
|
||
| <dependencies> | ||
| <!-- Compile/runtime dependencies --> | ||
| <dependency> | ||
| <groupId>com.google.cloud</groupId> | ||
| <artifactId>google-cloud-tasks</artifactId> | ||
| <version>0.88.0-beta</version> | ||
| </dependency> | ||
| </dependencies> | ||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.codehaus.mojo</groupId> | ||
| <artifactId>exec-maven-plugin</artifactId> | ||
| <version>1.4.0</version> | ||
| <configuration> | ||
| <mainClass>com.example.task.CreateHttpTask</mainClass> | ||
| <cleanupDaemonThreads>false</cleanupDaemonThreads> | ||
| </configuration> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| </project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /* | ||
| * Copyright 2018 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.example.task; | ||
|
|
||
| // [START cloud_tasks_create_http_task] | ||
| import com.google.cloud.tasks.v2beta3.CloudTasksClient; | ||
| import com.google.cloud.tasks.v2beta3.HttpMethod; | ||
| import com.google.cloud.tasks.v2beta3.HttpRequest; | ||
| import com.google.cloud.tasks.v2beta3.QueueName; | ||
| import com.google.cloud.tasks.v2beta3.Task; | ||
| import com.google.protobuf.ByteString; | ||
| import java.nio.charset.Charset; | ||
|
|
||
| public class CreateHttpTask { | ||
|
|
||
| public static void main(String[] args) throws Exception { | ||
| String projectId = System.getenv("PROJECT_ID"); | ||
| String queueName = System.getenv("QUEUE_ID"); | ||
| String location = System.getenv("LOCATION_ID"); | ||
| String url = System.getenv("URL"); | ||
|
|
||
| // Instantiates a client. | ||
| try (CloudTasksClient client = CloudTasksClient.create()) { | ||
| // Variables provided by the system variables. | ||
| // projectId = "my-project-id"; | ||
| // queueName = "my-appengine-queue"; | ||
| // location = "us-central1"; | ||
| // url = "https://<project-id>.appspot.com/tasks/create"; | ||
| String payload = "hello"; | ||
|
|
||
| // Construct the fully qualified queue name. | ||
| String queuePath = QueueName.of(projectId, location, queueName).toString(); | ||
|
|
||
| // Construct the task body. | ||
| Task.Builder taskBuilder = | ||
| Task.newBuilder() | ||
| .setHttpRequest( | ||
| HttpRequest.newBuilder() | ||
| .setBody(ByteString.copyFrom(payload, Charset.defaultCharset())) | ||
| .setUrl(url) | ||
| .setHttpMethod(HttpMethod.POST) | ||
| .build()); | ||
|
|
||
| // Send create task request. | ||
| Task task = client.createTask(queuePath, taskBuilder.build()); | ||
| System.out.println("Task created: " + task.getName()); | ||
| } | ||
| } | ||
| } | ||
| // [END cloud_tasks_create_http_task] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these instructions still correct? It looks like you are using environment variables now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is correct. This readme is associated with the App Engine sample.