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-co...
What is Nashorn?
• Started in 2011
• Embedded Javascript Engine
• Replacement of Rhino
• Open Source on the OpenJDK reposi...
Possible use cases
• Shell Scripting

-jjs
• Desktop app Javascript

- Java Fx
• Server Side Javascript

- More later
• En...
Nashorn Features
• ECMAScript-262 Edition 5.1 compliant
• JSR-292 “InvokeDynamic” (The Da Vinci Machine)
• Integrated with...
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 opera...
Java.extend
Java.extend function returns a type object for a subclass
of the specified Java class (or implementation of the...
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 mod...
Node.js
• Server side Javascript
• Based on Chrome V8 Engine
• Fast and Scalable
• Event driven, non-blocking I/O model
• ...
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
•...
Avatar.js Node.js Modules
• async
• coffee-script
• connect
• express
• grunt
• mongodb
• mongoose
• mustache
• q
• redis
...
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-
Se...
Project Avatar
• Server Side JavaScript Services Framework
• Focused on REST, WebSocket, Server Sent Event (SSE)
endpoints...
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

- Au...
Avatar client example
Server
Client
Additional Resources
• http://openjdk.java.net/projects/nashorn/
• https://java.net/projects/adoptopenjdk
• http://www.inf...
Thank you
Questions ?
Upcoming SlideShare
Loading in …5
×

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

2,797 views

Published on

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

Published in: Internet, Technology, Education
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,797
On SlideShare
0
From Embeds
0
Number of Embeds
340
Actions
Shares
0
Downloads
33
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

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

  1. 1. Nashorn & Avatar.js JAVA 8
  2. 2. Agenda • Nashorn Javascript Engine • Node.js • Avatar.js • Project Avatar
  3. 3. Rhino • Slow compared to other Javascript Engines (V8,Spidermonkey) • Legacy Code (since 1998) • Interpreted Code • Non-compliant with ECMAScript (not 100%)
  4. 4. What is Nashorn? • Started in 2011 • Embedded Javascript Engine • Replacement of Rhino • Open Source on the OpenJDK repository • Released with JAVA 8
  5. 5. 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
  6. 6. 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
  7. 7. Let’s jump to code
  8. 8. Simple Hello Word
  9. 9. Inject variable in the context
  10. 10. Javascript from Java
  11. 11. Java object • Java.type • Java.extend • Java.from • Java.to • Java.super
  12. 12. 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.
  13. 13. 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.
  14. 14. Java.from Converts Java array/Collection to Javascript array
 (Shallow copy)
  15. 15. Java.to Converts javascript objects into java type
  16. 16. Java.super Used to invoke superclass methods
  17. 17. Java from Javascript map.js
  18. 18. Implementing Java Interface NashornInterface.java hello.js
  19. 19. Shell scripting
  20. 20. IDE Support • IntelliJ IDEA 13.1 (Community and Ultimate Editions) • Debugger • Refactoring
  21. 21. Let’s talk about performance http://ariya.ofilabs.com/2014/03/nashorn-the-new-rhino- on-the-block.html esprima.org
  22. 22. What is Avatar.js ?
  23. 23. 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”.
  24. 24. 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
  25. 25. Node.js Programming Model • Single Threaded • Event Loop
 -Callback
 -Non-blocking I/O
  26. 26. Node.js Event Loop
  27. 27. Avatar.js Features • Run node applications on the JVM • Call any Java API from JavaScript • Support multiple event loops • Support multiple Java threads
  28. 28. Avatar.js Node.js Modules • async • coffee-script • connect • express • grunt • mongodb • mongoose • mustache • q • redis • socket.io • underscore …more
  29. 29. That means Will run in Node.js as well as in JVM
  30. 30. 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).
  31. 31. 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
  32. 32. Avatar Architecture
  33. 33. Avatar Services • REST • WebSocket • Push (SSE)
  34. 34. REST GET - PUT - POST - DELETE
  35. 35. WebSocket
  36. 36. Server Side Events (SSE)
  37. 37. Javascript Model-Store • Javascript Api
 -Javascript ORM (1:1, 1:N, M:N)
 -Support relational and non-relational databases
  38. 38. Javascript Model-store • Bind the model to the store • Create a new Todo
  39. 39. Avatar Client (Optional) • View
 - Declarative UI
 - Bundle widget (JQuery-[ui/mobile]) • Model
 - WS,REST,SSE,local
 - Autowired with Javascript Services
  40. 40. Avatar client example Server Client
  41. 41. 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
  42. 42. Thank you Questions ?

×