Top Banner
Project Avatar: Server Side JavaScript on the JVM QCon London 2014 David Delabassee @delabassee Software Evangelist - Oracle
42

Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

May 10, 2015

Download

Technology

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Project Avatar:Server Side JavaScript on the JVM QCon London 2014!David Delabassee @delabasseeSoftware Evangelist - Oracle

Page 2: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.!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.

Page 3: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.!3

"The French and the British are such good enemies that they can't resist being friends."

(Peter Ustinov)

Page 4: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.!4

Page 5: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.!5

Agenda

▪ Web Application Architecture Evolution ▪ JavaScript and Node on the JVM ▪ Project Avatar – Advanced JavaScript Services ▪ Avatar Client Framework ▪ Summary

Page 6: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.!6

Evolution of Web Application ArchitectureA Java EE Perspective

ClientServer

Pre

sent

atio

n !

(Ser

vlet

/ JS

P)

Ent

erpr

ise

Con

nect

ivity

and

Bus

ines

s Lo

gic

Http

▪ Request / Response ▪ Multi-Page Application

BrowserJava EE / JVM

Page 7: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.!7

Evolution of Web Application ArchitectureA Java EE Perspective

Pre

sent

atio

n !

(Ser

vlet

/ JS

P,

JSF)

Con

nect

ivity

(RE

ST

SS

E)

Ent

erpr

ise

Con

nect

ivity

and

Bus

ines

s Lo

gic

Http

REST

Java EE / JVM

▪ Multi-Page Application ▪ In-page updates (AJAX)

JavaScript

BrowserClientServer

Page 8: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.!8

Modern Web Application ArchitectureA Java EE Perspective

Http, REST

REST, SSE, WebSocketsP

rese

ntat

ion

!(S

ervl

et/ J

SP,

JSF)

Con

nect

ivity

(Web

Soc

ket,

RE

ST,

SS

E)

Ent

erpr

ise

Con

nect

ivity

and

Bus

ines

s Lo

gic

Java EE / JVM

▪ Single-page applications ▪ View/Controller in browser ▪ Model on server

View

Con

trolle

r

JavaScript

BrowserClientServer

Page 9: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.!9

Java EE – The Latest in Enterprise Java

ENTERPRISE EDITIONDEVELOPER

PRODUCTIVITYMEETING ENTERPRISE DEMANDS

Java EE 7

▪ Batch ▪ Concurrency ▪ Simplified JMS

▪ More annotated POJOs ▪ Less boilerplate code ▪ Cohesive integrated

platform▪ WebSockets ▪ JSON ▪ Servlet 3.1 NIO ▪ REST

Page 10: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.!10

Node.js

▪ Server-side JavaScript based on Chrome v8 engine ▪ Created by Ryan Dahl (2009), Open Source ▪ Designed for fast, scalable network applications ▪ Event-driven, non-blocking I/O model ▪ “Melting pot community”

– JavaScript, Java, .NET, PHP, … – Very active, with 60,000+ modules

Server Side JavaScript

Page 11: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.!11

Node.js Programming Model▪ Single threaded ▪ Event-loop

– Callback model – Non-blocking I/O calls

▪ Heavily parallelized – User code: sequential

▪ Multi-threading is hard – Thousands of concurrent connections – Deal with deadlocks and race conditions

▪ Blocking I/O is bad

HTTP Callback Example:

Page 12: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.!12

Node.js Event Loop

Single-ThreadedEvent Loop

Resource- Intensive

Operations

Network

File System

Network

Compute

Register Callback

CompleteOperation

Client Requests

(Http)

Response

Page 13: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.!13

Evolution of Web Application ArchitectureMobile-enabling existing services

REST/SSE/WebSocketsJa

vaS

crip

t

▪ Project-based end-to-end JavaScript ▪ Rapid prototyping & API layer ▪ Leverage backend resources

▪ Aggregate & transform content ▪ Return JSON to browser

View

Con

trolle

r

JavaScript

BrowserClientServer

Node

Page 14: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.!14

ClientServer

Evolution of Web Application ArchitectureMobile-enabling existing services

Ent

erpr

ise

Con

nect

ivity

and

Bus

ines

s Lo

gic

Java EE / JVM

Java

Scr

ipt

What if we could run Nodealongside Java EE inthe same JVM?

View

Con

trolle

r

JavaScript

Browser

REST / SSE/ WebSockets

Node

Page 15: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.!15

JavaScript and Node on the JVM

Page 16: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.!16

Project Nashorn

