In this webinar
JavaScript is a powerful, flexible, and dynamically typed language. JVM is proven to be reliable and stable production platform. The power of the JVM is an ability to run programs written in different languages, including JavaScript. You can short the development cycle by leveraging a scripting abilities of JavaScript. This presentation is about JVM’s scripting capabilities that allow to mix Java and JavaScript in the same application.
In this session you’ll get introduced to the latest state of the polyglot frameworks that use JavaScript and Java side-by-side.
We’ll cover these topics:
-The JDK8’s Nashorn benefits for the server and the client side developers
-Using JavaScript and NodeJS applications and libraries on the Java platform
-Real-world projects overview that leveraging on Java-JavaScript interactions
-Live Q&A
Presenter:
Viktor Gamov, Senior Solutions Architect at Hazelcast
Viktor joined Hazelcast with over 5 years experience of architecting and building the enterprise applications using open source technologies. At his previous roles, he helped the financial companies and startups with various Java and HTML5 projects. He holds MS in Computer Science. He is a co-author of the O’Reilly book «Enterprise Web Development. From Desktop To Mobile». Viktor presented at various international conferences (http://lanyrd.com/gamussa) on Java and JavaScript related topics. He tweets at @gamussa.
The Power of the JVM: Applied Polyglot Projects with Java and JavaScript
1. 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
22. Objects
var obj2 = {prop: 42};
// Store the data about Fox Mulder
var person = {
lastName: "Fox",
firstName: "Mulder",
age: 53
};
var obj = {};
23. // Declaring function
function calcTaxes(income, dependent){
// do stuff
}
Functions
// Invoking function
calcTaxes(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 variable
var calcTaxes = function (income, dependent){
// do stuff
};
24. Assigning a Function to
Object Property
person.doTaxes(5000, 2);
person.doTaxes = function (income, dependent) {
// do stuff
};
25. Object Methods in Literals
var person2 = {
lastName: "Fox",
firstName: "Mulder",
age: 53,
doTaxes: function (income, dependent) {
// do stuff
}
};
person2.doTaxes(5000, 2);
36. 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
37. 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
40. https://wiki.openjdk.java.net/display/Nashorn/Nashorn+extensions
var System = Java.type('java.lang.System');
System.out.println(10); // 10
System.out["println"](11.0); // 11.0
System.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
41.
42. 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
57. ★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
58. ★NODE api for Java platform
★USES DYNJS - ALTERNATIVE JAVASCRIPT ENGINE
★INTEGRATED WITH VERTX
nodyn
http://nodyn.io/
63. SHOW ME THE CODE
https://github.com/gAmUssA/java-scripting-experiments
https://github.com/gAmUssA/hazelcast-nashorn
https://github.com/gAmUssA/hazelcast-nodejs
https://github.com/gAmUssA/avatarjs-express