42
Nashorn & Avatar.js JAVA 8

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

Embed Size (px)

DESCRIPTION

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

Citation preview

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

Nashorn & Avatar.jsJAVA 8

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

Agenda

• Nashorn Javascript Engine

• Node.js

• Avatar.js

• Project Avatar

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

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

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

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

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

Let’s jump to code

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

Simple Hello Word

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

Inject variable in the context

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

Javascript from Java

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

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

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

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

Java.fromConverts Java array/Collection to Javascript array

(Shallow copy)

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

Java.toConverts javascript objects into java type

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

Java.superUsed to invoke superclass methods

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

Java from Javascriptmap.js

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

Implementing Java Interface

NashornInterface.java

hello.js

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

Shell scripting

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

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

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

What is Avatar.js ?

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

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

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

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

Node.js Event Loop

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

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

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

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

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

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

Avatar Architecture

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

Avatar Services

• REST

• WebSocket

• Push (SSE)

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

REST

GET - PUT - POST - DELETE

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

WebSocket

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

Server Side Events (SSE)

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

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

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

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

Avatar client exampleServer

Client

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

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

Thank you

Questions ?