Skip to content
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

Added native-image examples #351

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions native-image-examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Vert.x Native Image examples

This directory includes basic example of vert.x applications that have been
compiled to a native image.

* [vertx http server](./vertx-http-server)
* [vertx https server](./vertx-https-server)
* [vertx web client](./vertx-web-client)
* [vertx grpc server](./vertx-grpc-server)
* [vertx grpc client](./vertx-grpc-client)
122 changes: 122 additions & 0 deletions native-image-examples/vertx-grpc-client/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?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>io.vertx</groupId>
<artifactId>vertx-native-image-grpc-client-example</artifactId>
<version>3.7.0</version>

<properties>
<vertx.verticle>io.vertx.example.MainVerticle</vertx.verticle>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.testSource>1.8</maven.compiler.testSource>
<maven.compiler.testTarget>1.8</maven.compiler.testTarget>
<graal.version>19.0.0</graal.version>
<vertx.grpc.version>1.16.1</vertx.grpc.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-stack-depchain</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>com.oracle.substratevm</groupId>
<artifactId>svm-driver</artifactId>
<version>${graal.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-grpc</artifactId>
</dependency>
</dependencies>

<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.4.1.Final</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.0</version>
<configuration>
<!--
The version of protoc must match protobuf-java. If you don't depend on
protobuf-java directly, you will be transitively depending on the
protobuf-java version that grpc depends on.
-->
<protocArtifact>com.google.protobuf:protoc:3.6.1:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.vertx:protoc-gen-grpc-java:${vertx.grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>io.reactiverse</groupId>
<artifactId>vertx-maven-plugin</artifactId>
<version>1.0.18</version>
<executions>
<execution>
<id>vmp</id>
<goals>
<goal>initialize</goal>
<goal>package</goal>
</goals>
</execution>
</executions>
<configuration>
<redeploy>true</redeploy>
</configuration>
</plugin>
<plugin>
<groupId>com.oracle.substratevm</groupId>
<artifactId>native-image-maven-plugin</artifactId>
<version>${graal.version}</version>
<executions>
<execution>
<goals>
<goal>native-image</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
<configuration>
<imageName>${project.artifactId}</imageName>
<mainClass>io.vertx.core.Launcher</mainClass>
<buildArgs>--report-unsupported-elements-at-runtime --allow-incomplete-classpath</buildArgs>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// TODO: this file should be removed once core uses netty > 4.1.36
import com.oracle.svm.core.annotate.*;
import org.graalvm.nativeimage.*;

/**
* This substitution allows the usage of platform specific code to do low level
* buffer related tasks
*/
@TargetClass(className = "io.netty.util.internal.CleanerJava6")
final class Target_io_netty_util_internal_CleanerJava6 {

@Alias
@RecomputeFieldValue(kind = RecomputeFieldValue.Kind.FieldOffset, declClassName = "java.nio.DirectByteBuffer", name = "cleaner")
private static long CLEANER_FIELD_OFFSET;
}

/**
* This substitution allows the usage of platform specific code to do low level
* buffer related tasks
*/
@TargetClass(className = "io.netty.util.internal.PlatformDependent0")
final class Target_io_netty_util_internal_PlatformDependent0 {

@Alias
@RecomputeFieldValue(kind = RecomputeFieldValue.Kind.FieldOffset, declClassName = "java.nio.Buffer", name = "address")
private static long ADDRESS_FIELD_OFFSET;
}

@TargetClass(className = "io.netty.util.internal.PlatformDependent")
final class Target_io_netty_util_internal_PlatformDependent {
/**
* The class PlatformDependent caches the byte array base offset by reading the
* field from PlatformDependent0. The automatic recomputation of Substrate VM
* correctly recomputes the field in PlatformDependent0, but since the caching
* in PlatformDependent happens during image building, the non-recomputed value
* is cached.
*/
@Alias
@RecomputeFieldValue(kind = RecomputeFieldValue.Kind.ArrayBaseOffset, declClass = byte[].class)
private static long BYTE_ARRAY_BASE_OFFSET;
}

/**
* This substitution allows the usage of platform specific code to do low level
* buffer related tasks
*/
@TargetClass(className = "io.netty.util.internal.shaded.org.jctools.util.UnsafeRefArrayAccess")
final class Target_io_netty_util_internal_shaded_org_jctools_util_UnsafeRefArrayAccess {

@Alias
@RecomputeFieldValue(kind = RecomputeFieldValue.Kind.ArrayIndexShift, declClass = Object[].class)
public static int REF_ELEMENT_SHIFT;
}

/**
* This substitution forces the usage of the blocking DNS resolver
*/
@TargetClass(className = "io.vertx.core.spi.resolver.ResolverProvider")
final class Target_io_vertx_core_spi_resolver_ResolverProvider {

@Substitute
public static io.vertx.core.spi.resolver.ResolverProvider factory(io.vertx.core.Vertx vertx, io.vertx.core.dns.AddressResolverOptions options) {
return new io.vertx.core.impl.resolver.DefaultResolverProvider();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.vertx.example;

import io.grpc.ManagedChannel;
import io.grpc.examples.helloworld.GreeterGrpc;
import io.grpc.examples.helloworld.HelloRequest;
import io.vertx.core.AbstractVerticle;
import io.vertx.grpc.VertxChannelBuilder;

public class MainVerticle extends AbstractVerticle {

@Override
public void start() {
ManagedChannel channel = VertxChannelBuilder
.forAddress(vertx, "localhost", 50051)
.usePlaintext(true)
.build();
GreeterGrpc.GreeterVertxStub stub = GreeterGrpc.newVertxStub(channel);
HelloRequest request = HelloRequest.newBuilder().setName("Paulo").build();
stub.sayHello(request, asyncResponse -> {
if (asyncResponse.succeeded()) {
System.out.println("Succeeded " + asyncResponse.result().getMessage());
} else {
asyncResponse.cause().printStackTrace();
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
syntax = "proto3";

option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";
option objc_class_prefix = "HLW";

package helloworld;

// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
string name = 1;
}

// The response message containing the greetings
message HelloReply {
string message = 1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Args = --initialize-at-build-time=io.netty,com.fasterxml.jackson,javax \
--initialize-at-run-time=io.netty.handler.codec.http.HttpObjectEncoder,io.netty.handler.codec.http.websocketx.WebSocket00FrameEncoder,io.netty.handler.codec.http2.DefaultHttp2FrameWriter,io.netty.handler.ssl.ReferenceCountedOpenSslServerContext,io.netty.handler.ssl.JdkNpnApplicationProtocolNegotiator,io.netty.handler.ssl.ReferenceCountedOpenSslEngine,io.netty.handler.ssl.ConscryptAlpnSslEngine,io.netty.handler.ssl.JettyNpnSslEngine,io.netty.handler.ssl.ReferenceCountedOpenSslClientContext,io.vertx.core.net.impl.transport.EpollTransport,io.vertx.core.net.impl.transport.KQueueTransport \
-H:+UseServiceLoaderFeature \
-H:IncludeResources=(META-INF)/.* \
-H:ReflectionConfigurationFiles=classes/${.}/reflection.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[
{
"name": "io.netty.channel.socket.nio.NioServerSocketChannel",
"methods": [
{ "name": "<init>", "parameterTypes": [] }
]
},
{
"name": "io.vertx.core.impl.launcher.commands.RunCommand",
"allDeclaredConstructors": true,
"allDeclaredMethods": true
},
{
"name": "io.vertx.core.impl.launcher.commands.VertxIsolatedDeployer",
"allDeclaredConstructors": true,
"allDeclaredMethods": true
},
{
"name": "java.lang.Long",
"allDeclaredConstructors": true
},
{
"name": "java.lang.Integer",
"allDeclaredConstructors": true
},

{
"name": "io.vertx.example.MainVerticle",
"allDeclaredConstructors": true,
"allDeclaredMethods": true
}
]