Slide 1

Slide 1 text

AVATAR 2.0 AVATAR 2.0 MORE THAN JUST NODE.JS MORE THAN JUST NODE.JS ON THE JAVA VIRTUAL MACHINE ON THE JAVA VIRTUAL MACHINE Niko Köbler ( ) @dasniko {JavaScript}Training

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

I'M TO HIRE! I'M TO HIRE!

Slide 4

Slide 4 text

WHY WHY JAVASCRIPT? JAVASCRIPT? (undefined is not a function!)

Slide 5

Slide 5 text

WITH JAVA, WE HAVE... WITH JAVA, WE HAVE... Longterm investments Mature solutions Complex and extensive business logic Integration of heterogeneous environments across platforms Blueprints and best-practices available Proven infrastructure

Slide 6

Slide 6 text

AND NOW, AND NOW, YOU TELL US ABOUT THIS YOU TELL US ABOUT THIS BRAVE NEW (NODE-)WORLD BRAVE NEW (NODE-)WORLD Mostly new applications Mostly No-SQL backed New application approaches More focused silo apps "Cool stuff" > 110.000 NPM libraries available No enterprise integration (yet) Infrastructure?

Slide 7

Slide 7 text

SPEAKING JAVASCRIPT SPEAKING JAVASCRIPT Like it or not, JavaScript is everywhere these days - from browser to server to mobile - and now you, too, need to learn the language or dive deeper than you have. Dr. Axel Rauschmayer http://speakingjs.com

Slide 8

Slide 8 text

THOUGHTWORKS THOUGHTWORKS I think JavaScript has been seen as a serious language for the last two or three years; I think now increasingly we’re seeing JavaScript as a platform. (Sam Newman, ThoughtWorks’ Global Innovation Lead) JavaScript has emerged both as a platform for server-side code but also a platform to host other languages. (January, 2014) http://www.techworld.com.au/article/536950/rise_rise_javascript

Slide 9

Slide 9 text

INTEGRATION? INTEGRATION?

Slide 10

Slide 10 text

INTEGRATION! INTEGRATION!

Slide 11

Slide 11 text

NASHORN NASHORN JavaScript Enginge on the JVM based on invokedynamic feature competes with Google V8 ECMAScript 5.1 compatible (ECMAScript 6 in future) Seamless interoperability of Java and JavaScript Language and API Extensions closures, collections & for each, multi-line string literals, string interpolation, __noSuchProperty__, __noSuchMethod__, typed arrays, binding properties, error extensions, conditional catch clause, String functions, and many, many more... https://blogs.oracle.com/nashorn/

Slide 12

Slide 12 text

JAVA & JAVASCRIPT JAVA & JAVASCRIPT ...are similar than car and carpet are similar.

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

NASHORN NASHORN COMMAND LINE CLIENT COMMAND LINE CLIENT $ $JAVA_HOME/bin/jjs jjs> print('Hello Nashorn!'); INVOKING JAVASCRIPT FROM JAVA INVOKING JAVASCRIPT FROM JAVA ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn"); engine.eval("print('Hello Nashorn!');"); engine.eval(new FileReader("scriptfile.js")); Invocable invocable = (Invocable) engine; Object result = invocable.invokeFunction("jsSayHello", "Nashorn");

Slide 16

Slide 16 text

NASHORN NASHORN INVOKING JAVA FROM JAVASCRIPT INVOKING JAVA FROM JAVASCRIPT package my.package; public class MyJavaClass { static String sayHello(String name) { return String.format("Hello %s from Java!", name); } } var MyJavaClass = Java.type('my.package.MyJavaClass'); var result = MyJavaClass.sayHello('Nashorn'); print(result); // Hello Nashorn from Java!

Slide 17

Slide 17 text

AVATAR-JS AVATAR-JS Node.js on the JVM ~95% Node.js API compatibility no Chrome V8 native APIs many of the node-modules work (e.g.: abbrev, ansi, async, block-stream, chmodr, chownr, coffee-script, colors, commander, connect, debug, engine.io, express, ftsream, glob, graceful-fs, inherits, ini, init-package-json, grunt, grunt-bower-task, jade, lodash, mime, mkdirp, mocha, moment, mongodb, mongoose, mustache, node-unit, node-uuid, once, opener, optimist, osenv, passport, q, read, redis, request, retry, rimraf, ronn, semver, slide, socket.io, tar, uglify-js, uid-number, underscore, which, winston) https://avatar-js.java.net

Slide 18

Slide 18 text

https://avatar-js.java.net

Slide 19

Slide 19 text