▪ ECMAScript 5.1 compliant ▪ Bundled with JDK 8

– Replaces Rhino – Faster (2x – 10x) – More secure

▪ Seamless Java ßà JavaScript interoperability

JavaScript on the JVM

http://download.java.net/jdk8/docs/technotes/guides/scripting/nashorn/index.html

Page 17: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.!17

Avatar.js

▪ Platform for server side JavaScript applications ▪ Requires Nashorn (JDK 8) ▪ 95% Node compatibility

– Use popular packages (Express, async, commander, etc) – Uses same portability libraries as Node.js – Limitation: No Chrome v8 native APIs

▪ Avatar.js Advantages – Leverage Java frameworks, libraries and tools – Security manager

Node on the JVM

Page 18: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.!18

Avatar.js = Node + Java

▪ Node Programming Model – Code in JavaScript – Single event loop / thread – Require (import) Node modules

▪ Invoke Java code – Java types and libraries – new java.lang.Thread();!

– new com.myorg.MyObj();

Leverage Java, including Threads

Java

JavaScript

java.lang.Thread

java.util.SortedSet

java.math.BigInteger

Node App

JVM Process

com.myorg.MyObj

require (‘async’)

postE

vent(

)

Page 19: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.!19

Project Avatar:Advanced JavaScript ServicesLeveraging Java EE

Page 20: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.!20

Project Avatar

▪ Similar in spirit to Servlets, but focused on REST, WebSocket, Server Sent Event (SSE) endpoints

▪ Use familiar Node.js event-driven programming model and modules ▪ Layers on Avatar.js Node-compatible runtime ▪ Adds integrated enterprise features

A Server Side JavaScript Services Framework

Page 21: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.!21

Project Avatar

▪ Multi-threading, lightweight message passing, no mutable shared state

▪ Model Store – Object Relational Mapping ▪ HTTP listener / load-balancer managed by framework ▪ Messaging – JMS on Java EE container

Leveraging the JVM and Java EE

Page 22: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.!22

Avatar Architecture - ServerServer Database

Data

Notification

JDK 8 / Nashorn

Avatar Runtime

Node Modules

Avatar Modules

Server Runtime (Java EE)

Application Services

= Java framework= JavaScript framework= Application code

Avatar.js

Page 23: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.!23

Avatar ServicesMulti-core, state sharing, data storage

Java

JVM Process

JavaScriptServices Services ServicesServices

Database

Data

Notification

shared state

JSON JSON JSON JSON

HTTP Load Balancer

Page 24: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.!24

Shared State

▪ Two Models – MessageBus

▪ Publish/subscribe message passing – Shared State

▪ Simple map API ▪ Application-scoped instance ▪ Session-scoped instance

– Named – Leased, with configurable timeout

▪ Provide required serialization, concurrency, and caching

Lightweight inter-thread communication

Page 25: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.!25

State Sharing Example var avatar = require(‘org/glassfish/avatar’);! var threads = require(‘org/glassfish/avatar/threads’);! var app = avatar.application;! var name = app.name;! var bus = app.bus;!! // Listen for messages on the ‘hello’ topic! bus.on(‘hello’, function(msg) {! print(name + ‘ got ‘ + msg); ! });!! // Start a background thread which publishes to the ‘hello’ topic! // Background threads do not participate in request processing! threads.newBackgroundThread(‘background’, ‘hello.js’).start();!

Page 26: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.!26

Push Service Example var avatar = require(‘org/glassfish/avatar’);! var threads = require('org/glassfish/avatar/threads');! var bus = avatar.application.bus;!! // Register a push service that forwards background messages! avatar.registerPushService({url: 'push/message'}, function () { ! this.onOpen = function (context) {! bus.on(’background', function(msg) {! context.sendMessage({body: msg});! });! };! });!! // Create and start a background thread that publishes messages! threads.newBackgroundThread(‘background’, ‘background.js’).start();!

Page 27: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.!27

WebSocket Service Example

// Load avatar module! var avatar = require(‘org/glassfish/avatar’); !! // Register service instance! avatar.registerSocketService({ url: ‘websocket/chat’ }, !! ! function() { this.data = { transcript : ‘’ };!

this.onMessage = function (peer, message) { this.data.transcript += message; this.data.transcript += ‘\n’; peer.getContext().sendAll(this.data);! };! });!

Page 28: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.!28

Model-Store Framework

▪ JavaScript ORM library ▪ Pure JavaScript API that

– Supports relational and non-relational databases – Integration with other Avatar services

