SlideShare a Scribd company logo
1 of 32
Download to read offline
Node4J
Running Node.js in a Java
World
Dr. R. Ian Bull
EclipseSource
@irbull
Java and JavaScript
❖ Java a successful server side language
❖ JavaScript is a client side language
❖ SWT brought performant Java UIs to the desktop
❖ Node.js brought JavaScript to the server
❖ Java and JavaScript are two of the most popular
programming languages
Polyglot Systems
❖ Single language systems are rarely an option
❖ Legacy code
❖ New frameworks and technologies
❖ Evolving enterprises
❖ JEE will be here for another 20, 30, 50 (?) years
Bridging Java and JavaScript
❖ Three common Java technologies enable JS embedding
❖ Rhino
❖ Available since JDK 6
❖ Nashorn
❖ Replacing Rhino since JDK 8
❖ More performant
❖ V8 as a separate process, String based messages
Performance
❖ 30 Runs of the Esprima parser and tokenizer
❖ Nashorn compiles to bytecode
❖ V8 compiles to native assembly
❖ Best choice for raw JavaScript execution
J2V8
❖ A set of bindings that bring V8 to Java
❖ Inspired by SWT
❖ Create a thin JNI layer
❖ Expose (some) V8 API in Java
❖ Complicated logic lives in Java
J2V8 Goals
❖ Efficient JavaScript on Android
❖ Make JavaScript shine in an enterprise Java World
❖ Standard Java APIs
❖ Efficient Java / JavaScript bindings
J2V8 — History
❖ 1.0 Released in November 2014
❖ 2.0 Released in February 2015
❖ First presented at EclipseCon 2015
❖ 3.0 Released at EnterJS — Summer 2015
J2V8 Design
❖ Each V8 Object can be referenced using a Handle
❖ Each Object is stored in a V8 Persistent Object Store
❖ Objects must be explicitly freed
❖ Primitives where possible (no wrappers)
❖ Single Thread per isolate
Two-way binding
❖ JS functions and scripts can be invoked from Java
❖ Java methods can be called from JavaScript
❖ Data can be passed back and forth using V8Objects
J2V8 In Action — Tabris.js
❖ Mobile framework
❖ Apps written in JavaScript
❖ Native iOS and Android Apps
❖ Bindings to native UI components
Shameless Plug
Example
public String someJavaMethod(final String firstName, final String lastName) {
return firstName + ", " + lastName;
}
public void start() {
V8 v8 = V8.createV8Runtime();
v8.registerJavaMethod(this,
"someJavaMethod",
"someJavaMethod",
new Class[] { String.class, String.class });
v8.executeScript("var result = someJavaMethod('Ian', ‘Bull');");
String result = v8.getString("result");
System.out.println(result);
}
J2V8 —What’s New
❖ Typed Arrays
❖ Threads & Workers
❖ ES 6
❖ ChromeDev Tools
❖ NodeJS Support
Typed Arrays
V8Array result = (V8Array) v8.executeScript(""
+ "var buf = new ArrayBuffer(100);"
+ "var ints = new Int32Array(buf); "
+ "for(var i = 0; i < 25; i++) {"
+ " ints[i] = i;"
+ "}; "
+ “ints");
int[] ints = result.getIntegers(0, 25);
❖ Native support for JS Typed Arrays
❖ Access the values efficiently from Java
Threads
❖ Every thread can have it’s own Isolate (Isolated V8
Instance)
❖ V8Thread is a Java Thread with an associated Isolate
❖ Provide an easy way to execute JavaScript
Thread t = new V8Thread(new V8Runnable() {
public void run(V8 v8) {
int result = v8.executeIntegerScript("1+2");
}
});
t.start();
Executors
❖ Long running V8Thread with a message queue and
event loop
❖ Threads can communicate via message passing
❖ Useful for implementing Web Workers / Service
Workers
ES 6
❖ Snapshot builds of J2V8 support V8 4.10 & ES 6
❖ Arrows
❖ Classes
❖ Let / Const
❖ Interators + For..Of
❖ Generators
❖ …
Debug Support
❖ V8 (and now J2V8) no longer supports the Debug Agent
❖ JavaScript based Debug API is available instead
❖ J2V8 exposes this API in Java
❖ Integrated with the Stetho tool & Chrome Dev Tools
Debug Support Demo
Node.js
❖ JavaScript Virtual Machine (V8)
❖ Modules
❖ Native
❖ JavaScript
❖ Event Loop
Node.js® is a JavaScript runtime built on Chrome's V8
JavaScript engine. Node.js uses an event-driven,
non-blocking I/O model that makes it
lightweight and efficient.
Bridging to Node.js
❖ Out of process Node & REST Services
❖ Vert.x
❖ Node engine on Nashorn / Rhino?
Node4J
❖ Dynamically link Node.js to the JVM
❖ Access Node.js context via JNI
❖ Execute Node.js modules (require)
❖ Callbacks to Java
❖ Process Node.js message queue
Node4J Demo
public static void main(final String[] args) throws Exception {
final V8 v8 = V8.createV8Runtime("global");
v8.registerJavaMethod(…);
NodeJS node = V8.createNodeJS(v8);
V8Object exports = node.requireScript(nodeCode, "http");
exports.release();
boolean running = true;
while (running) {
running = node.pumpMessageLoop();
}
}
Performance Considerations
❖ Minimize callbacks from JavaScript to Java
❖ ~4000 Per Second on my MBP
❖ Use bulk array copy to move primitives from JS to Java
❖ 60fps in our animation demo
Resources
❖ Getting started with J2V8
❖ Registering Java Callbacks with J2V8
❖ Implementing WebWorkers with J2V8
❖ Multithreaded JavaScript with J2V8
❖ Using J2V8 with Heroku
❖ All linked from our GitHub Page
Future Work
❖ Advanced exception handling between Java and JS
❖ Improved debug support
❖ Typed array access in Java
❖ You tell me?
Using J2V8
❖ J2V8 is available in Maven Central
❖ Currently 5 variants are available:
com.eclipsesource.j2v8.j2v8_win32_x86:3.1.6

com.eclipsesource.j2v8.j2v8_macosx_x86_64:3.1.6

com.eclipsesource.j2v8.j2v8:3.1.6 (aar)

com.eclipsesource.j2v8.j2v8_android_armv7l:3.1.6

com.eclipsesource.j2v8.j2v8_android_x86:3.1.6
❖ j2v8:3.1.6 (aar) contains both x86 and armv7l
4.0!
Thank-you
❖ Open Source Java bindings for V8
❖ Node4J extensions bring Node.js to Java
❖ Licensed under the EPL
❖ For J2V8 news, follow me on Twitter @irbull
https://github.com/eclipsesource/j2v8
Node4J: Running Node.js in a JavaWorld

More Related Content

What's hot

M. de kanavel
M. de kanavelM. de kanavel
M. de kanavelXime Diaz
 
Bases fundamentales de la biomecánica
Bases fundamentales de la biomecánicaBases fundamentales de la biomecánica
Bases fundamentales de la biomecánicaRox Ortiz
 
Síndrome Del Tunel Carpiano
Síndrome Del Tunel CarpianoSíndrome Del Tunel Carpiano
Síndrome Del Tunel CarpianoPowerosa Haku
 
Lesiones de nervios perifericos
Lesiones de nervios perifericosLesiones de nervios perifericos
Lesiones de nervios perifericosMedicina Unerg
 
2011 método mckenzie diagnóstico y terapia mecánica de la columna vertebral y...
2011 método mckenzie diagnóstico y terapia mecánica de la columna vertebral y...2011 método mckenzie diagnóstico y terapia mecánica de la columna vertebral y...
2011 método mckenzie diagnóstico y terapia mecánica de la columna vertebral y...Marcelo Ortiz Valenzuela
 
Bases fisiologicas de la terapia manual y osteopatia marcel bienfait
Bases fisiologicas de la terapia manual y osteopatia   marcel bienfaitBases fisiologicas de la terapia manual y osteopatia   marcel bienfait
Bases fisiologicas de la terapia manual y osteopatia marcel bienfaitAdlem Dutra
 
Artroplastia de disco lumbar
Artroplastia de disco lumbarArtroplastia de disco lumbar
Artroplastia de disco lumbarKaren Fumero
 
GUÍA DEL USO DE AGENTES FÍSICOS EN LA REHABILITACIÓN MAGNETOTERAPIA, BAÑOS DE...
GUÍA DEL USO DE AGENTES FÍSICOS EN LA REHABILITACIÓN MAGNETOTERAPIA, BAÑOS DE...GUÍA DEL USO DE AGENTES FÍSICOS EN LA REHABILITACIÓN MAGNETOTERAPIA, BAÑOS DE...
GUÍA DEL USO DE AGENTES FÍSICOS EN LA REHABILITACIÓN MAGNETOTERAPIA, BAÑOS DE...MZ_ ANV11L
 
Guia del tratamiento_integral_de_la_espasticidad
Guia del tratamiento_integral_de_la_espasticidadGuia del tratamiento_integral_de_la_espasticidad
Guia del tratamiento_integral_de_la_espasticidadNorma Obaid
 
Fisioterapia en patología meniscal
Fisioterapia en patología meniscalFisioterapia en patología meniscal
Fisioterapia en patología meniscalJose Herrera
 
Electroterapia 120709231510-phpapp02
Electroterapia 120709231510-phpapp02Electroterapia 120709231510-phpapp02
Electroterapia 120709231510-phpapp02Mario Trigo
 

What's hot (20)

M. de kanavel
M. de kanavelM. de kanavel
M. de kanavel
 
Artritis Reumatoidea. Tratamientos
Artritis Reumatoidea. TratamientosArtritis Reumatoidea. Tratamientos
Artritis Reumatoidea. Tratamientos
 
COLUMNA DORSAL.pptx
COLUMNA DORSAL.pptxCOLUMNA DORSAL.pptx
COLUMNA DORSAL.pptx
 
Bases fundamentales de la biomecánica
Bases fundamentales de la biomecánicaBases fundamentales de la biomecánica
Bases fundamentales de la biomecánica
 
Trauma de mano
Trauma de manoTrauma de mano
Trauma de mano
 
Epicondilitis
Epicondilitis Epicondilitis
Epicondilitis
 
Magnetoterapia
MagnetoterapiaMagnetoterapia
Magnetoterapia
 
Tens irving
Tens irvingTens irving
Tens irving
 
Síndrome Del Tunel Carpiano
Síndrome Del Tunel CarpianoSíndrome Del Tunel Carpiano
Síndrome Del Tunel Carpiano
 
Ejercicio
EjercicioEjercicio
Ejercicio
 
Lesiones de nervios perifericos
Lesiones de nervios perifericosLesiones de nervios perifericos
Lesiones de nervios perifericos
 
2011 método mckenzie diagnóstico y terapia mecánica de la columna vertebral y...
2011 método mckenzie diagnóstico y terapia mecánica de la columna vertebral y...2011 método mckenzie diagnóstico y terapia mecánica de la columna vertebral y...
2011 método mckenzie diagnóstico y terapia mecánica de la columna vertebral y...
 
Bases fisiologicas de la terapia manual y osteopatia marcel bienfait
Bases fisiologicas de la terapia manual y osteopatia   marcel bienfaitBases fisiologicas de la terapia manual y osteopatia   marcel bienfait
Bases fisiologicas de la terapia manual y osteopatia marcel bienfait
 
Artroplastia de disco lumbar
Artroplastia de disco lumbarArtroplastia de disco lumbar
Artroplastia de disco lumbar
 
Manejo del dolor
Manejo del dolorManejo del dolor
Manejo del dolor
 
GUÍA DEL USO DE AGENTES FÍSICOS EN LA REHABILITACIÓN MAGNETOTERAPIA, BAÑOS DE...
GUÍA DEL USO DE AGENTES FÍSICOS EN LA REHABILITACIÓN MAGNETOTERAPIA, BAÑOS DE...GUÍA DEL USO DE AGENTES FÍSICOS EN LA REHABILITACIÓN MAGNETOTERAPIA, BAÑOS DE...
GUÍA DEL USO DE AGENTES FÍSICOS EN LA REHABILITACIÓN MAGNETOTERAPIA, BAÑOS DE...
 
Lesión ciático popliteo externo()
Lesión ciático popliteo externo()Lesión ciático popliteo externo()
Lesión ciático popliteo externo()
 
Guia del tratamiento_integral_de_la_espasticidad
Guia del tratamiento_integral_de_la_espasticidadGuia del tratamiento_integral_de_la_espasticidad
Guia del tratamiento_integral_de_la_espasticidad
 
Fisioterapia en patología meniscal
Fisioterapia en patología meniscalFisioterapia en patología meniscal
Fisioterapia en patología meniscal
 
Electroterapia 120709231510-phpapp02
Electroterapia 120709231510-phpapp02Electroterapia 120709231510-phpapp02
Electroterapia 120709231510-phpapp02
 

Similar to Node4J: Running Node.js in a JavaWorld

Running JavaScript Efficiently in a Java World
Running JavaScript Efficiently in a Java WorldRunning JavaScript Efficiently in a Java World
Running JavaScript Efficiently in a Java Worldirbull
 
JavaOne 2014 - Scalable JavaScript Applications with Project Nashorn [CON6423]
JavaOne 2014 - Scalable JavaScript Applications with Project Nashorn [CON6423]JavaOne 2014 - Scalable JavaScript Applications with Project Nashorn [CON6423]
JavaOne 2014 - Scalable JavaScript Applications with Project Nashorn [CON6423]Leonardo Zanivan
 
Developing realtime apps with Drupal and NodeJS
Developing realtime apps with Drupal and NodeJS Developing realtime apps with Drupal and NodeJS
Developing realtime apps with Drupal and NodeJS drupalcampest
 
Groovy & Grails eXchange 2012 vert.x presentation
Groovy & Grails eXchange 2012 vert.x presentationGroovy & Grails eXchange 2012 vert.x presentation
Groovy & Grails eXchange 2012 vert.x presentationStuart (Pid) Williams
 
Java Fx Ajaxworld Rags V1
Java Fx Ajaxworld Rags V1Java Fx Ajaxworld Rags V1
Java Fx Ajaxworld Rags V1rajivmordani
 
Why Nodejs Guilin Shanghai
Why Nodejs Guilin ShanghaiWhy Nodejs Guilin Shanghai
Why Nodejs Guilin ShanghaiJackson Tian
 
Why Node.js
Why Node.jsWhy Node.js
Why Node.jsguileen
 
Event-driven IO server-side JavaScript environment based on V8 Engine
Event-driven IO server-side JavaScript environment based on V8 EngineEvent-driven IO server-side JavaScript environment based on V8 Engine
Event-driven IO server-side JavaScript environment based on V8 EngineRicardo Silva
 
[2014.11.18] java script execution environment survey
[2014.11.18] java script execution environment survey[2014.11.18] java script execution environment survey
[2014.11.18] java script execution environment surveyDongGyun Han
 
Integrating React.js Into a PHP Application: Dutch PHP 2019
Integrating React.js Into a PHP Application: Dutch PHP 2019Integrating React.js Into a PHP Application: Dutch PHP 2019
Integrating React.js Into a PHP Application: Dutch PHP 2019Andrew Rota
 
Java EE Introduction
Java EE IntroductionJava EE Introduction
Java EE Introductionejlp12
 
What is Node JS ?
What is Node JS ?What is Node JS ?
What is Node JS ?Balajihope
 
Modern Cloud-Native Jakarta EE Frameworks: tips, challenges, and trends.
Modern Cloud-Native Jakarta EE Frameworks: tips, challenges, and trends.Modern Cloud-Native Jakarta EE Frameworks: tips, challenges, and trends.
Modern Cloud-Native Jakarta EE Frameworks: tips, challenges, and trends.Otávio Santana
 
Introduction to Node.js Platform
Introduction to Node.js PlatformIntroduction to Node.js Platform
Introduction to Node.js PlatformNaresh Chintalcheru
 
Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma
Java 8: Nashorn & avatar.js di Enrico Risa al JUG RomaJava 8: Nashorn & avatar.js di Enrico Risa al JUG Roma
Java 8: Nashorn & avatar.js di Enrico Risa al JUG RomaVitalij Zadneprovskij
 

Similar to Node4J: Running Node.js in a JavaWorld (20)

Running JavaScript Efficiently in a Java World
Running JavaScript Efficiently in a Java WorldRunning JavaScript Efficiently in a Java World
Running JavaScript Efficiently in a Java World
 
JavaOne 2014 - Scalable JavaScript Applications with Project Nashorn [CON6423]
JavaOne 2014 - Scalable JavaScript Applications with Project Nashorn [CON6423]JavaOne 2014 - Scalable JavaScript Applications with Project Nashorn [CON6423]
JavaOne 2014 - Scalable JavaScript Applications with Project Nashorn [CON6423]
 
Developing realtime apps with Drupal and NodeJS
Developing realtime apps with Drupal and NodeJS Developing realtime apps with Drupal and NodeJS
Developing realtime apps with Drupal and NodeJS
 
Groovy & Grails eXchange 2012 vert.x presentation
Groovy & Grails eXchange 2012 vert.x presentationGroovy & Grails eXchange 2012 vert.x presentation
Groovy & Grails eXchange 2012 vert.x presentation
 
Java Fx Ajaxworld Rags V1
Java Fx Ajaxworld Rags V1Java Fx Ajaxworld Rags V1
Java Fx Ajaxworld Rags V1
 
What is Java? Presentation On Introduction To Core Java By PSK Technologies
What is Java? Presentation On Introduction To Core Java By PSK TechnologiesWhat is Java? Presentation On Introduction To Core Java By PSK Technologies
What is Java? Presentation On Introduction To Core Java By PSK Technologies
 
Why Nodejs Guilin Shanghai
Why Nodejs Guilin ShanghaiWhy Nodejs Guilin Shanghai
Why Nodejs Guilin Shanghai
 
Why Node.js
Why Node.jsWhy Node.js
Why Node.js
 
Event-driven IO server-side JavaScript environment based on V8 Engine
Event-driven IO server-side JavaScript environment based on V8 EngineEvent-driven IO server-side JavaScript environment based on V8 Engine
Event-driven IO server-side JavaScript environment based on V8 Engine
 
[2014.11.18] java script execution environment survey
[2014.11.18] java script execution environment survey[2014.11.18] java script execution environment survey
[2014.11.18] java script execution environment survey
 
Integrating React.js Into a PHP Application: Dutch PHP 2019
Integrating React.js Into a PHP Application: Dutch PHP 2019Integrating React.js Into a PHP Application: Dutch PHP 2019
Integrating React.js Into a PHP Application: Dutch PHP 2019
 
Java EE Introduction
Java EE IntroductionJava EE Introduction
Java EE Introduction
 
What is Node JS ?
What is Node JS ?What is Node JS ?
What is Node JS ?
 
Modern Cloud-Native Jakarta EE Frameworks: tips, challenges, and trends.
Modern Cloud-Native Jakarta EE Frameworks: tips, challenges, and trends.Modern Cloud-Native Jakarta EE Frameworks: tips, challenges, and trends.
Modern Cloud-Native Jakarta EE Frameworks: tips, challenges, and trends.
 
Nodejs Intro Part One
Nodejs Intro Part OneNodejs Intro Part One
Nodejs Intro Part One
 
Node J pdf.docx
Node J pdf.docxNode J pdf.docx
Node J pdf.docx
 
Node J pdf.docx
Node J pdf.docxNode J pdf.docx
Node J pdf.docx
 
AJppt.pptx
AJppt.pptxAJppt.pptx
AJppt.pptx
 
Introduction to Node.js Platform
Introduction to Node.js PlatformIntroduction to Node.js Platform
Introduction to Node.js Platform
 
Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma
Java 8: Nashorn & avatar.js di Enrico Risa al JUG RomaJava 8: Nashorn & avatar.js di Enrico Risa al JUG Roma
Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma
 

Recently uploaded

2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdfAndrey Devyatkin
 
Introduction to Firebase Workshop Slides
Introduction to Firebase Workshop SlidesIntroduction to Firebase Workshop Slides
Introduction to Firebase Workshop Slidesvaideheekore1
 
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptxThe Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptxRTS corp
 
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonLeveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonApplitools
 
Advantages of Cargo Cloud Solutions.pptx
Advantages of Cargo Cloud Solutions.pptxAdvantages of Cargo Cloud Solutions.pptx
Advantages of Cargo Cloud Solutions.pptxRTS corp
 
2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shards2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shardsChristopher Curtin
 
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...Bert Jan Schrijver
 
[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf
[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf
[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdfSteve Caron
 
eSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration toolseSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration toolsosttopstonverter
 
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...OnePlan Solutions
 
Strategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsStrategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsJean Silva
 
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...OnePlan Solutions
 
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingOpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingShane Coughlan
 
Effectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorEffectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorTier1 app
 
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfEnhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfRTS corp
 
Keeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldKeeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldRoberto Pérez Alcolea
 
Osi security architecture in network.pptx
Osi security architecture in network.pptxOsi security architecture in network.pptx
Osi security architecture in network.pptxVinzoCenzo
 
What’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 UpdatesWhat’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 UpdatesVictoriaMetrics
 
SAM Training Session - How to use EXCEL ?
SAM Training Session - How to use EXCEL ?SAM Training Session - How to use EXCEL ?
SAM Training Session - How to use EXCEL ?Alexandre Beguel
 
Best Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITBest Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITmanoharjgpsolutions
 

Recently uploaded (20)

2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
 
Introduction to Firebase Workshop Slides
Introduction to Firebase Workshop SlidesIntroduction to Firebase Workshop Slides
Introduction to Firebase Workshop Slides
 
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptxThe Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
 
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonLeveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
 
Advantages of Cargo Cloud Solutions.pptx
Advantages of Cargo Cloud Solutions.pptxAdvantages of Cargo Cloud Solutions.pptx
Advantages of Cargo Cloud Solutions.pptx
 
2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shards2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shards
 
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
 
[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf
[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf
[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf
 
eSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration toolseSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration tools
 
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
 
Strategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsStrategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero results
 
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
 
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingOpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
 
Effectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorEffectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryError
 
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfEnhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
 
Keeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldKeeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository world
 
Osi security architecture in network.pptx
Osi security architecture in network.pptxOsi security architecture in network.pptx
Osi security architecture in network.pptx
 
What’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 UpdatesWhat’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 Updates
 
SAM Training Session - How to use EXCEL ?
SAM Training Session - How to use EXCEL ?SAM Training Session - How to use EXCEL ?
SAM Training Session - How to use EXCEL ?
 
Best Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITBest Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh IT
 

Node4J: Running Node.js in a JavaWorld

  • 1. Node4J Running Node.js in a Java World Dr. R. Ian Bull EclipseSource @irbull
  • 2. Java and JavaScript ❖ Java a successful server side language ❖ JavaScript is a client side language ❖ SWT brought performant Java UIs to the desktop ❖ Node.js brought JavaScript to the server ❖ Java and JavaScript are two of the most popular programming languages
  • 3. Polyglot Systems ❖ Single language systems are rarely an option ❖ Legacy code ❖ New frameworks and technologies ❖ Evolving enterprises ❖ JEE will be here for another 20, 30, 50 (?) years
  • 4. Bridging Java and JavaScript ❖ Three common Java technologies enable JS embedding ❖ Rhino ❖ Available since JDK 6 ❖ Nashorn ❖ Replacing Rhino since JDK 8 ❖ More performant ❖ V8 as a separate process, String based messages
  • 5. Performance ❖ 30 Runs of the Esprima parser and tokenizer ❖ Nashorn compiles to bytecode ❖ V8 compiles to native assembly ❖ Best choice for raw JavaScript execution
  • 6.
  • 7. J2V8 ❖ A set of bindings that bring V8 to Java ❖ Inspired by SWT ❖ Create a thin JNI layer ❖ Expose (some) V8 API in Java ❖ Complicated logic lives in Java
  • 8. J2V8 Goals ❖ Efficient JavaScript on Android ❖ Make JavaScript shine in an enterprise Java World ❖ Standard Java APIs ❖ Efficient Java / JavaScript bindings
  • 9. J2V8 — History ❖ 1.0 Released in November 2014 ❖ 2.0 Released in February 2015 ❖ First presented at EclipseCon 2015 ❖ 3.0 Released at EnterJS — Summer 2015
  • 10. J2V8 Design ❖ Each V8 Object can be referenced using a Handle ❖ Each Object is stored in a V8 Persistent Object Store ❖ Objects must be explicitly freed ❖ Primitives where possible (no wrappers) ❖ Single Thread per isolate
  • 11. Two-way binding ❖ JS functions and scripts can be invoked from Java ❖ Java methods can be called from JavaScript ❖ Data can be passed back and forth using V8Objects
  • 12. J2V8 In Action — Tabris.js ❖ Mobile framework ❖ Apps written in JavaScript ❖ Native iOS and Android Apps ❖ Bindings to native UI components
  • 14. Example public String someJavaMethod(final String firstName, final String lastName) { return firstName + ", " + lastName; } public void start() { V8 v8 = V8.createV8Runtime(); v8.registerJavaMethod(this, "someJavaMethod", "someJavaMethod", new Class[] { String.class, String.class }); v8.executeScript("var result = someJavaMethod('Ian', ‘Bull');"); String result = v8.getString("result"); System.out.println(result); }
  • 15. J2V8 —What’s New ❖ Typed Arrays ❖ Threads & Workers ❖ ES 6 ❖ ChromeDev Tools ❖ NodeJS Support
  • 16. Typed Arrays V8Array result = (V8Array) v8.executeScript("" + "var buf = new ArrayBuffer(100);" + "var ints = new Int32Array(buf); " + "for(var i = 0; i < 25; i++) {" + " ints[i] = i;" + "}; " + “ints"); int[] ints = result.getIntegers(0, 25); ❖ Native support for JS Typed Arrays ❖ Access the values efficiently from Java
  • 17. Threads ❖ Every thread can have it’s own Isolate (Isolated V8 Instance) ❖ V8Thread is a Java Thread with an associated Isolate ❖ Provide an easy way to execute JavaScript Thread t = new V8Thread(new V8Runnable() { public void run(V8 v8) { int result = v8.executeIntegerScript("1+2"); } }); t.start();
  • 18. Executors ❖ Long running V8Thread with a message queue and event loop ❖ Threads can communicate via message passing ❖ Useful for implementing Web Workers / Service Workers
  • 19. ES 6 ❖ Snapshot builds of J2V8 support V8 4.10 & ES 6 ❖ Arrows ❖ Classes ❖ Let / Const ❖ Interators + For..Of ❖ Generators ❖ …
  • 20. Debug Support ❖ V8 (and now J2V8) no longer supports the Debug Agent ❖ JavaScript based Debug API is available instead ❖ J2V8 exposes this API in Java ❖ Integrated with the Stetho tool & Chrome Dev Tools
  • 22. Node.js ❖ JavaScript Virtual Machine (V8) ❖ Modules ❖ Native ❖ JavaScript ❖ Event Loop Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.
  • 23. Bridging to Node.js ❖ Out of process Node & REST Services ❖ Vert.x ❖ Node engine on Nashorn / Rhino?
  • 24. Node4J ❖ Dynamically link Node.js to the JVM ❖ Access Node.js context via JNI ❖ Execute Node.js modules (require) ❖ Callbacks to Java ❖ Process Node.js message queue
  • 25. Node4J Demo public static void main(final String[] args) throws Exception { final V8 v8 = V8.createV8Runtime("global"); v8.registerJavaMethod(…); NodeJS node = V8.createNodeJS(v8); V8Object exports = node.requireScript(nodeCode, "http"); exports.release(); boolean running = true; while (running) { running = node.pumpMessageLoop(); } }
  • 26. Performance Considerations ❖ Minimize callbacks from JavaScript to Java ❖ ~4000 Per Second on my MBP ❖ Use bulk array copy to move primitives from JS to Java ❖ 60fps in our animation demo
  • 27. Resources ❖ Getting started with J2V8 ❖ Registering Java Callbacks with J2V8 ❖ Implementing WebWorkers with J2V8 ❖ Multithreaded JavaScript with J2V8 ❖ Using J2V8 with Heroku ❖ All linked from our GitHub Page
  • 28. Future Work ❖ Advanced exception handling between Java and JS ❖ Improved debug support ❖ Typed array access in Java ❖ You tell me?
  • 29. Using J2V8 ❖ J2V8 is available in Maven Central ❖ Currently 5 variants are available: com.eclipsesource.j2v8.j2v8_win32_x86:3.1.6
 com.eclipsesource.j2v8.j2v8_macosx_x86_64:3.1.6
 com.eclipsesource.j2v8.j2v8:3.1.6 (aar)
 com.eclipsesource.j2v8.j2v8_android_armv7l:3.1.6
 com.eclipsesource.j2v8.j2v8_android_x86:3.1.6 ❖ j2v8:3.1.6 (aar) contains both x86 and armv7l
  • 30. 4.0!
  • 31. Thank-you ❖ Open Source Java bindings for V8 ❖ Node4J extensions bring Node.js to Java ❖ Licensed under the EPL ❖ For J2V8 news, follow me on Twitter @irbull https://github.com/eclipsesource/j2v8