1 / 25

Server-side JavaScript on the JVM with Avatar.js

Server-side JavaScript on the JVM with Avatar.js. Akhil Arora Principal Member of Technical Staff Oracle.

fancy
Download Presentation

Server-side JavaScript on the JVM with Avatar.js

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Server-side JavaScript on the JVM with Avatar.js Akhil AroraPrincipal Member of Technical Staff Oracle

  2. 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. Program Agenda • Introduction • Features and Benefits • Current Status • Demo

  4. 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. 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. 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. 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. 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. 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. 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. 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. 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. Avatar Package Manager apm • Compliant with npmv1.2.15 (as of September 2013) • JavascriptAPI and command line utility • install, uninstall, test, publish … • Interacts with the node module registry (npmjs.org) • Use to search, install, publish modules

  14. 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. Module Status As of September 2013 • Modules known to work • abbrev ansiasync block-stream chmodrchownr colors commander connect debug express graceful-fs inherits iniinit-package-json grunt jade lodash mime mkdirp mocha node-unit node-uuid once opener optimist osenv passport q read redis request retry rimrafronnsemver slide socket.io tar uglify-jsuid-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. Some work planned after JavaOne 2013 Contributions Welcome! • Compatibility improvements • fs, crypto • Implement missing features • domain, readline, repl, signals • Sync with upstream changes

  17. 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. 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. Demo: Querying DB Searching for images ImageProvider.prototype.findAll = function(callback) { varsql = "select * from images“; createStatement().executeQuery(sql, function(err, resultSet) { if (err) callback(err) else callback(undefined, toArray(resultSet)) } }

  20. Demo: Saving Images Saving images to DB ImageProvider.prototype.save = function(img, callback) { varps = prepareStatement('insert into images(filename, image) values(?, ?)'); ps.setString(1, img.filename); varblob = createBlob(); blob.setBytes(1, img.array()); ps.setBlob(2, blob); ps.execute(function(err, ret) { ps.close(); callback(err, ret); }); }

  21. Demo: Resizing Images Using the javax.imageio.ImageIO API in Avatar exports.generateThumbnail = function(imgPath, format, width, height, callback) { varr = new java.lang.Runnable() { run: function() { varoriginalImage = javax.imageio.ImageIO.read(new java.io.File(imgPath)); varresizedImage = new java.awt.image.BufferedImage(width, height, type); varg = resizedImage.createGraphics(); g.drawImage(originalImage, 0, 0, width, height, null); javax.imageio.ImageIO.write(resizedImage, …); } }; eventloop.submit(r); }

  22. Resources • Project Home • http://avatar-js.java.net • Mailing List • users@avatar-js.java.net • Issue Tracker • https://java.net/jira/browse/AVATAR_JS • Source repository • git://java.net/avatar-js~avatarjs

  23. 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.

More Related