![Page 1: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/1.jpg)
Nashorn & Avatar.jsJAVA 8
![Page 2: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/2.jpg)
Agenda
• Nashorn Javascript Engine
• Node.js
• Avatar.js
• Project Avatar
![Page 3: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/3.jpg)
Rhino
• Slow compared to other Javascript Engines (V8,Spidermonkey)
• Legacy Code (since 1998)
• Interpreted Code
• Non-compliant with ECMAScript (not 100%)
![Page 4: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/4.jpg)
What is Nashorn?
• Started in 2011
• Embedded Javascript Engine
• Replacement of Rhino
• Open Source on the OpenJDK repository
• Released with JAVA 8
![Page 5: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/5.jpg)
Possible use cases• Shell Scripting
-jjs
• Desktop app Javascript - Java Fx
• Server Side Javascript - More later
• End-User Javascript- Rule engines - Etl- Reporting Tools..more
![Page 6: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/6.jpg)
Nashorn Features• ECMAScript-262 Edition 5.1 compliant
• JSR-292 “InvokeDynamic” (The Da Vinci Machine)
• Integrated with JSR-223 (Scripting for Java)
• Runtime Compiled to bytecode
• Small 1.5MB JAR
• Fast, 2x to 10x performance improvement
• Seamless Javascript/Java interoperability
![Page 7: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/7.jpg)
Let’s jump to code
![Page 8: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/8.jpg)
Simple Hello Word
![Page 9: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/9.jpg)
Inject variable in the context
![Page 10: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/10.jpg)
Javascript from Java
![Page 11: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/11.jpg)
Java object
• Java.type
• Java.extend
• Java.from
• Java.to
• Java.super
![Page 12: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/12.jpg)
Java.typeGiven a name of a Java type, returns an object representing that type in Nashorn. You can use with the new operator to create new instances of the class as well as to access static members of the class.
![Page 13: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/13.jpg)
Java.extendJava.extend function returns a type object for a subclass of the specified Java class (or implementation of the specified interface) that acts as a script-to-Java adapter for it.
![Page 14: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/14.jpg)
Java.fromConverts Java array/Collection to Javascript array
(Shallow copy)
![Page 15: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/15.jpg)
Java.toConverts javascript objects into java type
![Page 16: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/16.jpg)
Java.superUsed to invoke superclass methods
![Page 17: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/17.jpg)
Java from Javascriptmap.js
![Page 18: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/18.jpg)
Implementing Java Interface
NashornInterface.java
hello.js
![Page 19: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/19.jpg)
Shell scripting
![Page 20: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/20.jpg)
IDE Support
• IntelliJ IDEA 13.1 (Community and Ultimate Editions)
• Debugger
• Refactoring
![Page 21: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/21.jpg)
Let’s talk about performance
http://ariya.ofilabs.com/2014/03/nashorn-the-new-rhino-on-the-block.html
esprima.org
![Page 22: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/22.jpg)
What is Avatar.js ?
![Page 23: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/23.jpg)
Avatar.js
• An open source project
• A server side Javascript framework for the JVM
• A mix of- Node.js event driven model - Nashorn Javascript engine - Java platform APIs
• Brings “the Node programming model, APIs and module ecosystem to the Java platform”.
![Page 24: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/24.jpg)
Node.js
• Server side Javascript
• Based on Chrome V8 Engine
• Fast and Scalable
• Event driven, non-blocking I/O model
• Large community and adoption -NPM ~70000 packages
![Page 25: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/25.jpg)
Node.js Programming Model• Single Threaded
• Event Loop-Callback-Non-blocking I/O
![Page 26: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/26.jpg)
Node.js Event Loop
![Page 27: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/27.jpg)
Avatar.js Features
• Run node applications on the JVM
• Call any Java API from JavaScript
• Support multiple event loops
• Support multiple Java threads
![Page 28: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/28.jpg)
Avatar.js Node.js Modules
• async
• coffee-script
• connect
• express
• grunt
• mongodb
• mongoose
• mustache
• q
• redis
• socket.io
• underscore
…more
![Page 29: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/29.jpg)
That means
Will run in Node.js as well as in JVM
![Page 30: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/30.jpg)
Project Avatar
Project Avatar provides a JavaScript services layer zeroed in on supporting REST, WebSockets and Server-Sent Events, and a rich client side framework that assumes very minor JavaScript knowledge. The services side is focused on building data services using JavaScript, while the optional client side is entirely focused on supporting HTML5 and TSA (Thin Server Architecture).
![Page 31: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/31.jpg)
Project Avatar• Server Side JavaScript Services Framework
• Focused on REST, WebSocket, Server Sent Event (SSE) endpoints
• Node.js event-driven programming model
• Layers on Avatar.js Node-compatible runtime
• Integrated EE features
• Multi-threading
• Javascript Domain Model
![Page 32: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/32.jpg)
Avatar Architecture
![Page 33: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/33.jpg)
Avatar Services
• REST
• WebSocket
• Push (SSE)
![Page 34: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/34.jpg)
REST
GET - PUT - POST - DELETE
![Page 35: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/35.jpg)
WebSocket
![Page 36: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/36.jpg)
Server Side Events (SSE)
![Page 37: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/37.jpg)
Javascript Model-Store• Javascript Api
-Javascript ORM (1:1, 1:N, M:N) -Support relational and non-relational databases
![Page 38: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/38.jpg)
Javascript Model-store• Bind the model to the store
• Create a new Todo
![Page 39: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/39.jpg)
Avatar Client (Optional)
• View- Declarative UI- Bundle widget (JQuery-[ui/mobile])
• Model- WS,REST,SSE,local - Autowired with Javascript Services
![Page 40: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/40.jpg)
Avatar client exampleServer
Client
![Page 41: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/41.jpg)
Additional Resources• http://openjdk.java.net/projects/nashorn/
• https://java.net/projects/adoptopenjdk
• http://www.infoq.com/articles/nashorn
• http://strongloop.com/strongblog/how-to-run-node-js-on-the-jvm-with-avatar-js-and-loopback/
• https://avatar.java.net/
• http://blog.eisele.net/2013/12/project-avatar-whats-in-it-for-java-ee-8.html
![Page 42: Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma](https://reader036.vdocuments.us/reader036/viewer/2022062319/554a1d90b4c9058c5d8b557c/html5/thumbnails/42.jpg)
Thank you
Questions ?