The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

64
www.Hazelcast.com // follow me : twitter.com/gamussa The Power of the JVM Viktor Gamov Solutions Architect, Hazelcast Applied Polyglot Projects with Java and JavaScript

Transcript of The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

Page 1: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

www.Hazelcast.com // follow me : twitter.com/gamussa

The Power of the JVM

Viktor Gamov Solutions Architect, Hazelcast

Applied Polyglot Projects with Java and JavaScript

Page 2: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

“May you live in interesting times”

Chinese curse

Page 3: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

Why you’re here

★you're excited about JDK8 features

Page 4: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript
Page 5: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

Why you’re here

★you're excited about JDK8 features

★JavaScript enthusiast

Page 6: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript
Page 7: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

Why you’re here

★you're excited about JDK8 features

★JavaScript enthusiast

★You need to run JS and Java side-by-side

Page 8: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript
Page 9: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

Who is talking?

★Solutions Architect

Page 10: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

The leading open source in-memory data grid

Page 11: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

Who is talking?

★Solutions Architect

★co-author and jug co-lead

Page 12: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

DO you even javascript ?

Page 13: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript
Page 14: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

http://enterprisewebbook.com

Available in stores

Page 15: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

http://enterprisewebbook.com

Page 16: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

Part one

Page 17: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

Part one

Javascript for java developers

Page 18: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

Not-so-distant pasT

Java devs about javascript

I don’t care

Page 19: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

MANY good Parts

Page 20: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

Present

Java devs about javascript

Gotta learn more

Page 21: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

Everybody knows how to javascript

Page 22: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

Objects

var obj2 = {prop: 42};

// Store the data about Fox Muldervar person = { lastName: "Fox", firstName: "Mulder", age: 53 };

var obj = {};

Page 23: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

// Declaring functionfunction calcTaxes(income, dependent){ // do stuff}

Functions

// Invoking functioncalcTaxes(5000, 2);var myTaxes = calcTaxes(5000, 2); // declaration and invocation at the same time

// IIFE - immediate invoked function expression (function calcTaxes(income, dependent){ // do stuff})();

// Assigning function literal to a variablevar calcTaxes = function (income, dependent){ // do stuff};

Page 24: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

Assigning a Function to Object Property

