Node.js on the JVM Node.js on the JVM JavaScript Evented I/O & - - PowerPoint PPT Presentation

node js on the jvm node js on the jvm
SMART_READER_LITE
LIVE PREVIEW

Node.js on the JVM Node.js on the JVM JavaScript Evented I/O & - - PowerPoint PPT Presentation

Node.js on the JVM Node.js on the JVM JavaScript Evented I/O & more JavaScript Evented I/O & more for the Java Enterprise Ecosystem for the Java Enterprise Ecosystem Niko Kbler ( @dasniko ) {JavaScript} Training I'm to hire! I'm


slide-1
SLIDE 1

JavaScript Evented I/O & more JavaScript Evented I/O & more for the Java Enterprise Ecosystem for the Java Enterprise Ecosystem

Niko Köbler ( ) @dasniko {JavaScript}Training

Node.js on the JVM Node.js on the JVM

slide-2
SLIDE 2
slide-3
SLIDE 3

I'm to hire! I'm to hire!

slide-4
SLIDE 4
slide-5
SLIDE 5

What is Node.js? What is Node.js?

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

var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(1337, '127.0.0.1'); console.log('Server running at http://127.0.0.1:1337/');

slide-6
SLIDE 6

The Event Loop The Event Loop

slide-7
SLIDE 7

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-8
SLIDE 8

Why JavaScript? Why JavaScript?

“ (undefined is not a function!)

slide-9
SLIDE 9

Why Java? Why Java?

slide-10
SLIDE 10

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 and monitoring options

slide-11
SLIDE 11

And now, And now, you wanna tell us about this you wanna tell us about this "brave" "brave" new (Node-)world? new (Node-)world?

Mostly new applications Mostly No-SQL backed New application approaches More focused silo apps "Cool stuff" ~ 120.000 NPM libraries available (01/2015) No enterprise integration (yet) Infrastructure? Monitoring? Operations?

slide-12
SLIDE 12

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-13
SLIDE 13

“ 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-14
SLIDE 14
slide-15
SLIDE 15

Integration Integration

slide-16
SLIDE 16

Integration Integration

slide-17
SLIDE 17

dynamic dynamic invoke invoke

slide-18
SLIDE 18

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 With Java 8u40 native execution of TypeScript 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-19
SLIDE 19

Java and JavaScript Java and JavaScript

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

slide-20
SLIDE 20
slide-21
SLIDE 21
slide-22
SLIDE 22

Nashorn Nashorn

Command Line Client

$ $JAVA_HOME/bin/jjs jjs> print('Hello 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-23
SLIDE 23

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-24
SLIDE 24

Nashorn Nashorn

Listing Docker Containers via REST-Call

#!/usr/bin/jjs -fv var host = "DOCKER_HOST" var dockerUri="http://${host}:5555/containers/json"; var command = "curl ${dockerUri}"; $EXEC(command); var containers = JSON.parse($OUT); for each(container in containers){ print("${container.Image} / ${container.Names[0]} / ${container.Status}"); }

(by ) Adam Bien

slide-25
SLIDE 25

Avatar Avatar

The Oracle solution The Oracle solution

slide-26
SLIDE 26

Avatar 2.0 Avatar 2.0

Inter-thread communication Message Bus Shared State (Map API: Key-Value-Store) Model-Store-API JPA (Eclipselink) / JDBC based https://avatar.java.net

slide-27
SLIDE 27

Avatar 2.0 Avatar 2.0

slide-28
SLIDE 28

The RedHat counter The RedHat counter

slide-29
SLIDE 29

developed by project:odd at RedHat based on DynJS (Nashorn is on its way!) "more JavaScript than Node.js" Netty for async I/O Vert.x integration/interaction http://nodyn.io

slide-30
SLIDE 30

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

slide-31
SLIDE 31

“ 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-32
SLIDE 32

Node.js Architecture Node.js Architecture

slide-33
SLIDE 33

Nodyn Architecture Nodyn Architecture

slide-34
SLIDE 34

Embed in Java programs Embed in Java programs

public class EmbedExample { private static final String SCRIPT = "" + "var main = require('./project/main.js');" + "main.run();"; public void runMain(String... args) throws InterruptedException { // Use DynJS runtime RuntimeFactory factory = RuntimeFactory.init( EmbedExample.class.getClassLoader(), RuntimeFactory.RuntimeType.DYNJS); // Set config to run main.js NodynConfig config = new NodynConfig( new String[] { "-e", SCRIPT } ); // Create a new Nodyn and run it Nodyn nodyn = factory.newRuntime(config); nodyn.setExitHandler( new NoOpExitHandler() ); try { int exitCode = nodyn.run(); if (exitCode != 0) { throw new TestFailureException(); } } catch (Throwable t) { throw new TestFailureException( t ); } } }

slide-35
SLIDE 35

“ 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-36
SLIDE 36

vertx2-core vertx2-core

This module exposes the vert.x 2.x eventbus to node.js clients. https://github.com/nodyn/vertx2-core

mod-nodyn mod-nodyn

This supports running Node.js workloads inside of Vert.x. https://github.com/nodyn/mod-nodyn

slide-37
SLIDE 37

Beer-as-a-Service Beer-as-a-Service

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

slide-38
SLIDE 38

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-39
SLIDE 39

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-40
SLIDE 40
slide-41
SLIDE 41
slide-42
SLIDE 42

Conclusion Conclusion

JavaScript is an emerging language and widely adopted Node.js / IO.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 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

slide-43
SLIDE 43

Thank you! Thank you!

Questions? Questions?

Contact: @dasniko niko@n-k.de http://slides.com/dasniko/nodejs-jvm