Sunday, July 14, 2024
HomeJavaGraalVM Native Picture Help within the AWS SDK for Java 2.x

GraalVM Native Picture Help within the AWS SDK for Java 2.x


We’re excited to announce that AWS SDK for Java 2.x (model 2.16.1 or later) now has out-of-the-box assist for GraalVM Native Image compilation.

GraalVM is a common digital machine that helps JVM-based languages (e.g. Java, Scala, Kotlin), dynamic languages (e.g. Python, JavaScript), and LLVM-based languages (e.g. C, C++). GraalVM Native Picture is likely one of the hottest options that GraalVM provides. It permits Forward-of-Time (AoT) compilation of Java packages right into a self-contained native executable, known as a local picture. The executable is optimized and accommodates every part wanted to run the appliance, and it has sooner startup time and smaller heap reminiscence footprint in contrast with a JVM. Due to this fact, native photos are well-suited for serverless purposes the place startup latency is essential.

We in contrast the startup efficiency of the executable created from GraalVM native picture with the identical software operating on JVM. The check software creates an S3Client with ApacheHttpClient and sends a easy GetObjectRequest. In our testing, we measured the SDK 2.x startup time and the primary request latency in addition to the reminiscence utilization. The outcomes point out that the native picture has considerably sooner startup and decrease reminiscence consumption.

Because of the nature of AoT, options that depend on dynamic class loading, proxies, or reflections have to be identified at construct time and require particular assist. For instance, the lessons use reflections have to be registered within the native picture configuration recordsdata. The newest Java SDK 2.x accommodates such configurations required for SDK lessons, and it’s able to be constructed right into a native picture.

The newest launch additionally features a new Maven Archetype, archetype-app-quickstart, that allows you to shortly bootstrap a native-image suitable Java software configured with the AWS SDK for Java 2.x as a dependency. On this weblog, we are going to present you use the archetype to create a brand new software with GraalVM Native picture assist.

Getting Began

Conditions

This put up assumes you could have some familiarity with Java programming, the Maven construct system, and the GraalVM runtime. To make use of this tutorial, it is advisable to have the next software program put in:

Java 8+
Apache Maven
GraalVM 21.0.0+
GraalVM Native Picture

Producing the appliance

To generate a undertaking, open a terminal (command line) window, run mvn archetype:generate with archetype-app-quickstart. The model of the archetype is identical because the Java SDK 2.x model, and you should utilize the most recent model. There are two modes you may select: interactive mode and batch mode.

In interactive mode, you’ll be prompted to supply required properties, one after the other.

mvn archetype:generate 
  -DarchetypeGroupId=software program.amazon.awssdk 
  -DarchetypeArtifactId=archetype-app-quickstart 
  -DarchetypeVersion=2.16.1

In batch mode, it is advisable to present all required properties directly. Under is the listing of the required properties and you may take a look at all out there choices right here.
Required Properties

    • service: the service consumer for use within the lambda perform, eg: s3, dynamodb. Just one service ought to be offered.
    • groupId: the Maven group ID of the appliance
    • artifactId: the Maven artifact ID of you software
    • httpClient: specifies the http consumer for use by the SDK consumer. Out there choices are url-connection-client (sync), apache-client (sync) and netty-nio-client (async).
    • nativeImage: specifies whether or not GraalVM Native Picture configuration ought to be included
mvn archetype:generate 
    -DarchetypeGroupId=software program.amazon.awssdk 
    -DarchetypeArtifactId=archetype-app-quickstart 
    -DarchetypeVersion=2.16.1 
    -DgroupId=com.check 
    -DartifactId=sample-project 
    -Dservice=s3  
    -DhttpClient=apache-client 
    -DnativeImage=true 
    -Dregion=us-west-2 
    -DinteractiveMode=false

The generated software has a dependency of AWS SDK for Java 2.16.1. The output shall be one thing like this

[INFO] ----------------------------------------------------------------------------
[INFO] Utilizing following parameters for creating undertaking from Archetype: archetype-app-quickstart:2.16.1
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Worth: com.check
[INFO] Parameter: artifactId, Worth: sample-project
[INFO] Parameter: model, Worth: 1.0-SNAPSHOT
[INFO] Parameter: package deal, Worth: com.check
[INFO] Parameter: packageInPathFormat, Worth: com/check
[INFO] Parameter: service, Worth: s3
[INFO] Parameter: model, Worth: 1.0-SNAPSHOT
[INFO] Parameter: package deal, Worth: com.check
[INFO] Parameter: nativeImage, Worth: true
[INFO] Parameter: httpClient, Worth: apache-client
[INFO] Parameter: groupId, Worth: com.check
[INFO] Parameter: javaSdkVersion, Worth: 2.16.1
[INFO] Parameter: artifactId, Worth: sample-project
[INFO] Challenge created from Archetype in dir: /tmp/sample-project
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Complete time:  4.004 s
[INFO] Completed at: 2021-02-17T15:07:45-08:00
[INFO] ------------------------------------------------------------------------

