Nashorn & Avatar.js
JAVA 8
Agenda
• Nashorn Javascript Engine
• Node.js
• Avatar.js
• Project Avatar
Rhino
• Slow compared to other Javascript Engines
(V8,Spidermonkey)
• Legacy Code (since 1998)
• Interpreted Code
• Non-compliant with ECMAScript (not 100%)
What is Nashorn?
• Started in 2011
• Embedded Javascript Engine
• Replacement of Rhino
• Open Source on the OpenJDK repository
• Released with JAVA 8
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
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
Let’s jump to code
Simple Hello Word
Inject variable in the context
Javascript from Java
Java object
• Java.type
• Java.extend
• Java.from
• Java.to
• Java.super
Java.type
Given 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.
Java.extend
Java.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.
Java.from
Converts Java array/Collection to Javascript array

(Shallow copy)
Java.to
Converts javascript objects into java type
Java.super
Used to invoke superclass methods
Java from Javascript
map.js
Implementing Java Interface
NashornInterface.java
hello.js
Shell scripting
IDE Support
• IntelliJ IDEA 13.1 (Community and Ultimate Editions)
• Debugger
• Refactoring
Let’s talk about performance
http://ariya.ofilabs.com/2014/03/nashorn-the-new-rhino-
on-the-block.html
esprima.org
What is Avatar.js ?
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”.
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
Node.js Programming Model
• Single Threaded
• Event Loop

-Callback

-Non-blocking I/O
Node.js Event Loop
Avatar.js Features
• Run node applications on the JVM
• Call any Java API from JavaScript
• Support multiple event loops
• Support multiple Java threads
Avatar.js Node.js Modules
• async
• coffee-script
• connect
• express
• grunt
• mongodb
• mongoose
• mustache
• q
• redis
• socket.io
• underscore
…more
That means
Will run in Node.js as well as in JVM
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).
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
Avatar Architecture
Avatar Services
• REST
• WebSocket
• Push (SSE)
REST
GET - PUT - POST - DELETE
WebSocket
Server Side Events (SSE)
Javascript Model-Store
• Javascript Api

-Javascript ORM (1:1, 1:N, M:N)

-Support relational and non-relational databases
Javascript Model-store
• Bind the model to the store
• Create a new Todo
Avatar Client (Optional)
• View

- Declarative UI

- Bundle widget (JQuery-[ui/mobile])
• Model

- WS,REST,SSE,local

- Autowired with Javascript Services
Avatar client example
Server
Client
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
Thank you
Questions ?

Java 8: Nashorn & avatar.js di Enrico Risa al JUG Roma