Skip to content

Commit a4fa69b

Browse files
docs(sample): Add sample for Native Image support in Pub/Sub (#1026)
* docs(sample): Add sample for Native Image support in Pub/Sub * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix checkstyle issues * fix header and modify copyright year to 2022 * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix link in copyright * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Add helper class to utilities directory * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md fix checkstyle * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix sample to also run with standard java Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 3cd8bd1 commit a4fa69b

File tree

7 files changed

+938
-0
lines changed

7 files changed

+938
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,8 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-pubsub/tree/m
240240

241241
| Sample | Source Code | Try it |
242242
| --------------------------- | --------------------------------- | ------ |
243+
| Native Image Pub Sub Sample | [source code](https://github.com/googleapis/java-pubsub/blob/main/samples/native-image-sample/src/main/java/pubsub/NativeImagePubSubSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-pubsub&page=editor&open_in_editor=samples/native-image-sample/src/main/java/pubsub/NativeImagePubSubSample.java) |
244+
| Publish Operations | [source code](https://github.com/googleapis/java-pubsub/blob/main/samples/native-image-sample/src/main/java/utilities/PublishOperations.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-pubsub&page=editor&open_in_editor=samples/native-image-sample/src/main/java/utilities/PublishOperations.java) |
243245
| Create Avro Schema Example | [source code](https://github.com/googleapis/java-pubsub/blob/main/samples/snippets/src/main/java/pubsub/CreateAvroSchemaExample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-pubsub&page=editor&open_in_editor=samples/snippets/src/main/java/pubsub/CreateAvroSchemaExample.java) |
244246
| Create Proto Schema Example | [source code](https://github.com/googleapis/java-pubsub/blob/main/samples/snippets/src/main/java/pubsub/CreateProtoSchemaExample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-pubsub&page=editor&open_in_editor=samples/snippets/src/main/java/pubsub/CreateProtoSchemaExample.java) |
245247
| Create Pull Subscription Example | [source code](https://github.com/googleapis/java-pubsub/blob/main/samples/snippets/src/main/java/pubsub/CreatePullSubscriptionExample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-pubsub&page=editor&open_in_editor=samples/snippets/src/main/java/pubsub/CreatePullSubscriptionExample.java) |

samples/native-image-sample/README.md

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Pub/Sub Sample Application with Native Image
2+
3+
The Pub/Sub sample application demonstrates some common operations with Pub/Sub and is compatible with Native Image compilation.
4+
5+
## Setup Instructions
6+
7+
You will need to follow these prerequisite steps in order to run the samples:
8+
9+
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).
10+
11+
2. Install the [Google Cloud SDK](https://cloud.google.com/sdk/) which will allow you to run the sample with your project's credentials.
12+
13+
Once installed, log in with Application Default Credentials using the following command:
14+
15+
```
16+
gcloud auth application-default login
17+
```
18+
19+
**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.
20+
21+
3. Install the GraalVM compiler.
22+
23+
You can follow the [official installation instructions](https://www.graalvm.org/docs/getting-started/#install-graalvm) from the GraalVM website.
24+
After following the instructions, ensure that you install the native image extension installed by running:
25+
26+
```
27+
gu install native-image
28+
```
29+
30+
Once you finish following the instructions, verify that the default version of Java is set to the GraalVM version by running `java -version` in a terminal.
31+
32+
You will see something similar to the below output:
33+
34+
```
35+
$ java -version
36+
37+
openjdk version "11.0.7" 2020-04-14
38+
OpenJDK Runtime Environment GraalVM CE 20.1.0 (build 11.0.7+10-jvmci-20.1-b02)
39+
OpenJDK 64-Bit Server VM GraalVM CE 20.1.0 (build 11.0.7+10-jvmci-20.1-b02, mixed mode, sharing)
40+
```
41+
42+
4. [Enable the Pub/Sub APIs](https://console.cloud.google.com/apis/api/pubsub.googleapis.com).
43+
44+
### Sample
45+
46+
Navigate to this directory in a new terminal.
47+
48+
1. Compile the application using the Native Image Compiler. This step may take a few minutes.
49+
50+
```
51+
mvn package -P native -DskipTests
52+
```
53+
54+
2. Run the application:
55+
56+
```
57+
./target/native-image-sample
58+
```
59+
60+
3. The application will create a new Pub/Sub topic, send and receive a message from it, and then delete the topic.
61+
62+
```
63+
Created topic: projects/YOUR_PROJECT_ID/topics/graal-pubsub-test-00e72640-4e36-4aff-84d2-13b7569b2289 under project: YOUR_PROJECT_ID
64+
Created pull subscription: projects/YOUR_PROJECT_ID/subscriptions/graal-pubsub-test-sub2fb5e3f3-cb26-439b-b88c-9cb0cfca9e45
65+
Published message with ID: 457327433078420
66+
Received Payload: Pub/Sub Native Image Test published message at timestamp: 2020-09-23T19:45:42.746514Z
67+
Deleted topic projects/YOUR_PROJECT_ID/topics/graal-pubsub-test-00e72640-4e36-4aff-84d2-13b7569b2289
68+
Deleted subscription projects/YOUR_PROJECT_ID/subscriptions/graal-pubsub-test-sub2fb5e3f3-cb26-439b-b88c-9cb0cfca9e45
69+
```
70+
71+
## Sample Integration Test with native Image Support
72+
73+
In order to run the sample integration test as a native image, call the following command:
74+
75+
```
76+
mvn test -Pnative
77+
```

samples/native-image-sample/pom.xml

+175
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
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
5+
http://maven.apache.org/xsd/maven-4.0.0.xsd">
6+
<modelVersion>4.0.0</modelVersion>
7+
<groupId>com.google.cloud</groupId>
8+
<artifactId>native-image-sample</artifactId>
9+
<name>Native Image Sample</name>
10+
<url>https://github.com/googleapis/java-pubsub</url>
11+
12+
<!--
13+
The parent pom defines common style checks and testing strategies for our samples.
14+
Removing or replacing it should not affect the execution of the samples in anyway.
15+
-->
16+
<parent>
17+
<groupId>com.google.cloud.samples</groupId>
18+
<artifactId>shared-configuration</artifactId>
19+
<version>1.2.0</version>
20+
</parent>
21+
22+
<properties>
23+
<maven.compiler.target>11</maven.compiler.target>
24+
<maven.compiler.source>11</maven.compiler.source>
25+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
26+
</properties>
27+
28+
<dependencyManagement>
29+
<dependencies>
30+
<dependency>
31+
<groupId>com.google.cloud</groupId>
32+
<artifactId>libraries-bom</artifactId>
33+
<version>24.3.0</version>
34+
<type>pom</type>
35+
<scope>import</scope>
36+
</dependency>
37+
</dependencies>
38+
</dependencyManagement>
39+
40+
<dependencies>
41+
<dependency>
42+
<groupId>com.google.cloud</groupId>
43+
<artifactId>google-cloud-core</artifactId>
44+
</dependency>
45+
<dependency>
46+
<groupId>com.google.cloud</groupId>
47+
<artifactId>google-cloud-pubsub</artifactId>
48+
</dependency>
49+
50+
<dependency>
51+
<groupId>junit</groupId>
52+
<artifactId>junit</artifactId>
53+
<version>4.13.2</version>
54+
<scope>test</scope>
55+
</dependency>
56+
<dependency>
57+
<groupId>com.google.truth</groupId>
58+
<artifactId>truth</artifactId>
59+
<version>1.1.3</version>
60+
<scope>test</scope>
61+
</dependency>
62+
</dependencies>
63+
64+
<build>
65+
<plugins>
66+
<plugin>
67+
<groupId>org.apache.maven.plugins</groupId>
68+
<artifactId>maven-jar-plugin</artifactId>
69+
<configuration>
70+
<archive>
71+
<manifest>
72+
<addClasspath>true</addClasspath>
73+
<classpathPrefix>dependency-jars/</classpathPrefix>
74+
<mainClass>pubsub.NativeImagePubSubSample</mainClass>
75+
</manifest>
76+
</archive>
77+
</configuration>
78+
</plugin>
79+
<plugin>
80+
<groupId>org.apache.maven.plugins</groupId>
81+
<artifactId>maven-dependency-plugin</artifactId>
82+
<version>3.2.0</version>
83+
<executions>
84+
<execution>
85+
<id>copy-dependencies</id>
86+
<phase>package</phase>
87+
<goals>
88+
<goal>copy-dependencies</goal>
89+
</goals>
90+
<configuration>
91+
<outputDirectory>
92+
${project.build.directory}/dependency-jars/
93+
</outputDirectory>
94+
</configuration>
95+
</execution>
96+
</executions>
97+
</plugin>
98+
</plugins>
99+
</build>
100+
101+
<!-- Native Profile-->
102+
<profiles>
103+
<profile>
104+
<id>native</id>
105+
106+
<dependencies>
107+
<dependency>
108+
<groupId>com.google.cloud</groupId>
109+
<artifactId>native-image-support</artifactId>
110+
<version>0.12.4</version>
111+
</dependency>
112+
<dependency>
113+
<groupId>org.junit.vintage</groupId>
114+
<artifactId>junit-vintage-engine</artifactId>
115+
<version>5.8.2</version>
116+
<scope>test</scope>
117+
</dependency>
118+
<dependency>
119+
<groupId>org.graalvm.buildtools</groupId>
120+
<artifactId>junit-platform-native</artifactId>
121+
<version>0.9.9</version>
122+
<scope>test</scope>
123+
</dependency>
124+
</dependencies>
125+
126+
<build>
127+
<plugins>
128+
<plugin>
129+
<groupId>org.apache.maven.plugins</groupId>
130+
<artifactId>maven-surefire-plugin
131+
</artifactId> <!-- Must use older version of surefire plugin for native-image testing. -->
132+
<version>2.22.2</version>
133+
<configuration>
134+
<includes>
135+
<include>**/*IT</include>
136+
</includes>
137+
</configuration>
138+
</plugin>
139+
<plugin>
140+
<groupId>org.graalvm.buildtools</groupId>
141+
<artifactId>native-maven-plugin</artifactId>
142+
<version>0.9.9</version>
143+
<extensions>true</extensions>
144+
<configuration>
145+
<mainClass>pubsub.NativeImagePubSubSample
146+
</mainClass>
147+
<buildArgs>
148+
<buildArg>--no-fallback</buildArg>
149+
<buildArg>--no-server</buildArg>
150+
<buildArg>--features=com.google.cloud.nativeimage.features.ProtobufMessageFeature</buildArg>
151+
</buildArgs>
152+
</configuration>
153+
<executions>
154+
<execution>
155+
<id>build-native</id>
156+
<goals>
157+
<goal>build</goal>
158+
<goal>test</goal>
159+
</goals>
160+
<phase>package</phase>
161+
</execution>
162+
<execution>
163+
<id>test-native</id>
164+
<goals>
165+
<goal>test</goal>
166+
</goals>
167+
<phase>test</phase>
168+
</execution>
169+
</executions>
170+
</plugin>
171+
</plugins>
172+
</build>
173+
</profile>
174+
</profiles>
175+
</project>

0 commit comments

Comments
 (0)