SlideShare une entreprise Scribd logo
1  sur  32
Télécharger pour lire hors ligne
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 Java World

Contenu connexe

Dernier

Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 

Dernier (20)

Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 

En vedette

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

En vedette (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Node4J: Running Node.js in Java World

  • 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