Skip to content
This repository was archived by the owner on Sep 16, 2023. It is now read-only.

Commit ec53481

Browse files
authored
docs(sample): add native-image sample (#621)
Relocating the sample from native-image-support to this repository `mvn clean verify -DskipTests -P lint` passes. Fixes #<issue_number_goes_here> ☕️ If you write sample code, please follow the [samples format]( https://github.com/GoogleCloudPlatform/java-docs-samples/blob/main/SAMPLE_FORMAT.md).
1 parent 6444ff5 commit ec53481

File tree

6 files changed

+399
-0
lines changed

6 files changed

+399
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-tasks/tree/ma
7676

7777
| Sample | Source Code | Try it |
7878
| --------------------------- | --------------------------------- | ------ |
79+
| Tasks Sample Application | [source code](https://github.com/googleapis/java-tasks/blob/main/samples/native-image-sample/src/main/java/com/example/tasks/TasksSampleApplication.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-tasks&page=editor&open_in_editor=samples/native-image-sample/src/main/java/com/example/tasks/TasksSampleApplication.java) |
7980
| Create Http Task | [source code](https://github.com/googleapis/java-tasks/blob/main/samples/snippets/src/main/java/com/example/task/CreateHttpTask.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-tasks&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/task/CreateHttpTask.java) |
8081
| Create Http Task With Token | [source code](https://github.com/googleapis/java-tasks/blob/main/samples/snippets/src/main/java/com/example/task/CreateHttpTaskWithToken.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-tasks&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/task/CreateHttpTaskWithToken.java) |
8182
| Create Queue | [source code](https://github.com/googleapis/java-tasks/blob/main/samples/snippets/src/main/java/com/example/task/CreateQueue.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-tasks&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/task/CreateQueue.java) |

samples/native-image-sample/README.md

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# Cloud Tasks Sample Application with Native Image
2+
3+
The Cloud Tasks sample application demonstrates some common operations with
4+
[Google Cloud Tasks](https://cloud.google.com/tasks) and is compatible with
5+
Native Image compilation.
6+
7+
This application will create a new queue called `graal-test-queue` if it does
8+
not already exist.
9+
It will then submit a new task to this queue.
10+
11+
## Setup Instructions
12+
13+
1. Follow the [GCP Project and Native Image Setup Instructions](../../README.md).
14+
15+
2. [Enable the Cloud Tasks APIs](https://console.cloud.google.com/apis/api/cloudtasks.googleapis.com).
16+
17+
### Run with Native Image Compilation
18+
19+
Navigate to this directory in a new terminal.
20+
21+
1. Compile the application using the Native Image Compiler. This step may take a few minutes.
22+
23+
```
24+
$ mvn package -P native
25+
```
26+
27+
The project uses an environment variable `LOCATION_ID` to run the test.
28+
29+
2. Run the application:
30+
31+
```
32+
$ LOCATION_ID=us-east1 ./target/tasks-sample
33+
```
34+
35+
The sample application uses an environment variable `LOCATION_ID`.
36+
37+
3. The application runs through some basic Cloud Tasks operations (create queue, create task) and then prints some results of the operations.
38+
39+
```
40+
Test queue ready: name: "projects/xxxxxxxxxx/locations/us-central1/queues/graal-test-queue-4009"
41+
rate_limits {
42+
max_dispatches_per_second: 500.0
43+
max_burst_size: 100
44+
max_concurrent_dispatches: 1
45+
}
46+
retry_config {
47+
max_attempts: 100
48+
min_backoff {
49+
nanos: 100000000
50+
}
51+
max_backoff {
52+
seconds: 3600
53+
}
54+
max_doublings: 16
55+
}
56+
state: RUNNING
57+
58+
Created task: name: "projects/xxxxxxxxxx/locations/us-central1/queues/graal-test-queue-4009/tasks/5886258204485021611"
59+
http_request {
60+
url: "https://google.com/"
61+
http_method: GET
62+
headers {
63+
key: "User-Agent"
64+
value: "Google-Cloud-Tasks"
65+
}
66+
}
67+
schedule_time {
68+
seconds: 1613189391
69+
nanos: 486293000
70+
}
71+
create_time {
72+
seconds: 1613189391
73+
}
74+
dispatch_deadline {
75+
seconds: 600
76+
}
77+
view: BASIC
78+
79+
Queue purged
80+
Queue deleted
81+
```
82+
83+
4. Run the test in the project in the native-image mode
84+
85+
```
86+
$ LOCATION_ID=us-east1 mvn test -P native
87+
...
88+
[INFO] -------------------------------------------------------
89+
[INFO] T E S T S
90+
[INFO] -------------------------------------------------------
91+
[INFO] Running com.example.tasks.ITNativeImageTasksSample
92+
...
93+
[INFO] --- native-maven-plugin:0.9.9:test (test-native) @ native-image-sample ---
94+
[INFO] ====================
95+
[INFO] Initializing project: native-image-sample
96+
...
97+
com.example.tasks.ITNativeImageTasksSample > testRunSampleApplication SUCCESSFUL
98+
99+
100+
Test run finished after 1025 ms
101+
[ 3 containers found ]
102+
[ 0 containers skipped ]
103+
[ 3 containers started ]
104+
[ 0 containers aborted ]
105+
[ 3 containers successful ]
106+
[ 0 containers failed ]
107+
[ 1 tests found ]
108+
[ 0 tests skipped ]
109+
[ 1 tests started ]
110+
[ 0 tests aborted ]
111+
[ 1 tests successful ]
112+
[ 0 tests failed ]
113+
114+
[INFO] ------------------------------------------------------------------------
115+
[INFO] BUILD SUCCESS
116+
[INFO] ------------------------------------------------------------------------
117+
[INFO] Total time: 02:27 min
118+
...
119+
```

samples/native-image-sample/pom.xml

+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>com.example.tasks</groupId>
7+
<artifactId>native-image-sample</artifactId>
8+
<name>Native Image Sample</name>
9+
10+
<!--
11+
The parent pom defines common style checks and testing strategies for our samples.
12+
Removing or replacing it should not affect the execution of the samples in anyway.
13+
-->
14+
<parent>
15+
<groupId>com.google.cloud.samples</groupId>
16+
<artifactId>shared-configuration</artifactId>
17+
<version>1.2.0</version>
18+
</parent>
19+
20+
<properties>
21+
<!-- Java 8 because the Kokoro Sample test uses that Java version -->
22+
<maven.compiler.target>1.8</maven.compiler.target>
23+
<maven.compiler.source>1.8</maven.compiler.source>
24+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
25+
</properties>
26+
27+
<dependencyManagement>
28+
<dependencies>
29+
<dependency>
30+
<groupId>com.google.cloud</groupId>
31+
<artifactId>libraries-bom</artifactId>
32+
<version>24.2.0</version>
33+
<type>pom</type>
34+
<scope>import</scope>
35+
</dependency>
36+
</dependencies>
37+
</dependencyManagement>
38+
39+
<dependencies>
40+
<dependency>
41+
<groupId>com.google.cloud</groupId>
42+
<artifactId>google-cloud-core</artifactId>
43+
</dependency>
44+
<dependency>
45+
<groupId>com.google.cloud</groupId>
46+
<artifactId>google-cloud-tasks</artifactId>
47+
</dependency>
48+
<dependency>
49+
<groupId>junit</groupId>
50+
<artifactId>junit</artifactId>
51+
<version>4.13.2</version>
52+
<scope>test</scope>
53+
</dependency>
54+
<dependency>
55+
<groupId>com.google.truth</groupId>
56+
<artifactId>truth</artifactId>
57+
<version>1.1.3</version>
58+
<scope>test</scope>
59+
</dependency>
60+
</dependencies>
61+
62+
<build>
63+
<!-- These plugins enable building the application to a JAR *not* using Native Image -->
64+
<plugins>
65+
<plugin>
66+
<groupId>org.apache.maven.plugins</groupId>
67+
<artifactId>maven-jar-plugin</artifactId>
68+
<configuration>
69+
<archive>
70+
<manifest>
71+
<mainClass>com.example.TasksSampleApplication</mainClass>
72+
</manifest>
73+
</archive>
74+
</configuration>
75+
</plugin>
76+
</plugins>
77+
</build>
78+
79+
<profiles>
80+
<profile>
81+
<id>native</id>
82+
<dependencies>
83+
<dependency>
84+
<!-- TODO: remove this when it's no longer needed -->
85+
<groupId>com.google.cloud</groupId>
86+
<artifactId>native-image-support</artifactId>
87+
<version>0.10.0</version>
88+
</dependency>
89+
<dependency>
90+
<groupId>org.junit.vintage</groupId>
91+
<artifactId>junit-vintage-engine</artifactId>
92+
<version>5.8.2</version>
93+
</dependency>
94+
<dependency>
95+
<groupId>org.graalvm.buildtools</groupId>
96+
<artifactId>junit-platform-native</artifactId>
97+
<version>0.9.9</version>
98+
<scope>test</scope>
99+
</dependency>
100+
</dependencies>
101+
<build>
102+
<plugins>
103+
<plugin>
104+
<groupId>org.apache.maven.plugins</groupId>
105+
<artifactId>maven-surefire-plugin</artifactId>
106+
<!-- Must use older version of surefire plugin for native-image testing. -->
107+
<version>2.22.2</version>
108+
<configuration>
109+
<includes>
110+
<include>**/IT*</include>
111+
</includes>
112+
</configuration>
113+
</plugin>
114+
<plugin>
115+
<groupId>org.graalvm.buildtools</groupId>
116+
<artifactId>native-maven-plugin</artifactId>
117+
<version>0.9.9</version>
118+
<extensions>true</extensions>
119+
<configuration>
120+
<mainClass>com.example.tasks.TasksSampleApplication</mainClass>
121+
<buildArgs>
122+
<buildArg>--no-fallback</buildArg>
123+
<buildArg>--no-server</buildArg>
124+
<buildArg>--initialize-at-build-time</buildArg>
125+
<buildArg>--features=com.google.cloud.nativeimage.features.ProtobufMessageFeature</buildArg>
126+
</buildArgs>
127+
</configuration>
128+
<executions>
129+
<execution>
130+
<id>build-native</id>
131+
<goals>
132+
<goal>build</goal>
133+
<goal>test</goal>
134+
</goals>
135+
<phase>package</phase>
136+
</execution>
137+
<execution>
138+
<id>test-native</id>
139+
<goals>
140+
<goal>test</goal>
141+
</goals>
142+
<phase>test</phase>
143+
</execution>
144+
</executions>
145+
</plugin>
146+
</plugins>
147+
</build>
148+
</profile>
149+
</profiles>
150+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.tasks;
18+
19+
import com.google.cloud.ServiceOptions;
20+
import com.google.cloud.tasks.v2.CloudTasksClient;
21+
import com.google.cloud.tasks.v2.CreateQueueRequest;
22+
import com.google.cloud.tasks.v2.HttpMethod;
23+
import com.google.cloud.tasks.v2.HttpRequest;
24+
import com.google.cloud.tasks.v2.LocationName;
25+
import com.google.cloud.tasks.v2.Queue;
26+
import com.google.cloud.tasks.v2.QueueName;
27+
import com.google.cloud.tasks.v2.RateLimits;
28+
import com.google.cloud.tasks.v2.Task;
29+
import java.io.IOException;
30+
import java.util.UUID;
31+
32+
/** Sample application demonstrating Native Image compatibility with Google Cloud Tasks APIs. */
33+
public class TasksSampleApplication {
34+
/**
35+
* Queue name randomness added to avoid FAILED_PRECONDITION: The queue cannot be created because a
36+
* queue with this name existed too recently.
37+
*/
38+
private static final String GRAALVM_TEST_QUEUE_NAME = "graal-test-queue-";
39+
40+
private static final String LOCATION_ID = System.getenv("LOCATION_ID");
41+
42+
/** Runs the Cloud Tasks sample application. */
43+
public static void main(String[] args) throws IOException {
44+
String projectId = ServiceOptions.getDefaultProjectId();
45+
LocationName parent = LocationName.of(projectId, LOCATION_ID);
46+
QueueName queueName =
47+
QueueName.of(
48+
parent.getProject(),
49+
parent.getLocation(),
50+
GRAALVM_TEST_QUEUE_NAME + UUID.randomUUID().toString());
51+
52+
try (CloudTasksClient client = CloudTasksClient.create()) {
53+
// Create queue
54+
Queue queue =
55+
Queue.newBuilder()
56+
.setName(queueName.toString())
57+
.setRateLimits(RateLimits.newBuilder().setMaxConcurrentDispatches(1).build())
58+
.build();
59+
60+
CreateQueueRequest createQueueRequest =
61+
CreateQueueRequest.newBuilder().setParent(parent.toString()).setQueue(queue).build();
62+
63+
Queue createdQueue = client.createQueue(createQueueRequest);
64+
System.out.println("Test queue ready: " + createdQueue);
65+
66+
// Create task
67+
HttpRequest taskTarget =
68+
HttpRequest.newBuilder()
69+
.setUrl("https://google.com")
70+
.setHttpMethod(HttpMethod.GET)
71+
.build();
72+
73+
Task taskRequest = Task.newBuilder().setHttpRequest(taskTarget).build();
74+
Task task = client.createTask(queueName, taskRequest);
75+
System.out.println("Created task: " + task);
76+
77+
// Cleanup
78+
client.purgeQueue(queueName);
79+
System.out.println("Queue purged");
80+
81+
client.deleteQueue(queueName);
82+
System.out.println("Queue deleted");
83+
}
84+
}
85+
}

0 commit comments

Comments
 (0)