Skip to content

Commit 93c91d8

Browse files
committed
Merge remote-tracking branch 'migration/main' into java-tasks-migration
2 parents f6055b4 + 8729b7d commit 93c91d8

File tree

15 files changed

+1195
-0
lines changed

15 files changed

+1195
-0
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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+
You will need to follow these prerequisite steps in order to run these samples:
14+
15+
1. If you have not already, [create a Google Cloud Platform Project](https://cloud.google.com/resource-manager/docs/creating-managing-projects#creating_a_project).
16+
17+
2. Install the [Google Cloud SDK](https://cloud.google.com/sdk/) which will allow you to run the sample with your project's credentials.
18+
19+
Once installed, log in with Application Default Credentials using the following command:
20+
21+
```
22+
gcloud auth application-default login
23+
```
24+
25+
**Note:** Authenticating with Application Default Credentials is convenient to use during development, but we recommend [alternate methods of authentication](https://cloud.google.com/docs/authentication/production) during production use.
26+
27+
3. Install the native image compiler.
28+
29+
You can follow the [official installation instructions](https://www.graalvm.org/docs/getting-started/#install-graalvm).
30+
After following the instructions, ensure that you install the Native Image extension installed by running:
31+
32+
```
33+
gu install native-image
34+
```
35+
36+
Once you finish following the instructions, verify that the default version of Java is set to the correct version by running `java -version` in a terminal.
37+
38+
You will see something similar to the below output:
39+
40+
```
41+
$ java -version
42+
43+
openjdk version "17.0.3" 2022-04-19
44+
OpenJDK Runtime Environment GraalVM CE 22.1.0 (build 17.0.3+7-jvmci-22.1-b06)
45+
OpenJDK 64-Bit Server VM GraalVM CE 22.1.0 (build 17.0.3+7-jvmci-22.1-b06, mixed mode, sharing)
46+
47+
```
48+
49+
4. [Enable the Cloud Tasks APIs](https://console.cloud.google.com/apis/api/cloudtasks.googleapis.com).
50+
51+
### Run with Native Image Compilation
52+
53+
Navigate to this directory in a new terminal.
54+
55+
1. The project uses an environment variable `LOCATION_ID` to run the test. Set the environment variable by calling:
56+
57+
```
58+
export LOCATION_ID=us-east1
59+
```
60+
61+
2. Compile the application using the native image Compiler. This step may take a few minutes.
62+
63+
```
64+
$ mvn package -P native
65+
```
66+
67+
68+
69+
3. Run the application:
70+
71+
```
72+
$ ./target/native-image-sample
73+
```
74+
75+
4. The application runs through some basic Cloud Tasks operations (create queue, create task) and then prints some results of the operations.
76+
77+
```
78+
Test queue ready: name: "projects/xxxxxxxxxx/locations/us-central1/queues/graal-test-queue-4009"
79+
rate_limits {
80+
max_dispatches_per_second: 500.0
81+
max_burst_size: 100
82+
max_concurrent_dispatches: 1
83+
}
84+
retry_config {
85+
max_attempts: 100
86+
min_backoff {
87+
nanos: 100000000
88+
}
89+
max_backoff {
90+
seconds: 3600
91+
}
92+
max_doublings: 16
93+
}
94+
state: RUNNING
95+
96+
Created task: name: "projects/xxxxxxxxxx/locations/us-central1/queues/graal-test-queue-4009/tasks/5886258204485021611"
97+
http_request {
98+
url: "https://google.com/"
99+
http_method: GET
100+
headers {
101+
key: "User-Agent"
102+
value: "Google-Cloud-Tasks"
103+
}
104+
}
105+
schedule_time {
106+
seconds: 1613189391
107+
nanos: 486293000
108+
}
109+
create_time {
110+
seconds: 1613189391
111+
}
112+
dispatch_deadline {
113+
seconds: 600
114+
}
115+
view: BASIC
116+
117+
Queue purged
118+
Queue deleted
119+
```
120+
121+
5. Run the test in the project in the native-image mode
122+
123+
```
124+
$ export LOCATION_ID=us-east1 && mvn test -P native
125+
...
126+
[INFO] -------------------------------------------------------
127+
[INFO] T E S T S
128+
[INFO] -------------------------------------------------------
129+
[INFO] Running com.example.tasks.ITNativeImageTasksSample
130+
...
131+
[INFO] --- native-maven-plugin:0.9.9:test (test-native) @ native-image-sample ---
132+
[INFO] ====================
133+
[INFO] Initializing project: native-image-sample
134+
...
135+
com.example.tasks.ITNativeImageTasksSample > testRunSampleApplication SUCCESSFUL
136+
137+
138+
Test run finished after 1025 ms
139+
[ 3 containers found ]
140+
[ 0 containers skipped ]
141+
[ 3 containers started ]
142+
[ 0 containers aborted ]
143+
[ 3 containers successful ]
144+
[ 0 containers failed ]
145+
[ 1 tests found ]
146+
[ 0 tests skipped ]
147+
[ 1 tests started ]
148+
[ 0 tests aborted ]
149+
[ 1 tests successful ]
150+
[ 0 tests failed ]
151+
152+
[INFO] ------------------------------------------------------------------------
153+
[INFO] BUILD SUCCESS
154+
[INFO] ------------------------------------------------------------------------
155+
[INFO] Total time: 02:27 min
156+
...
157+
```

tasks/native-image-sample/pom.xml

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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>26.1.3</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+
<groupId>org.junit.vintage</groupId>
85+
<artifactId>junit-vintage-engine</artifactId>
86+
<version>5.9.1</version>
87+
</dependency>
88+
<dependency>
89+
<groupId>org.graalvm.buildtools</groupId>
90+
<artifactId>junit-platform-native</artifactId>
91+
<version>0.9.14</version>
92+
<scope>test</scope>
93+
</dependency>
94+
</dependencies>
95+
<build>
96+
<plugins>
97+
<plugin>
98+
<groupId>org.apache.maven.plugins</groupId>
99+
<artifactId>maven-surefire-plugin</artifactId>
100+
<!-- Must use older version of surefire plugin for native-image testing. -->
101+
<version>2.22.2</version>
102+
<configuration>
103+
<includes>
104+
<include>**/IT*</include>
105+
</includes>
106+
</configuration>
107+
</plugin>
108+
<plugin>
109+
<groupId>org.graalvm.buildtools</groupId>
110+
<artifactId>native-maven-plugin</artifactId>
111+
<version>0.9.14</version>
112+
<extensions>true</extensions>
113+
<configuration>
114+
<mainClass>com.example.tasks.TasksSampleApplication</mainClass>
115+
<buildArgs>
116+
<buildArg>--no-fallback</buildArg>
117+
<buildArg>--no-server</buildArg>
118+
</buildArgs>
119+
</configuration>
120+
<executions>
121+
<execution>
122+
<id>build-native</id>
123+
<goals>
124+
<goal>build</goal>
125+
<goal>test</goal>
126+
</goals>
127+
<phase>package</phase>
128+
</execution>
129+
<execution>
130+
<id>test-native</id>
131+
<goals>
132+
<goal>test</goal>
133+
</goals>
134+
<phase>test</phase>
135+
</execution>
136+
</executions>
137+
</plugin>
138+
</plugins>
139+
</build>
140+
</profile>
141+
</profiles>
142+
</project>
Lines changed: 85 additions & 0 deletions
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)