Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Polygot Java EE on the GraalVM

248 views

Published on

The new GraalVM from Oracle supports multiple language including JavaScript, Python, Ruby, R, C++ as well as Java and other JVM languages. This opens up interesting possibilities for polygot enterprise applications. Now you can use a Node library in a Java application or call an R statistical function from an EJB. Previously, this type of integration was extremely challenging. This session will provide recipes to get up and running along with best practices and some cool demos.
Code: https://github.com/rcuprak/graalvm_jee

Published in: Software
  • Be the first to comment

  • Be the first to like this

Polygot Java EE on the GraalVM

  1. 1. POLYGOT JAVA EE ON THE GRAALVM Ryan Cuprak
  2. 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. 3. OpenJDK Distributions Confused about distributions?
  4. 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. 5. 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
  6. 6. GraalVM Overview Source: https://www.graalvm.org/docs/
  7. 7. 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
  8. 8. 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
  9. 9. 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
  10. 10. GraalVM
  11. 11. 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!
  12. 12. Top Libraries
  13. 13. Why Polygot? - Use best tool for the job -
  14. 14. 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 -
  15. 15. 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 -
  16. 16. GraalVM Possibilities Invoke Invoke Invoke Invoke Invoke Invoke
  17. 17. GraalVM Possibilities… Invoke Invoke Invoke Invoke Invoke Invoke
  18. 18. 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
  19. 19. JEE + GraalVM Wrap Invoke
  20. 20. Installation https://www.graalvm.org/downloads/ Docker containers available. docker pull oracle/graalvm-ce:19.0.2
  21. 21. 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.
  22. 22. Maven Toolchain ~/.m2/toolchains.xml
  23. 23. Installation Check
  24. 24. JDK Differences Removed Added javafxpackager gu javapackager hsdb jmc js lli node npm polygot
  25. 25. 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
  26. 26. Debugging
  27. 27. Project Setup - Maven References ~/.m2/toolchains.xml
  28. 28. Maven Coordinates
  29. 29. Hello World Example
  30. 30. GraalVM API Package Polygot (org.graalvm.polyglot) Proxy (org.graalvm.polyglot.proxy) IO (org.graalvm.polyglot.io)
  31. 31. 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.
  32. 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. 33. 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')
  34. 34. C/C++ Interoperability clang -g -O1 -c -emit-llvm - I$GRAALVM_HOME/jre/languages/llvm/include hello.c Use #include<polygot.h>
  35. 35. Nashorn Interoperability https://github.com/graalvm/graaljs/blob/master/docs/user/ JavaInterop.md
  36. 36. 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
  37. 37. Jakarta EE/GraalVM Hello World Demo
  38. 38. 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
  39. 39. Express + Jakarta EE https://expressjs.com
  40. 40. Express + Jakarta EE
  41. 41. Express + Jakarta EE
  42. 42. Jakarta EE + Python • Use a CDI interceptor to catch runaway scripts • Seamlessly pass JPA entities to Python to manipulate
  43. 43. 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…
  44. 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. 45. Challenges & Limitations… • Rudimentary Documentation • On-going changes to GraalVM • Run headless (NodeJS invoking Java) • Lots of trial and error…
  46. 46. Best Practices • Warm-up contexts to improve performance @Startup @Singleton • Limit access of untrusted scripts • Monitor and kills runaway scripts
  47. 47. GraalVM EE Starter
  48. 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. 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

×