Slide 1

Slide 1 text

Running distributed Node.js Apps Running distributed Node.js Apps in JVM environments in JVM environments Nodyn & Vert.x Nodyn & Vert.x Niko Köbler Independent Software-Architect, Developer & Trainer > > > niko@n-k.de www.n-k.de @dasniko

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

What is Node.js? What is Node.js? Asyncronous, non-blocking, evented I/O with JavaScript Server-side JavaScript platform built on Google V8 engine What JavaScript has done for the webbrowser, Node.js is doing for the backend server http://nodejs.org

Slide 4

Slide 4 text

The Event Loop The Event Loop

Slide 5

Slide 5 text

The cost of I/O The cost of I/O L1-cache 3 cycles L2-cache 14 cycles RAM 250 cycles Disk 41 000 000 cycles Network 240 000 000 cycles

Slide 6

Slide 6 text

Why JavaScript? Why JavaScript? “ (undefined is not a function!)

Slide 7

Slide 7 text

Why Java? Why Java?

Slide 8

Slide 8 text

With Java, we have... With Java, we have...

Slide 9

Slide 9 text

And now, you wanna tell us And now, you wanna tell us about this about this

Slide 10

Slide 10 text

“In four years ... Node.js has experienced phenomenal growth. Node.js is the language of choice for high performance, low latency applications and has been powering everything from robots to API engines to cloud stacks to mobile web sites. Node.js Advisory Board - October 23, 2014

Slide 11

Slide 11 text

Any application that Any application that can can be be written in JavaScript, written in JavaScript, will will eventually be written in eventually be written in JavaScript JavaScript Atwood's Law http://blog.codinghorror.com/the-principle-of-least-power/

Slide 12

Slide 12 text

Integration Integration

Slide 13

Slide 13 text

Integration Integration

Slide 14

Slide 14 text

Integration Integration

Slide 15

Slide 15 text

Nashorn Nashorn JavaScript Enginge on the JVM based on invokedynamic feature competes with Google V8 ECMAScript 5.1 compatible (ES6/ES2015 with Java 9) Seamless interoperability of Java and JavaScript Shell Scripting Language and API Extensions http://openjdk.java.net/projects/nashorn/​ 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...

Slide 16

Slide 16 text

Java and JavaScript Java and JavaScript “ ...are similar than car and carpet are similar.

Slide 17

Slide 17 text

Nashorn Nashorn 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 18

Slide 18 text

Nashorn Nashorn 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 19

Slide 19 text

by by project:odd project:odd @RedHat @RedHat http://nodyn.io

Slide 20

Slide 20 text

also based on invokedynamic REPL / command line Java interaction / embedding http://dynjs.org

Slide 21

Slide 21 text

“ Netty is an asynchronous event-driven network application framework for rapid development of maintainable high performance protocol servers & clients. http://netty.io Netty Netty

Slide 22

Slide 22 text

Node.js Architecture Node.js Architecture

Slide 23

Slide 23 text

Nodyn Architecture Nodyn Architecture “ more JavaScript than Node.js

Slide 24

Slide 24 text

“ Vert.x is a lightweight, high performance application platform for the JVM that's designed for modern mobile, web, and enterprise applications. Polyglott* Simplicity Scalability Concurrency Distributed Event Bus** http://vertx.io *) Java, JavaScript, Ruby, Groovy, Python, Scala, Clojure, Ceylon **) using Hazelcast In-Memory Data Grid ( ) hazelcast.org

Slide 25

Slide 25 text

Beer-as-a-Service Beer-as-a-Service https://github.com/dasniko/beer-as-a-service

Slide 26

Slide 26 text

var http = require("http"); var vertx = require("vertx2-core"); var registration = vertx.eventbus.register("bar", function(message) { var amount = message.body.amount; console.log("BAR: Someone ordered " + amount + " beer(s)."); message.reply({wait_time: amount * 1.75}); }); var server = http.createServer(function(request, response) { var parts = request.url.split("/"); var amount = parts[1]; vertx.eventbus.send("bar", {amount: amount}, function(message) { response.write(amount + " beer(s) will be ready in " + message.body.wait_time + " minutes"); response.end(); }); }); server.listen(9000, function() { console.log( "Beer-Server is listening on port 9000" ); }); beer-as-a-service.js run with nodyn

Slide 27

Slide 27 text

var http = require("http"); var vertx = require("vertx2-core"); var server = http.createServer(function(request, response) { var parts = request.url.split("/"); var amount = parts[1]; vertx.eventbus.send("bar", {amount: amount}, function(message) { response.write(amount + " beer(s) will be ready in " + message.body.wait_time + " minutes"); response.end(); }); }); server.listen(9000, function() { console.log( "Beer-Server is listening on port 9000" ); }); beer-web.js beer-bar.js var eventBus = require("vertx/event_bus"); eventBus.registerHandler("bar", function(message, replier) { java.lang.System.err.println("BAR: Someone ordered " + message.amount + " beer(s)"); replier({wait_time: message.amount * 1.75}); }); java.lang.System.err.println("The BAR is open!"); run with vertx in cluster mode

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

https://github.com/nodyn/nodyn

Slide 31

Slide 31 text

“ We are always mindful where we invest our resources and focus our efforts, and believe the Node.js Foundation has an important role to play in helping to make Node.js successful, as well as the developers using the technology to deploy Node.js applications. We look forward to working alongside our colleagues in the Foundation to help guide the project in the future. Rich Sharples, senior director, Product Management, Red Hat https://www.redhat.com/en/about/press-releases/red-hat-joins-nodejs-foundation

Slide 32

Slide 32 text

Conclusion Conclusion JavaScript is an emerging language and widely adopted Node.js is very popular Operations, monitoring and integration lacks JVM is Enterprise environment of choice (and all the above lacks are already solved) Run JavaScript on the JVM thanks to invokedynamic (Nashorn, DynJS) Re-use your infrastructures and libraries with Node.js Nodyn from Red Hat Re-use of Node API module, integration of Netty and Vert.x Process-bindings in Java/JavaScript Embed Node.js apps into your Java applications Run Node.js apps in distributed environments

Slide 33

Slide 33 text

Thank you! Thank you! Questions? Questions? Slides: http://slides.com/dasniko/nodyn-vertx Niko Köbler Independent Software-Architect, Developer & Trainer > > > niko@n-k.de www.n-k.de @dasniko