This repository was archived by the owner on Sep 16, 2023. It is now read-only.
Repository navigation
docs(sample): add native-image sample #621
Merged
gcf-merge-on-green
merged 7 commits into
googleapis:main
from
suztomo:native-image-sample
Mar 1, 2022
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f1ed2e9
docs(sample): add native-image sample
suztomo 5d9a432
rafactor: fixed license header
suztomo 3c482b8
test: adding ITNativeImageTasksSample
suztomo a37b8a2
test: enriched readme in native-image-sample
suztomo 0497a17
test: using environment variable for location
suztomo aa186e4
test: adding comment on Java version for native-image-sample
suztomo 77eb07c
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| # Cloud Tasks Sample Application with Native Image | ||
|
|
||
| The Cloud Tasks sample application demonstrates some common operations with | ||
| [Google Cloud Tasks](https://cloud.google.com/tasks) and is compatible with | ||
| Native Image compilation. | ||
|
|
||
| This application will create a new queue called `graal-test-queue` if it does | ||
| not already exist. | ||
| It will then submit a new task to this queue. | ||
|
|
||
| ## Setup Instructions | ||
|
|
||
| 1. Follow the [GCP Project and Native Image Setup Instructions](../../README.md). | ||
|
|
||
| 2. [Enable the Cloud Tasks APIs](https://console.cloud.google.com/apis/api/cloudtasks.googleapis.com). | ||
|
|
||
| ### Run with Native Image Compilation | ||
|
|
||
| Navigate to this directory in a new terminal. | ||
|
|
||
| 1. Compile the application using the Native Image Compiler. This step may take a few minutes. | ||
|
|
||
| ``` | ||
| $ mvn package -P native | ||
| ``` | ||
|
|
||
| The project uses an environment variable `LOCATION_ID` to run the test. | ||
|
|
||
| 2. Run the application: | ||
|
|
||
| ``` | ||
| $ LOCATION_ID=us-east1 ./target/tasks-sample | ||
| ``` | ||
|
|
||
| The sample application uses an environment variable `LOCATION_ID`. | ||
|
|
||
| 3. The application runs through some basic Cloud Tasks operations (create queue, create task) and then prints some results of the operations. | ||
|
|
||
| ``` | ||
| Test queue ready: name: "projects/xxxxxxxxxx/locations/us-central1/queues/graal-test-queue-4009" | ||
| rate_limits { | ||
| max_dispatches_per_second: 500.0 | ||
| max_burst_size: 100 | ||
| max_concurrent_dispatches: 1 | ||
| } | ||
| retry_config { | ||
| max_attempts: 100 | ||
| min_backoff { | ||
| nanos: 100000000 | ||
| } | ||
| max_backoff { | ||
| seconds: 3600 | ||
| } | ||
| max_doublings: 16 | ||
| } | ||
| state: RUNNING | ||
|
|
||
| Created task: name: "projects/xxxxxxxxxx/locations/us-central1/queues/graal-test-queue-4009/tasks/5886258204485021611" | ||
| http_request { | ||
| url: "https://google.com/" | ||
| http_method: GET | ||
| headers { | ||
| key: "User-Agent" | ||
| value: "Google-Cloud-Tasks" | ||
| } | ||
| } | ||
| schedule_time { | ||
| seconds: 1613189391 | ||
| nanos: 486293000 | ||
| } | ||
| create_time { | ||
| seconds: 1613189391 | ||
| } | ||
| dispatch_deadline { | ||
| seconds: 600 | ||
| } | ||
| view: BASIC | ||
|
|
||
| Queue purged | ||
| Queue deleted | ||
| ``` | ||
|
|
||
| 4. Run the test in the project in the native-image mode | ||
|
|
||
| ``` | ||
| $ LOCATION_ID=us-east1 mvn test -P native | ||
| ... | ||
| [INFO] ------------------------------------------------------- | ||
| [INFO] T E S T S | ||
| [INFO] ------------------------------------------------------- | ||
| [INFO] Running com.example.tasks.ITNativeImageTasksSample | ||
| ... | ||
| [INFO] --- native-maven-plugin:0.9.9:test (test-native) @ native-image-sample --- | ||
| [INFO] ==================== | ||
| [INFO] Initializing project: native-image-sample | ||
| ... | ||
| com.example.tasks.ITNativeImageTasksSample > testRunSampleApplication SUCCESSFUL | ||
|
|
||
|
|
||
| Test run finished after 1025 ms | ||
| [ 3 containers found ] | ||
| [ 0 containers skipped ] | ||
| [ 3 containers started ] | ||
| [ 0 containers aborted ] | ||
| [ 3 containers successful ] | ||
| [ 0 containers failed ] | ||
| [ 1 tests found ] | ||
| [ 0 tests skipped ] | ||
| [ 1 tests started ] | ||
| [ 0 tests aborted ] | ||
| [ 1 tests successful ] | ||
| [ 0 tests failed ] | ||
|
|
||
| [INFO] ------------------------------------------------------------------------ | ||
| [INFO] BUILD SUCCESS | ||
| [INFO] ------------------------------------------------------------------------ | ||
| [INFO] Total time: 02:27 min | ||
| ... | ||
| ``` |
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,150 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <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> | ||
| <groupId>com.example.tasks</groupId> | ||
| <artifactId>native-image-sample</artifactId> | ||
| <name>Native Image Sample</name> | ||
|
|
||
| <!-- | ||
| 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.2.0</version> | ||
| </parent> | ||
|
|
||
| <properties> | ||
| <!-- Java 8 because the Kokoro Sample test uses that Java version --> | ||
| <maven.compiler.target>1.8</maven.compiler.target> | ||
| <maven.compiler.source>1.8</maven.compiler.source> | ||
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| </properties> | ||
|
|
||
| <dependencyManagement> | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>com.google.cloud</groupId> | ||
| <artifactId>libraries-bom</artifactId> | ||
| <version>24.2.0</version> | ||
| <type>pom</type> | ||
| <scope>import</scope> | ||
| </dependency> | ||
| </dependencies> | ||
| </dependencyManagement> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>com.google.cloud</groupId> | ||
| <artifactId>google-cloud-core</artifactId> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.google.cloud</groupId> | ||
| <artifactId>google-cloud-tasks</artifactId> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>junit</groupId> | ||
| <artifactId>junit</artifactId> | ||
| <version>4.13.2</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.google.truth</groupId> | ||
| <artifactId>truth</artifactId> | ||
| <version>1.1.3</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <build> | ||
| <!-- These plugins enable building the application to a JAR *not* using Native Image --> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-jar-plugin</artifactId> | ||
| <configuration> | ||
| <archive> | ||
| <manifest> | ||
| <mainClass>com.example.TasksSampleApplication</mainClass> | ||
| </manifest> | ||
| </archive> | ||
| </configuration> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
|
|
||
| <profiles> | ||
| <profile> | ||
| <id>native</id> | ||
| <dependencies> | ||
| <dependency> | ||
| <!-- TODO: remove this when it's no longer needed --> | ||
| <groupId>com.google.cloud</groupId> | ||
| <artifactId>native-image-support</artifactId> | ||
| <version>0.10.0</version> | ||
| </dependency> | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No JUnit-related dependencies because this project doesn't use JUnit. |
||
| <dependency> | ||
| <groupId>org.junit.vintage</groupId> | ||
| <artifactId>junit-vintage-engine</artifactId> | ||
| <version>5.8.2</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.graalvm.buildtools</groupId> | ||
| <artifactId>junit-platform-native</artifactId> | ||
| <version>0.9.9</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-surefire-plugin</artifactId> | ||
| <!-- Must use older version of surefire plugin for native-image testing. --> | ||
| <version>2.22.2</version> | ||
| <configuration> | ||
| <includes> | ||
| <include>**/IT*</include> | ||
| </includes> | ||
| </configuration> | ||
| </plugin> | ||
| <plugin> | ||
| <groupId>org.graalvm.buildtools</groupId> | ||
| <artifactId>native-maven-plugin</artifactId> | ||
| <version>0.9.9</version> | ||
| <extensions>true</extensions> | ||
| <configuration> | ||
| <mainClass>com.example.tasks.TasksSampleApplication</mainClass> | ||
| <buildArgs> | ||
| <buildArg>--no-fallback</buildArg> | ||
| <buildArg>--no-server</buildArg> | ||
| <buildArg>--initialize-at-build-time</buildArg> | ||
| <buildArg>--features=com.google.cloud.nativeimage.features.ProtobufMessageFeature</buildArg> | ||
| </buildArgs> | ||
| </configuration> | ||
| <executions> | ||
| <execution> | ||
| <id>build-native</id> | ||
| <goals> | ||
| <goal>build</goal> | ||
| <goal>test</goal> | ||
| </goals> | ||
| <phase>package</phase> | ||
| </execution> | ||
| <execution> | ||
| <id>test-native</id> | ||
| <goals> | ||
| <goal>test</goal> | ||
| </goals> | ||
| <phase>test</phase> | ||
| </execution> | ||
| </executions> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| </profile> | ||
| </profiles> | ||
| </project> | ||
85 changes: 85 additions & 0 deletions
85
samples/native-image-sample/src/main/java/com/example/tasks/TasksSampleApplication.java
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 @@ | ||
| /* | ||
| * Copyright 2022 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.tasks; | ||
|
|
||
| import com.google.cloud.ServiceOptions; | ||
| import com.google.cloud.tasks.v2.CloudTasksClient; | ||
| import com.google.cloud.tasks.v2.CreateQueueRequest; | ||
| import com.google.cloud.tasks.v2.HttpMethod; | ||
| import com.google.cloud.tasks.v2.HttpRequest; | ||
| import com.google.cloud.tasks.v2.LocationName; | ||
| import com.google.cloud.tasks.v2.Queue; | ||
| import com.google.cloud.tasks.v2.QueueName; | ||
| import com.google.cloud.tasks.v2.RateLimits; | ||
| import com.google.cloud.tasks.v2.Task; | ||
| import java.io.IOException; | ||
| import java.util.UUID; | ||
|
|
||
| /** Sample application demonstrating Native Image compatibility with Google Cloud Tasks APIs. */ | ||
| public class TasksSampleApplication { | ||
| /** | ||
| * Queue name randomness added to avoid FAILED_PRECONDITION: The queue cannot be created because a | ||
| * queue with this name existed too recently. | ||
| */ | ||
| private static final String GRAALVM_TEST_QUEUE_NAME = "graal-test-queue-"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we replace "graalvm" with "native" or "native-image"? |
||
|
|
||
| private static final String LOCATION_ID = System.getenv("LOCATION_ID"); | ||
|
|
||
| /** Runs the Cloud Tasks sample application. */ | ||
| public static void main(String[] args) throws IOException { | ||
| String projectId = ServiceOptions.getDefaultProjectId(); | ||
| LocationName parent = LocationName.of(projectId, LOCATION_ID); | ||
| QueueName queueName = | ||
| QueueName.of( | ||
| parent.getProject(), | ||
| parent.getLocation(), | ||
| GRAALVM_TEST_QUEUE_NAME + UUID.randomUUID().toString()); | ||
|
|
||
| try (CloudTasksClient client = CloudTasksClient.create()) { | ||
| // Create queue | ||
| Queue queue = | ||
| Queue.newBuilder() | ||
| .setName(queueName.toString()) | ||
| .setRateLimits(RateLimits.newBuilder().setMaxConcurrentDispatches(1).build()) | ||
| .build(); | ||
|
|
||
| CreateQueueRequest createQueueRequest = | ||
| CreateQueueRequest.newBuilder().setParent(parent.toString()).setQueue(queue).build(); | ||
|
|
||
| Queue createdQueue = client.createQueue(createQueueRequest); | ||
| System.out.println("Test queue ready: " + createdQueue); | ||
|
|
||
| // Create task | ||
| HttpRequest taskTarget = | ||
| HttpRequest.newBuilder() | ||
| .setUrl("https://google.com") | ||
| .setHttpMethod(HttpMethod.GET) | ||
| .build(); | ||
|
|
||
| Task taskRequest = Task.newBuilder().setHttpRequest(taskTarget).build(); | ||
| Task task = client.createTask(queueName, taskRequest); | ||
| System.out.println("Created task: " + task); | ||
|
|
||
| // Cleanup | ||
| client.purgeQueue(queueName); | ||
| System.out.println("Queue purged"); | ||
|
|
||
| client.deleteQueue(queueName); | ||
| System.out.println("Queue deleted"); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
This is the version that works with native-image-support 0.10.0.