▪ Similar to pure Node.js libraries – Sequelize, JugglingDB, db

Page 29: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.!29

Model-Store APIModel and Database setup

var Product = avatar.newModel({! "name": {! type: "string",! primary: true! },! "price": "number",! "quantity": "integer"! });!

var store = avatar.newStore(‘mysql’, {! host: ‘localhost’,! port: 3306,! database: ‘test’,! username: ‘root’,! createDb: true,! dropTables: true! });!

Page 30: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.!30

Model-Store ExampleCreating and Storing an Object

// Binds Product model with store! Product.bind(store); ! // Insert a new product into the db! store.connect(function() { Product.create({ name: 'Widget',! price: 1.00, quantity: 2 }, function(err, w1) { console.log(JSON.stringify(w1)); store.disconnect(function() { // Done! }); }); });

▪ Bind Model to Store

▪ Connect to Store – Creates Product table if

required – Callback adds product to

table

Page 31: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.!31

Model-Store API

▪ Models can have relationships with other models – 1:1, 1:n, M,N

▪ Data Stores – Relational

▪ Testing: Oracle DB, MySQL, Derby (Embedded, Network) ▪ Non-tested: Any other JDBC driver

– Non-relational ▪ Oracle NoSQL, MongoDB (in progress)

Page 32: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.!32

Model-Store API

▪ Lots of possibilities – Configure JPA provider using properties – Generate JavaScript model from database schema – User transactions – 2nd level JPA cache, TopLink Grid – Oracle RAC Support – More …

▪ Maintain pure JavaScript API ▪ We’re looking for YOUR feedback!

Opportunities to leverage JPA features

Page 33: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.!33

Avatar Client Framework

Page 34: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.!34

Avatar Client Model

▪ View – Extensible component views – Pre-defined Widget Sets: jQuery UI (default), jQuery Mobile, Dijit – Declarative UI components

▪ Model – Models (WS, SSE, REST, local) in JavaScript – Easily connects to Java and JavaScript services – Model library usable as standalone JavaScript file

▪ Other Highlights – Familiar syntax in HTML with “data-” tags – Bidirectional Data binding using EL (Expression Language) – CSS support – AMD modules for code partitioning

Page 35: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.!35

Avatar Architecture – Server and ClientServer

Client

REST/WebSocket/SSE

*.html

*.css

*.js HTP

Database

Data

Notification

JDK 8 / Nashorn

Avatar Compiler

Avatar Runtime

Node Modules

Avatar Modules

Server Runtime (Java EE)

Application Services

Application Views

= Java framework= JavaScript framework= Application code

Avatar.js

Page 36: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.!36

Hello World Example <script data-model=”local” data-instance="name">!       var NameModel = function() {!             this.first = "John";!             this.last = "Doe";!             this.clear = function() { this.first = this.last = "”; }; ! };! </script>!

View

<form>! <label for="first">First Name</label>! <input id="first" type="text” data-value="#{name.first}"/>! <label for=“last">Last Name</label>! <input id=“last" type="text” data-value="#{name.last}"/>! Hello #{name.first} #{name.last}! <button onclick="#{name.clear()}”>Clear</button>! </form>!

Model

Page 37: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.!37

Chat Example: WebSockets

<script data-model="socket">! var ChatModel = function() {! …! this.sendMsg = function() {! this.send(this.user + ":" + this.message);! this.message = "";! };! };! </script>!! <script data-type="ChatModel" data-instance="chat”! data-url=“websocket/chat”>! </script>!

Page 38: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.!38

Summary

▪ Invoke Java code ▪ Multi-threading optimizations

– Share state across threads, JVMs – Built-in load balancing across threads

▪ Leverage Java EE services ▪ Deploy on existing Java EE infrastructure

– Leverage AppServer features (clustering, lifecycle management)

Server Side JavaScript on the JVM

Page 39: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.!39

Avatar Roadmap

WebLogic Runtime

2014 20152013

Project Avatar Launch (JavaOne, 2013 !

GlassFish 4 Runtime

WebLogic 12.1.4 !Avatar Commercial

Support

Page 40: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.!40

Next Steps

1) Download https://avatar.java.net/

2) Try it out !3) Give us feedback https://avatar.java.net/mailing.html

Page 41: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.!41

Thanks!

Project Avatar https://avatar.java.net/

!Java EE 8 Survey http://glassfish.org/survey

Page 42: Server Side JavaScript on the JVM - Project Avatar - QCon London March 2014

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.!42