POLYGOT JAVA EE ON
THE GRAALVM
Ryan Cuprak
What is GraalVM?
• OpenJDK distribution from Oracle
• Java 8 update 212
• Oracle technologies not included (Java Webstart etc.)
• Community & Enterprise Editions
• Enables polygot development:
• JavaScript, Python, R, Ruby, C, C++
• Can generate native, statically linked executables
OpenJDK Distributions
Confused about distributions?
OpenJDK Distributions
GraalVM vs. other OpenJDK distributions:
• Additional languages (JavaScript, R, Python, Ruby etc.)
• Different JIT compiler (better performance)
• Can generate native images
• Isolates – multiple VM instances in process
Why GraalVM?
• Interop with other languages painful/high impact:
• JNI
• Process API painful
• Nashorn can’t keep up…
• Java can’t compete in serverless
• Startup time to long
• Image size too big
GraalVM native-image
Generates statically
linked native executable
GraalVM Overview
Source: https://www.graalvm.org/docs/
GraalVM Components
GraalVM Compiler Just-in-Time (JIT) compiler
implemented in Java (like C2 in
hotspot)
GraalVM Native Image Ahead-of-time compilation technology
that produces executable binaries of
class files.
Truffle Framework Framework for implementing
languages on top of GraalVM
Instrumentation Support Language-agnostic debugger, profiler,
heap viewer tool chain
GraalVM Pieces
• Substrate VM – Ahead-of-Time Compiler, turns a regular
Java application into a binary file
• https://github.com/oracle/graal/blob/master/substratevm/README.
md
• Sulong – high-performance LLVM bitcode interpreter built
on the GraalVM.
• https://github.com/oracle/graal/tree/master/sulong
GraalVM Pieces
• Java Hotspot VM + GraalVM compiler
• Node.js runtime + GraalVM JavaScript interpreterRuntimes
• GraalVM compiler
• JavaScript interpreter
• LLVM bytecode interpreter
• GraalVM Polygot API
Libraries
• JavaScript REPL
• LLVM bitcode interpreter
• GraalVM Updater
Utilities
GraalVM
Available Libraries
0 100000 200000 300000 400000 500000 600000 700000 800000
Java
Ruby
R
Python
Node
Non-Java
• Java packages 30%
• C/C++ Libraries not included!
Top Libraries
Why Polygot?
- Use best tool for the job -
GraalVM FAQ
• Can’t Java already run JavaScript, Python, Ruby via
Nashorn/Rhino, Jython, and Ruby?
• Was Nashorn killed because of GraalVM?
• Native libraries, what’s wrong with JNI?
• Is native-client related to jlink?
Yes – but with lots of limitations…
Very challenging…
- No -
- No -
GraalVM FAQ…
• With GraalVM can I leverage native libraries etc.?
• Does it require a commercial license for production?
• What platforms are supported?
• Do existing Java/Jakarta EE containers run on GraalVM?
- Yes -
- No -
- Mac / Linux -
- Yes -
GraalVM Possibilities
Invoke
Invoke
Invoke
Invoke
Invoke
Invoke
GraalVM Possibilities…
Invoke Invoke
Invoke Invoke
Invoke Invoke
GraalVM + Jakarta EE
• Polygot expands JEE ecosystem possibilities
• Unifies enterprise architecture
• No separate processes for python/Javascript
• Use same libraries server-side/client-side
• Example: Run same UI validation logic on server as client.
• No clunky language interop packages
• Improves performance (R/Ruby/Java)
• Simplifies runtime architecture and coordination
JEE + GraalVM
Wrap
Invoke
Installation https://www.graalvm.org/downloads/
Docker
containers
available.
docker pull
oracle/graalvm-ce:19.0.2
Installation
• tar xfz graalvm-ce.tar.gz
• MacOS X
• mv graalvm-ce-19.0.2 /Library/Java/JavaVirtualMachines/
• Jenv (http://www.jenv.be)
• jenv add <installation director>
• jenv global 1.8.0.212 (can be confusing with Oracle’s OpenJDK)
• Add GRAAL_HOME environment variable (convenience)
export GRAAL_HOME= /Library/Java/JavaVirtualMachines/graalvm-ce-
19.0.2/Contents/home
If graalvm directory own by root complicates installing
additional languages/packages.
Maven Toolchain
~/.m2/toolchains.xml
Installation Check
JDK Differences
Removed Added
javafxpackager gu
javapackager hsdb
jmc js
lli
node
npm
polygot
Additional Languages
• Install optional languages:
• gu install R
• gu install python
• gu install ruby
• gu install native-image
• New additions to bin
R Rscript Bundle Bundler Gem
graalpython irb native-image native-image-configure rake
rdoc rebuild-images ri ruby truffleruby
Debugging
Project Setup - Maven
References
~/.m2/toolchains.xml
Maven Coordinates
Hello World Example
GraalVM API Package
Polygot (org.graalvm.polyglot)
Proxy (org.graalvm.polyglot.proxy)
IO (org.graalvm.polyglot.io)
Key Polygot Package Classes
• Context – context for guest language(s), evaluates code
• Engine – execution engine, enables inspection
• Source – source code unit to be evaluated
• Value – polygot value accessed via language agnostic
operations.
Interoperability
R
• eval.polyglot('languageId', 'code’)
• eval.polyglot(path = '/path/to/file.extension’)
• export('polyglot-value-name', rObject)
• import('exported-polyglot-value-name’)
Ruby/Python
• Polygot.eval(‘languageId’,’code’)
JavaScript
• Polygot.export/import/eval
Interoperability – Data Types
• JavaScript
var BigInteger = Java.type('java.math.BigInteger’);
• R
imageClass <- java.type('java.awt.image.BufferedImage’)
• Ruby
BigDecimal = Java.type('java.math.BigDecimal’)
• Python
import java
BigDecimal = java.type('java.math.BigDecimal')
C/C++ Interoperability
clang -g -O1 -c -emit-llvm -
I$GRAALVM_HOME/jre/languages/llvm/include hello.c
Use #include<polygot.h>
Nashorn Interoperability
https://github.com/graalvm/graaljs/blob/master/docs/user/
JavaInterop.md
Jakarta EE/GraalVM Hello World
• Jakara EE application running on Payara Micro
• Payara Micro, JEE container in a jar file (70 megs)
• Function implemented in each language that:
• Accepts a parameter
• Returns a message
• Use Warm-up bean to service requests fast
• Languages:
• R, JavaScript, Python, Ruby, C
Jakarta EE/GraalVM Hello World Demo
Express + Jakarta EE
Wrap
• Express web framework for Node.js
• Easy-to-use framework for developing web apps
• Start application in Node, then launch the application container
(Payara Micro)
• Express then calls into Payara Micro to invoke EJBs
Express + Jakarta EE
https://expressjs.com
Express + Jakarta EE
Express + Jakarta EE
Jakarta EE + Python
• Use a CDI interceptor to catch runaway scripts
• Seamlessly pass JPA entities to Python to manipulate
Future Opportunities!
• Jakarta EE no longer limited to Java?
• Non-Java bindings for other languages
• C++ EJB Singleton?
• Dependency Injection for non-Java objects
• Isolates instead of classloaders
• GraalVM isolates – multiple independent VM instances in JVM
• Improves security
• Limits impact crashes
• Reactive support?
• Many possibilities…
Challenges & Limitations
• Window distribution incomplete
• Building & packaging
• Java cannot launch Node.js
• R & Python support is experimental
• Python C API is incomplete
• Practically no packages are supported
• graalpython -m ginstall install numpy
Challenges & Limitations…
• Rudimentary Documentation
• On-going changes to GraalVM
• Run headless (NodeJS invoking Java)
• Lots of trial and error…
Best Practices
• Warm-up contexts to improve performance
@Startup @Singleton
• Limit access of untrusted scripts
• Monitor and kills runaway scripts
GraalVM EE Starter
Resources
• Gitter Community
https://gitter.im/graalvm/home
• Medium GraalVM Blog
https://medium.com/graalvm
• Wrapping SQLite
https://habr.com/ru/post/358700/
Q&A
Twitter: @ctjava
Email: rcuprak@gmail.com / r5k@3ds.com
Blog: cuprak.info
Linkedin: www.linkedin.com/in/rcuprak
Slides: www.slideshare.net/rcuprak/presentations

Polygot Java EE on the GraalVM

  • 1.
    POLYGOT JAVA EEON THE GRAALVM Ryan Cuprak
  • 2.
    What is GraalVM? •OpenJDK distribution from Oracle • Java 8 update 212 • Oracle technologies not included (Java Webstart etc.) • Community & Enterprise Editions • Enables polygot development: • JavaScript, Python, R, Ruby, C, C++ • Can generate native, statically linked executables
  • 3.
  • 4.
    OpenJDK Distributions GraalVM vs.other OpenJDK distributions: • Additional languages (JavaScript, R, Python, Ruby etc.) • Different JIT compiler (better performance) • Can generate native images • Isolates – multiple VM instances in process
  • 5.
    Why GraalVM? • Interopwith other languages painful/high impact: • JNI • Process API painful • Nashorn can’t keep up… • Java can’t compete in serverless • Startup time to long • Image size too big GraalVM native-image Generates statically linked native executable
  • 6.
  • 7.
    GraalVM Components GraalVM CompilerJust-in-Time (JIT) compiler implemented in Java (like C2 in hotspot) GraalVM Native Image Ahead-of-time compilation technology that produces executable binaries of class files. Truffle Framework Framework for implementing languages on top of GraalVM Instrumentation Support Language-agnostic debugger, profiler, heap viewer tool chain
  • 8.
    GraalVM Pieces • SubstrateVM – Ahead-of-Time Compiler, turns a regular Java application into a binary file • https://github.com/oracle/graal/blob/master/substratevm/README. md • Sulong – high-performance LLVM bitcode interpreter built on the GraalVM. • https://github.com/oracle/graal/tree/master/sulong
  • 9.
    GraalVM Pieces • JavaHotspot VM + GraalVM compiler • Node.js runtime + GraalVM JavaScript interpreterRuntimes • GraalVM compiler • JavaScript interpreter • LLVM bytecode interpreter • GraalVM Polygot API Libraries • JavaScript REPL • LLVM bitcode interpreter • GraalVM Updater Utilities
  • 10.
  • 11.
    Available Libraries 0 100000200000 300000 400000 500000 600000 700000 800000 Java Ruby R Python Node Non-Java • Java packages 30% • C/C++ Libraries not included!
  • 12.
  • 13.
    Why Polygot? - Usebest tool for the job -
  • 14.
    GraalVM FAQ • Can’tJava already run JavaScript, Python, Ruby via Nashorn/Rhino, Jython, and Ruby? • Was Nashorn killed because of GraalVM? • Native libraries, what’s wrong with JNI? • Is native-client related to jlink? Yes – but with lots of limitations… Very challenging… - No - - No -
  • 15.
    GraalVM FAQ… • WithGraalVM can I leverage native libraries etc.? • Does it require a commercial license for production? • What platforms are supported? • Do existing Java/Jakarta EE containers run on GraalVM? - Yes - - No - - Mac / Linux - - Yes -
  • 16.
  • 17.
  • 18.
    GraalVM + JakartaEE • Polygot expands JEE ecosystem possibilities • Unifies enterprise architecture • No separate processes for python/Javascript • Use same libraries server-side/client-side • Example: Run same UI validation logic on server as client. • No clunky language interop packages • Improves performance (R/Ruby/Java) • Simplifies runtime architecture and coordination
  • 19.
  • 20.
  • 21.
    Installation • tar xfzgraalvm-ce.tar.gz • MacOS X • mv graalvm-ce-19.0.2 /Library/Java/JavaVirtualMachines/ • Jenv (http://www.jenv.be) • jenv add <installation director> • jenv global 1.8.0.212 (can be confusing with Oracle’s OpenJDK) • Add GRAAL_HOME environment variable (convenience) export GRAAL_HOME= /Library/Java/JavaVirtualMachines/graalvm-ce- 19.0.2/Contents/home If graalvm directory own by root complicates installing additional languages/packages.
  • 22.
  • 23.
  • 24.
    JDK Differences Removed Added javafxpackagergu javapackager hsdb jmc js lli node npm polygot
  • 25.
    Additional Languages • Installoptional languages: • gu install R • gu install python • gu install ruby • gu install native-image • New additions to bin R Rscript Bundle Bundler Gem graalpython irb native-image native-image-configure rake rdoc rebuild-images ri ruby truffleruby
  • 26.
  • 27.
    Project Setup -Maven References ~/.m2/toolchains.xml
  • 28.
  • 29.
  • 30.
    GraalVM API Package Polygot(org.graalvm.polyglot) Proxy (org.graalvm.polyglot.proxy) IO (org.graalvm.polyglot.io)
  • 31.
    Key Polygot PackageClasses • Context – context for guest language(s), evaluates code • Engine – execution engine, enables inspection • Source – source code unit to be evaluated • Value – polygot value accessed via language agnostic operations.
  • 32.
    Interoperability R • eval.polyglot('languageId', 'code’) •eval.polyglot(path = '/path/to/file.extension’) • export('polyglot-value-name', rObject) • import('exported-polyglot-value-name’) Ruby/Python • Polygot.eval(‘languageId’,’code’) JavaScript • Polygot.export/import/eval
  • 33.
    Interoperability – DataTypes • JavaScript var BigInteger = Java.type('java.math.BigInteger’); • R imageClass <- java.type('java.awt.image.BufferedImage’) • Ruby BigDecimal = Java.type('java.math.BigDecimal’) • Python import java BigDecimal = java.type('java.math.BigDecimal')
  • 34.
    C/C++ Interoperability clang -g-O1 -c -emit-llvm - I$GRAALVM_HOME/jre/languages/llvm/include hello.c Use #include<polygot.h>
  • 35.
  • 36.
    Jakarta EE/GraalVM HelloWorld • Jakara EE application running on Payara Micro • Payara Micro, JEE container in a jar file (70 megs) • Function implemented in each language that: • Accepts a parameter • Returns a message • Use Warm-up bean to service requests fast • Languages: • R, JavaScript, Python, Ruby, C
  • 37.
  • 38.
    Express + JakartaEE Wrap • Express web framework for Node.js • Easy-to-use framework for developing web apps • Start application in Node, then launch the application container (Payara Micro) • Express then calls into Payara Micro to invoke EJBs
  • 39.
    Express + JakartaEE https://expressjs.com
  • 40.
  • 41.
  • 42.
    Jakarta EE +Python • Use a CDI interceptor to catch runaway scripts • Seamlessly pass JPA entities to Python to manipulate
  • 43.
    Future Opportunities! • JakartaEE no longer limited to Java? • Non-Java bindings for other languages • C++ EJB Singleton? • Dependency Injection for non-Java objects • Isolates instead of classloaders • GraalVM isolates – multiple independent VM instances in JVM • Improves security • Limits impact crashes • Reactive support? • Many possibilities…
  • 44.
    Challenges & Limitations •Window distribution incomplete • Building & packaging • Java cannot launch Node.js • R & Python support is experimental • Python C API is incomplete • Practically no packages are supported • graalpython -m ginstall install numpy
  • 45.
    Challenges & Limitations… •Rudimentary Documentation • On-going changes to GraalVM • Run headless (NodeJS invoking Java) • Lots of trial and error…
  • 46.
    Best Practices • Warm-upcontexts to improve performance @Startup @Singleton • Limit access of untrusted scripts • Monitor and kills runaway scripts
  • 47.
  • 48.
    Resources • Gitter Community https://gitter.im/graalvm/home •Medium GraalVM Blog https://medium.com/graalvm • Wrapping SQLite https://habr.com/ru/post/358700/
  • 49.
    Q&A Twitter: @ctjava Email: rcuprak@gmail.com/ r5k@3ds.com Blog: cuprak.info Linkedin: www.linkedin.com/in/rcuprak Slides: www.slideshare.net/rcuprak/presentations