person.doTaxes(5000, 2); person.doTaxes = function (income, dependent) { // do stuff };

Page 25: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

Object Methods in Literals

var person2 = { lastName: "Fox", firstName: "Mulder", age: 53, doTaxes: function (income, dependent) { // do stuff } };

person2.doTaxes(5000, 2);

Page 26: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

Constructor function

function Tax(income, dependents) { // Do something here } // Creating 2 Tax objects var t1 = new Tax(50000, 3);var t2 = new Tax(68000, 1);

Page 27: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

prototype// Constructor function Person function Person(name, title) { this.name = name; this.title = title; this.subordinates = [];} // Constructor function Employeefunction FbiAgent(name, title) { this.name = name; this.title = title;} FbiAgent.prototype = new Person();var emp = new FbiAgent("Dana Scully", "Special Agent");

Page 28: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

print(3[3]);

Page 29: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript
Page 30: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

Number.prototype[3] = ”JS Rocks!!!”; print(3[3]);

Page 31: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

www.destroyallsoftware.com/talks/wat

Page 32: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

Part TWO

Page 33: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

Part TWO

NASHORN: JSR-233 and beyond

Page 34: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

JAVAScript is everywhere

★v8 and nodejs

★Rhino was here for years

★Here comes nashorn

Page 35: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

What is Nashorn?

Page 36: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

Ultimate invokedynamic consumer

100% pure Java implementation

100% ECMAScript 5.1 compliant

JavaScript Engine for JVM

What is Nashorn?

100% compiled to bytecode, no interpreter

http://www.1001ausmalbilder.de/ausmalbilder/gross/ausmalbild-nashorn-7.jpg

Page 37: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

HOW To USE NASHORN?

★The only API is JSR-223: javax.scripting.*

★Java from JavaScript and vice versa

★Create and manipulate Java/JavaScript objects

★Extend Java classes

★ Implement Java interfaces

Page 38: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

ScriptEngineManager factory = new ScriptEngineManager();ScriptEngine engine = factory.getEngineByName("nashorn");engine.eval("Number.prototype[3] = “JS Rocks!”; print(3[3]);")

Page 39: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

String[] options = new String[] { "--persistent-code-cache", "--class-cache-size=50", “—no-java"}; * NashornScriptEngineFactory factory = new NashornScriptEngineFactory();NashornScriptEngine engine = (NashornScriptEngine) factory.getScriptEngine(options); engine.eval("Number.prototype[3] = ‘JS Rocks'; print(3[3]);");

* jjs -xhelp

http://cr.openjdk.java.net/~sundar/jdk.nashorn.api/8u40/javadoc/jdk/nashorn/api/scripting/NashornScriptEngineFactory.html

https://blogs.oracle.com/nashorn/entry/improving_nashorn_startup_time_using

Page 40: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

https://wiki.openjdk.java.net/display/Nashorn/Nashorn+extensions

var System = Java.type('java.lang.System'); System.out.println(10); // 10System.out["println"](11.0); // 11.0System.out["println(double)"](12); // 12.0

Parameter overloading

var Date = Java.type('java.util.Date');var date = new Date();date.year += 1900; print(date.year); // 2014

Java Beans

print(__FILE__, __LINE__, __DIR__); Whereis

Load Scripts//load('http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min.js');load('./lib/underscore-min.js');var odds = _.filter([1, 2, 3, 4, 5, 6], function (num) { return num % 2 == 1; });print(odds); // 1, 3, 5

Page 41: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript
Page 42: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

SHELL Scripting

Extend app functionality on runtime

1

2

3

JAVASCRIPT on JVM USE cASES

WEB-content generation4

provide extension points for application

https://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn/shell.html

Page 43: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

Part Three

Page 44: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

Part Three

Java and NODE.JS

Page 45: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

Node.js

Page 46: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

★Use npm to download js dependencies

★use common.js module format

★run under JVM / nashorn

JVM-NPMhttps://github.com/nodyn/jvm-npm

Page 47: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

load('./lib/jvm-npm.js');var _ = require('underscore');var odds = _.filter([1, 2, 3, 4, 5, 6], function (num) { return num % 2 == 1; });print(odds); // 1, 3, 5

Page 48: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript
Page 49: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

Avatar.js ftw!

https://avatar-js.java.net

Page 50: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

Take first available express demo from internet what could go wrong

Page 51: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

https://github.com/hacksparrow/ninja-store

https://github.com/gAmUssA/avatarjs-express

Page 52: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

import com.oracle.avatar.js.Server;import java.io.File;import java.lang.reflect.Field;public class Main { static { try { final Field sysPathsField = ClassLoader.class.getDeclaredField("sys_paths"); sysPathsField.setAccessible(true); sysPathsField.set(null, null); System.setProperty("java.library.path", new File(Main.class.getResource("/libavatar-js.dylib").getPath()) .getParentFile().getCanonicalPath()); System.loadLibrary("avatar-js"); } catch (Throwable t) { throw new RuntimeException(t); } } public static void main(String[] args) throws Throwable { Server.main(Main.class.getResource("/app.js").getPath()); }}

credits: https://github.com/danveloper/midwestjs-2014-nashorn/blob/master/node-app/src/main/java/midwestjs/nashorn/Main.java

Page 53: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript
Page 54: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

NASHORN in «wild life»

★Avatar 2.0

★vert.x

★nodyn.io

Page 55: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

Avatar 2.0★polyglot platform (sort oF)

★integration with nashorn

★Node api

★Model-Store api

★Event bus

★Distributed data

★not open yet

Page 56: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

Avatar 2.0

https://blogs.oracle.com/theaquarium/entry/project_avatar_update

R.I.P 2013 -2015

Page 57: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

★polyglot platform ★Develop with Java, Javascript, jRuby, Groovy, jython, clojure,

★Scala, php, ceylon

★integration with nashorn ★https://github.com/vert-x/mod-lang-nashorn

★Event bus (down to the browser)

★Distributed data (in v.3)

★Open source: EPL / Apache

vertx.io

Page 58: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

★NODE api for Java platform

★USES DYNJS - ALTERNATIVE JAVASCRIPT ENGINE

★INTEGRATED WITH VERTX

nodynhttp://nodyn.io/

Page 59: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

p.s. Node ❤ Java

Page 60: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript
Page 61: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

Demo

Page 62: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

Q&A

Page 63: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

SHOW ME THE CODEhttps://github.com/gAmUssA/java-scripting-experiments

https://github.com/gAmUssA/hazelcast-nashorn

https://github.com/gAmUssA/hazelcast-nodejs

https://github.com/gAmUssA/avatarjs-express

Page 64: The Power of the JVM: Applied Polyglot Projects with Java and JavaScript

THANKS FOR YOUR ATTENTION

www.hazelcast.com // follow me : twitter.com/gamussa