Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
1 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Server-side JavaScript on the JVM with Nashorn and Avatar.js Akhil Arora Principal Member of Technical Staff Oracle The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle. 3 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Program Agenda Introduction Features and Benefits Current Status Demo 4 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Introduction What is Avatar.js? An open-source project on java.net A server-side JavaScript framework for the JVM A combination of – The node event-driven model, module system and APIs – Nashorn JavaScript engine (new in Java8) – Java bindings for some native node dependencies libuv and http-parser – Some additional native code – Java Platform APIs 5 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Goals Avatar.js project goals A platform for JavaScript Enterprise Applications Compatible with the node programming model (as much as possible) – node applications should run with little or no modification on Avatar – Except for modules and apps with native code (v8-specific) Enable using JavaEE APIs in Avatar applications – By providing JavaScript wrappers – By providing asynchronous adapters where necessary – To not block the main JavaScript thread 6 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Features Some salient features of Avatar.js Run node applications on the JVM Call any Java API from JavaScript Support large heaps Support multiple event loops Support multiple Java threads – JavaScript remains single-threaded 7 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Multiple Event Loops Each Event Loop runs in its own JavaScript context Each context provides a global reference to its event loop Event Loop has APIs to post events and async tasks Advantages – Improve core utilization – Run multiple apps in the same JVM – Improve density 8 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Multiple Java threads Can offload cpu-intensive tasks to background Java threads Can adapt synchronous APIs to asynchronous facade Threads can post events to the event queue – and thus call-back into JavaScript Simpler than forking child processes – and communicating with them over pipes in JSON 9 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Benefits Why use Avatar.js? Can reuse existing Java libraries and middleware Can take advantage of multiple cores Can be secured using the Java security model Can be managed using existing Java tools – VisualVM, MBeans, Flight Recorder, Mission Control Can access Java Platform APIs to complement node APIs – BigInteger, Date, Calendar, XML, SQL, Multicast, etc Small enough for embedded usage (less than 2Mb) 10 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Implementation Avatar.js Implementation Create Java bindings for libuv and node’s http-parser Implement the _wrap modules to call into – libuv and http-parser Java bindings – Java SE platform APIs Try to use node modules js unmodified as much as possible Test implementation with node unit tests Lightly patch node tests and modules when unavoidable 11 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. libuv Cross-platform asynchronous I/O Library Porting layer for node.js Encapsulates platform differences Features – Non-blocking tcp sockets, named pipes, udp, dns, tty, fork, etc We created a Java binding using JNI for libuv, called libuv-java This Java binding is used by Avatar.js – to call into libuv – to receive callbacks from libuv 12 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. http-parser Synchronous http parser http parser used internally by node Input is a buffer containing raw bytes Calls callbacks synchronously as http elements are parsed We created a Java binding using JNI, called http-parser-java This Java binding is used by Avatar.js – To parse http content 13 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Avatar Package Manager apm Compliant with npm v1.2.15 (as of September 2013) Javascript API and command line utility – install, uninstall, test, publish … Interacts with the node module registry (npmjs.org) Use to search, install, publish modules 14 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Status As of September 2013 Compatible with node v0.10.18 Most modules work – Most reuse node JavaScript sources unmodified – Some use Java APIs (buffer, fs, crypto, dns, timers, zlib) Of 584 node unit tests in v0.10.18 – 470 tests pass (80%) – Some tests (8%) are patched lightly to work with Avatar.js 15 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Module Status As of September 2013 Modules known to work – abbrev ansi async block-stream chmodr chownr colors commander connect debug express graceful-fs inherits ini init-package-json grunt jade lodash mime mkdirp mocha 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 Modules that work with a small patch – express/ejs, grunt-contrib-uglify, lockfile, vows Modules not yet working – coffee-script 16 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Some work planned after JavaOne 2013 Contributions Welcome! Compatibility improvements – fs, crypto Implement missing features – domain, readline, repl, signals Sync with upstream changes 17 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Demo Photo uploader – a Hybrid node/Java app Written using express, socket.io, jade, jquery, JDBC, javax.imageio Calls into Java to illustrate access to Java EE APIs – JDBC API wrapped with JavaScript and made non-blocking – javax.imageio to rescale uploaded images to thumbnails – Rescaling done in a background Java thread to offload main thread Use the best of both node and Java ecosystems 18 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Demo: Database Access Example JDBC Wrapper for Avatar function Connection(url, options, callback) Connection.prototype.createStatement = function() {…} // returns Statement Connection.prototype.prepareStatement = function(query) {…} // returns Statement function Statement() {} Statement.prototype.execute = function(callback) {…} Statement.prototype.executeQuery = function(query, callback) {…} // maps ResultSet to array of JS Objects with keys == rs.metaData.columnName exports.toArray = function(resultSet) {…} 19 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Demo: Querying DB Searching for images ImageProvider.prototype.findAll = function(callback) { var sql = "select * from images“; createStatement().executeQuery(sql, function(err, resultSet) { if (err) callback(err) else callback(undefined, toArray(resultSet)) } } 20 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Demo: Saving Images Saving images to DB ImageProvider.prototype.save = function(img, callback) { var ps = prepareStatement('insert into images(filename, image) values(?, ?)'); ps.setString(1, img.filename); var blob = createBlob(); blob.setBytes(1, img.array()); ps.setBlob(2, blob); ps.execute(function(err, ret) { ps.close(); callback(err, ret); }); } 21 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Demo: Resizing Images Using the javax.imageio.ImageIO API in Avatar exports.generateThumbnail = function(imgPath, format, width, height, callback) { var r = function() { var originalImage = javax.imageio.ImageIO.read(new java.io.File(imgPath)); var resizedImage = new java.awt.image.BufferedImage(width, height, type); var g = resizedImage.createGraphics(); g.drawImage(originalImage, 0, 0, width, height, null); javax.imageio.ImageIO.write(resizedImage, …); }; eventloop.submit(r); } 22 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Resources Project Home – http://avatar-js.java.net Mailing List – [email protected] Issue Tracker – https://java.net/jira/browse/AVATAR_JS Source repository – git://java.net/avatar-js~src 23 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. The preceding is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle. 24 Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 25 Copyright © 2013, Oracle and/or its affiliates. All rights reserved.