Project Avatar (Lyon JUG & Alpes JUG - March 2014)

Click here to load reader

  • date post

    15-Feb-2017
  • Category

    Technology

  • view

    4.243
  • download

    1

Embed Size (px)

Transcript of Project Avatar (Lyon JUG & Alpes JUG - March 2014)

  • Project Avatar: Server Side JavaScript on the JVM Lyon JUG - March 2014

    !David Delabassee @delabassee Software Evangelist - Oracle

  • 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 Oracles products remains at the sole discretion of Oracle.

  • Copyright 2014, Oracle and/or its affiliates. All rights reserved.Copyright 2014, Oracle and/or its affiliates. All rights reserved.

    Agenda

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

    !3

  • Copyright 2014, Oracle and/or its affiliates. All rights reserved.Copyright 2014, Oracle and/or its affiliates. All rights reserved.

    Evolution of Web Application Architecture

    !4

    A Java EE Perspective

    Http, REST

    REST, SSE, WebSockets

    Pre

    sent

    atio

    n !

    (Ser

    vlet

    / JS

    P,

    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

  • Copyright 2014, Oracle and/or its affiliates. All rights reserved.Copyright 2014, Oracle and/or its affiliates. All rights reserved.

    Java EE The Latest in Enterprise Java

    !5

    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

  • Copyright 2014, Oracle and/or its affiliates. All rights reserved.Copyright 2014, Oracle and/or its affiliates. All rights reserved.

    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

    !6

    Server Side JavaScript

  • Copyright 2014, Oracle and/or its affiliates. All rights reserved.Copyright 2014, Oracle and/or its affiliates. All rights reserved.

    Node.js Programming Model

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

    Blocking I/O is bad

    !7

    Single threaded Event-loop

    Callback model Non-blocking I/O calls Heavily parallelized

    HTTP Callback Example:

  • Copyright 2014, Oracle and/or its affiliates. All rights reserved.Copyright 2014, Oracle and/or its affiliates. All rights reserved.

    Node.js Event Loop

    !8

    Single-ThreadedEvent Loop

    Resource-Intensive Operations

    Network

    File System

    Network

    Compute

    Register

    Callback

    Complete

    Operation

    Client Requests

    (Http)

    Response

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

    REST/SSE/WebSockets

    Java

    Scr

    ipt

    View

    Con

    trolle

    r

    JavaScript

    BrowserClientServer

    Node

    Evolution of Web Application Architecture

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

    Aggregate & transform content Return JSON to browser

    Mobile-enabling existing services

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

    Evolution of Web Application ArchitectureMobile-enabling existing services

    ClientServer

    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

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

    JavaScript and Node on the JVM

  • Copyright 2014, Oracle and/or its affiliates. All rights reserved.Copyright 2014, Oracle and/or its affiliates. All rights reserved.

    Project Nashorn

    ECMAScript 5.1 compliant Bundled with JDK 8

    Replaces Rhino Faster (2x 10x) More secure

    Java JavaScript interoperability

    !12

    JavaScript on the JVM

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

  • Copyright 2014, Oracle and/or its affiliates. All rights reserved.Copyright 2014, Oracle and/or its affiliates. All rights reserved.

    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 JVM e.g. Java frameworks, libraries, tools

    !13

  • Copyright 2014, Oracle and/or its affiliates. All rights reserved.Copyright 2014, Oracle and/or its affiliates. All rights reserved.

    Avatar.js = Node + Java

    !14

    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(

    ) 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();

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

    Project Avatar:Advanced JavaScript ServicesLeveraging Java EE

  • Copyright 2014, Oracle and/or its affiliates. All rights reserved.Copyright 2014, Oracle and/or its affiliates. All rights reserved.

    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

    !16

    A Server Side JavaScript Services Framework

  • Copyright 2014, Oracle and/or its affiliates. All rights reserved.Copyright 2014, Oracle and/or its affiliates. All rights reserved.

    Project Avatar

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

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

    !17

    Leveraging the JVM and Java EE

  • Copyright 2014, Oracle and/or its affiliates. All rights reserved.Copyright 2014, Oracle and/or its affiliates. All rights reserved.

    Avatar Architecture

    !18

    Server sideServer 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

  • Copyright 2014, Oracle and/or its affiliates. All rights reserved.Copyright 2014, Oracle and/or its affiliates. All rights reserved.

    Avatar Services

    !19

    Multi-core, state sharing, data storage

    Java

    JVM Process

    JavaScriptServices Services ServicesServices

    Database

    Data

    Notification

    shared state

    JSON JSON JSON JSON

    HTTP Load Balancer

  • Copyright 2014, Oracle and/or its affiliates. All rights reserved.Copyright 2014, Oracle and/or its affiliates. All rights reserved.

    Shared State

    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

    !20

    Lightweight inter-thread communication

  • Copyright 2014, Oracle and/or its affiliates. All rights reserved.Copyright 2014, Oracle and/or its affiliates. All rights reserved.

    Shared State Example

    !21

    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 alert topic! bus.on(alert, function(msg) {! print(name + got + msg); ! });!! // Start a background thread which publishes to the alert topic! // Background threads do not participate in request processing! threads.newBackgroundThread(background, monitor.js).start();!

  • Copyright 2014, Oracle and/or its affiliates. All rights reserved.Copyright 2014, Oracle and/or its affiliates. All rights reserved.

    Push Service Example

    !22

    var avatar = require(org/