When the archetype:generate objective completes, you should have a Maven undertaking with the construction as follows

sample-project
├── README.md
├── pom.xml
└── src
    ├── major
    │   ├── java
    │   │   └── com
    │   │       └── check
    │   │           ├── App.java
    │   │           ├── DependencyFactory.java
    │   │           └── Handler.java
    │   └── sources
    │       └── simplelogger.properties
    └── check
        └── java
            └── com
                └── check
                    └── HandlerTest.java

The App.java class is the entry level for the appliance. The DependencyFactory.java class accommodates the configured SDK consumer.

Constructing the appliance

To construct the appliance, run the next instructions:

# Go to undertaking listing created. 
# The identify ought to be the identical because the artifactId offered
cd sample-project

# Construct the appliance
mvn clear package deal

Making a native executable

You possibly can run the next command to create a local picture:

mvn clear package deal -P native-image

As soon as it completes, it would generate an executable within the goal folder. The identify ought to be the identical because the artifactId offered.

-H:ReflectionConfigurationResources=META-INF/native-image/software program.amazon.awssdk/s3/reflect-config.json 
-H:EnableURLProtocols=https 
-H:+ReportUnsupportedElementsAtRuntime 
-H:ReflectionConfigurationResources=META-INF/native-image/software program.amazon.awssdk/sdk-core/reflect-config.json 
-H:ResourceConfigurationResources=META-INF/native-image/software program.amazon.awssdk/sdk-core/resource-config.json 
-H:ReflectionConfigurationResources=META-INF/native-image/software program.amazon.awssdk/apache-client/reflect-config.json 
-H:ResourceConfigurationResources=META-INF/native-image/software program.amazon.awssdk/apache-client/resource-config.json 
-H:DynamicProxyConfigurationResources=META-INF/native-image/software program.amazon.awssdk/apache-client/proxy-config.json 
-H:FallbackThreshold=0 
-H:ClassInitialization=org.slf4j:build_time 
-H:Class=com.check.App 
-H:Title=sample-project 
-H:CLibraryPath=/Library/Java/JavaVirtualMachines/graalvm-ce-java8-21.0.0/Contents/House/jre/lib/svm/clibraries/darwin-amd64 

]
[sample-project:33940]    classlist:   6,671.68 ms,  1.36 GB
[sample-project:33940]        (cap):   4,022.10 ms,  1.36 GB
[sample-project:33940]        setup:   6,922.21 ms,  1.36 GB
[sample-project:33940]     (clinit):   1,352.10 ms,  2.80 GB
[sample-project:33940]   (typeflow):  33,266.42 ms,  2.80 GB
[sample-project:33940]    (objects):  16,911.48 ms,  2.80 GB
[sample-project:33940]   (options):   1,717.77 ms,  2.80 GB
[sample-project:33940]     evaluation:  54,700.08 ms,  2.80 GB
[sample-project:33940]     universe:   4,559.62 ms,  2.81 GB
[sample-project:33940]      (parse):  13,027.95 ms,  3.06 GB
[sample-project:33940]     (inline):   9,664.36 ms,  3.70 GB
[sample-project:33940]    (compile):  73,425.18 ms,  4.75 GB
[sample-project:33940]      compile: 100,632.59 ms,  4.75 GB
[sample-project:33940]        picture:  10,399.21 ms,  4.82 GB
[sample-project:33940]        write:   1,834.74 ms,  4.82 GB
[sample-project:33940]      [total]: 186,280.64 ms,  4.82 GB
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Complete time:  03:12 min
[INFO] Completed at: 2021-02-17T15:12:31-08:00
[INFO] ------------------------------------------------------------------------

Operating the native executable

To execute the native picture, you may run the next command

goal/sample-project

The output ought to be just like the next:

2021-02-17 15:13:18:071 -0800 [main] INFO com.check.App - Software begins
2021-02-17 15:13:18:086 -0800 [main] INFO com.check.App - Software ends

Conclusion

On this weblog put up we confirmed you the advantages of utilizing native picture compiled of purposes utilizing the AWS SDK for Java 2x. We additionally confirmed you ways fast it’s to make use of the brand new archetype to get begin with native-image suitable purposes utilizing AWS SDK for Java 2.x. If you wish to study extra in regards to the archetype, you may take a look at the archetype-app-quickstart supply code on GitHub. Please give this new archetype a try to tell us what you assume through the GitHub points web page.

Zoe Wang

Zoe Wang

Zoe is a Software program Improvement Engineer engaged on the AWS SDK for Java. She is obsessed with constructing instruments to make software program growth simpler. You will discover her on GitHub @zoewangg.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments