Presentation at JavaOne San Francisco 2013 - Avatar.js - Java.net

bootlessbwakInternet and Web Development

Nov 12, 2013 (3 years and 6 months ago)

91 views

Copyright
©
2013, Oracle and/or its affiliates. All rights reserved.
1
Server
-
side JavaScript on the
JVM with
Nashorn
and
Avatar.js
Akhil Arora
Principal Member of Technical Staff
Oracle
Copyright
©
2013, Oracle and/or its affiliates. All rights reserved.
3
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.
Copyright
©
2013, Oracle and/or its affiliates. All rights reserved.
4
Program Agenda

Introduction

Features and Benefits

Current Status

Demo
Copyright
©
2013, Oracle and/or its affiliates. All rights reserved.
5
Introduction

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
What is Avatar.js?
Copyright
©
2013, Oracle and/or its affiliates. All rights reserved.
6
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
Avatar.js project goals
Copyright
©
2013, Oracle and/or its affiliates. All rights reserved.
7
Features

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
Some salient features of Avatar.js
Copyright
©
2013, Oracle and/or its affiliates. All rights reserved.
8
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

I
mprove density
Copyright
©
2013, Oracle and/or its affiliates. All rights reserved.
9
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
Copyright
©
2013, Oracle and/or its affiliates. All rights reserved.
10
Benefits

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)
Why use Avatar.js?
Copyright
©
2013, Oracle and/or its affiliates. All rights reserved.
11
Implementation

Create Java bindings for
libuv
and
n
ode’s http
-
parser

Implement the _wrap modules to call into

libuv
and http
-
parser Java bindings

Java SE platform APIs

Try to use n
ode
modules
js
unmodified
as much as possible

Test implementation with node unit tests

Lightly patch
n
ode tests and modules when unavoidable
Avatar.js Implementation
Copyright
©
2013, Oracle and/or its affiliates. All rights reserved.
12
libuv

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

t
o receive callbacks from
libuv
Cross
-
platform
asynchronous I/O Library
Copyright
©
2013, Oracle and/or its affiliates. All rights reserved.
13
http
-
parser

http parser used internally by node

I
nput is a buffer containing raw bytes

C
alls 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
Synchronous http parser
Copyright
©
2013, Oracle and/or its affiliates. All rights reserved.
14
Avatar Package Manager

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
apm
Copyright
©
2013, Oracle and/or its affiliates. All rights reserved.
15
Status

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
As of September 2013
Copyright
©
2013, Oracle and/or its affiliates. All rights reserved.
16
Module Status

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
As of September 2013
Copyright
©
2013, Oracle and/or its affiliates. All rights reserved.
17
Some work planned after
JavaOne
2013

Compatibility improvements

fs
, crypto

Implement missing features

domain,
readline
,
repl
, signals

Sync with upstream changes
Contributions Welcome!
Copyright
©
2013, Oracle and/or its affiliates. All rights reserved.
18
Demo

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

j
avax.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
Photo uploader

a Hybrid node/Java app
Copyright
©
2013, Oracle and/or its affiliates. All rights reserved.
19
Demo: Database Access
function
Connection(
url
, options
, callback)
Connection.prototype.createStatement
= function()
{…} // returns Statement
Connection.prototype.prepareStatement
= function(query)
{…} // returns Statement
function Statement() {}
Statement.prototype.execute
=
function(
c
allback
)
{…}
Statement.prototype.executeQuery
= function(query,
callback
)
{…}
// maps
ResultSet
to
array of JS Objects with keys ==
rs.metaData.columnName
exports.toArray
= function(
resultSet
)
{…}
Example JDBC Wrapper for Avatar
Copyright
©
2013, Oracle and/or its affiliates. All rights reserved.
20
Demo: Querying DB
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
))
}
}
Searching for images
Copyright
©
2013, Oracle and/or its affiliates. All rights reserved.
21
Demo: Saving Images
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
);
});
}
Saving images to DB
Copyright
©
2013, Oracle and/or its affiliates. All rights reserved.
22
Demo: Resizing Images
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);
}
Using the
javax.imageio.ImageIO
API in Avatar
Copyright
©
2013, Oracle and/or its affiliates. All rights reserved.
23
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~src
Copyright
©
2013, Oracle and/or its affiliates. All rights reserved.
24
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.
Copyright
©
2013, Oracle and/or its affiliates. All rights reserved.
25