Thursday, 2 October 2014

Nashorn and Avatar, or bring JavaScript and Node.js to the JVM

Introduction

There is a lot of attention lately for the server side javascript possibilities.  This in response to increased popularity of the language during the last years.

And I’m not going to judge here if it is a bad or good move, javascript on the server. It became a platform and probably the best feature is the non-blocking style of actions.

But what if you are a Java developer for a long time, are very confident programming in it?  Should you thow it all away and start over again in JavaScript?

Or how can you integrate your JavaScript code with enterprise services?

Nashorn

With the release of Java 8, not only the lambdas, stream and all those other goodies are added to the JVM, there is also something that is called Nashorn.

Nashorn is a JavaScript engine which complies to the JSR-292 specification, the support for the dynamically typed languages. And it allows you to run some JavaScript on the JVM.

There are various possibilities. From executing a few lines of JavaScript which are placed in a String from within Java to a full blown JavaScript file which is started with the jss command from the command line.

In each case, the javascript is compiled to run on the JVM.  And since it is running on the JVM, why not foresee some interoperability with Java.  
And that is why I find Nashorn interesting.  It allows you easily to call some Java method you have written or which are defined in some library.

You can write something like 

var LinkedList = Java.type("java.util.LinkedList");
var list = new LinkedList;
list.add(1);
list.add(2);
print(list);
print(list instanceof LinkedList);

And you are using the Java LinkedList object from within JavaScript. You can interpret Java.type as an import statement. 

So the end result is a very transparently interaction between the two.  You can pass JavaScript variables to Java Methods and Java variables to JavaScript functions.  There is no distinction.

You can even mix calls, like a Java method as the JavaScrip callback function.

avatar.js

node.js is one of the popular JavaScript server side solutions today.  So there are a lot of modules created for this platform.
So in the same philosophy that you want to bring JavaScript to the JVM, avatar.js brings the node programming model (API and modules) to the JVM.

Of course, not every node.js module can be run by avatar.js and the JVM. If it makes use of some V8 engine specific features, it will not run of course since they are not available on Nashorn.

But the site lists quite some of the most important modules which are tested and run smoothly.  They guess that up to 95% of the modules work.

Another addition is that with avatar-js, you can use the full power of your machine. node.js event loop runs in a single thread.  But with avatar.js you can easily spawn other threads and do some work over there. 
For example when you have a task which may take a while and for which you don’t need the result in the current request.
And you can even have multiple threads, each of them running the event loop and they are connected with an event bus to communicate between each other.

Avatar 2.0

There used to be also a project avatar.  It was an extension on top of avatar.js which had some kind of view layer and access points to Java EE technologies like JPA and JMS.
So there would be a strong integration with the Java EE stack and it should be deployed on Java EE Servers.

But at JavaOne 2014, they announced that they have abandoned this path.

Instead, they have now Avatar 2.0.  Which is the combination of avatar.js and a model store which allows you to store your data in a RDBMS or No-sql store. And it is highly inspired on JDBC and JPA but entirely written in JavaScript.

Based on some JSON schema dialect, you can define the mapping between the properties and the database fields.  You can even define some relations like foreign keys.

Why interesting ?

So maybe you ask yourself, why I find this interesting and place this text on the statelessprime blog?

Well, until now, I was always playing with some client side javascript frameworks like AngularJS which are talking to the backend using JAX-RS.

But if you want to split up a project, you put services, data access and model in one project.  And controller (like JAX-RS controllers) and view technology (if any) in another project.

So I’m playing now with the idea to move the RESTful services to server side javascript (using Avatar 2.0 and for example the express node module).  This javascript part can talk then to your Java EE business logic by using the Java interface.

And by using for example the BeanManagerProvider from DeltaSpike, it should theoretically be possible to call into the Java EE components.

BeanManager bm = BeanManagerProvider.getInstance().getBeanManager();

For now, it is maybe a wild idea, but I’m willing to give it a try.  In any case, the results will be posted here some day.



No comments:

Post a Comment