PROJECT AVATAR PROJECT AVATAR End-to-end fullstack framework Client-Library Services (REST, WS, SSE) JMS Running in a Java EE Application Server Glassfish 4 WebLogic 12.1.3

Slide 20

Slide 20 text

AVATAR-JS & AVATAR-JS & PROJECT AVATAR PROJECT AVATAR BECOME BECOME AVATAR 2.0 AVATAR 2.0

Slide 21

Slide 21 text

AVATAR 2.0 AVATAR 2.0 no more Java EE application server needed single, standalone JVM

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

THREADS & THREADS & INTER-THREAD INTER-THREAD COMMUNICATION COMMUNICATION

Slide 24

Slide 24 text

MESSAGE BUS MESSAGE BUS var avatar = require('org/glassfish/avatar'); var threads = require('org/glassfish/avatar/threads'); var app = avatar.application; var name = app.name; var bus = app.bus; // listen for messages on topic 'hello' bus.on('hello', function(body, msg) { print(name + ' got message: ' + JSON.stringify(body)); }); // publishing to 'hello' topic (e.g. in file hello.js): bus.publish('hello', { x : 'x', y : 'y' }); // start a background thread for publishing threads.newBackgroundThread('background', 'hello.js').start();

Slide 25

Slide 25 text

MESSAGE BUS METHODS MESSAGE BUS METHODS bus.publish(topic, body); bus.publishTo(address, topic, body); bus.send(topic, body); bus.sendTo(address, topic, body); bus.reply(replyTo, body); bus.subscribe(topic); bus.unsubscribe(topic);

Slide 26

Slide 26 text

SHARED STATE SHARED STATE Cache/Map API: Key-Value-Store (Coherence backed, Cache/JSR-107 compliant) var avatar = require('org/glassfish/avatar'); var state = avatar.application.state; state.put('key', {'value': 'myValue'}); state.keys(); // -> Array state.contains('key'); // -> boolean state.get('key'); // -> Object state.remove('key'); // -> void

Slide 27

Slide 27 text

AVATAR AVATAR PERSISTENCE PERSISTENCE MODEL-STORE-API MODEL-STORE-API

Slide 28

Slide 28 text

MODEL-STORE API MODEL-STORE API database setup var store = avatar.newStore(‘mysql’, { host: 'localhost', port: 3306, database: 'test', username: 'root', createDb: true }); JPA (Eclipselink) / JDBC based relational and non-relational user-transactions possible

Slide 29

Slide 29 text

MODEL-STORE API MODEL-STORE API model setup var Family = avatar.newModel('family', { "name" : { type : "string", primary : true }, "description" : "string" }); var Product = avatar.newModel('product', { "name" : { type : "string", primary : true }, "madeBy" : "string", "price" : "number", "quantity" : "integer" });

Slide 30

Slide 30 text

MODEL-STORE API MODEL-STORE API relations & binding Family.hasMany(Product, { as : 'products', foreign : 'family' }); store.bind(Family, Product);

Slide 31

Slide 31 text

MODEL-STORE API MODEL-STORE API usage during runtime store.connect(function() { Product.create({ name: 'Widget', price: 1.00, quantity: 2, }, function(err, w1) { console.log(JSON.stringify(w1)); store.disconnect(function() { // done }); }); });

Slide 32

Slide 32 text

R.I.P. R.I.P. UI Library REST Services (Web-)Socket Services Push Services (SSE) JMS --> NEW T3 THIN CLIENT --> NEW T3 THIN CLIENT FOR JMS & REMOTE EJB CALLS FOR JMS & REMOTE EJB CALLS

Slide 33

Slide 33 text

DEMO TIME DEMO TIME

Slide 34

Slide 34 text

COMPETITORS ? COMPETITORS ? Red Hat Nodyn ( ) Vert.x interaction/integration based on DynJS ( ) nodyn.io dynjs.org --> Java Magazin 03/2015

Slide 35

Slide 35 text

AVATAR 2.0 - CONCLUSION AVATAR 2.0 - CONCLUSION Lightweight Node.js integration in Java Enterprise context Run JavaScript Apps on a standard Java Infrastructure 95% Node.js API compatibility Multi-threaded, asynchronous, non-blocking API Run one event-loop per thread, multiple threads Inter-Thread-communication / shared state via event bus or map API Avatar Persistence (Model-Store-API) http://avatar.java.net

Slide 36

Slide 36 text

AVATAR 2.0 AVATAR 2.0 THANK YOU! THANK YOU! QUESTIONS? QUESTIONS? @dasniko