36
JavaScript on Java EE Markus Eisele, @myfear Developer Advocate [email protected] September, 2014

JavaScript on Java EE - Joker 2014 · Compiled from "helloToWorld.groovy" public class helloToWorld extends groovy.lang.Script{ public static java.lang.Long __timeStamp; public static

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Page 1: JavaScript on Java EE - Joker 2014 · Compiled from "helloToWorld.groovy" public class helloToWorld extends groovy.lang.Script{ public static java.lang.Long __timeStamp; public static

JavaScript on Java EE

Markus Eisele, @myfear

Developer Advocate

[email protected]

September, 2014

Page 2: JavaScript on Java EE - Joker 2014 · Compiled from "helloToWorld.groovy" public class helloToWorld extends groovy.lang.Script{ public static java.lang.Long __timeStamp; public static

2

Java Developers and JavaScript

Page 3: JavaScript on Java EE - Joker 2014 · Compiled from "helloToWorld.groovy" public class helloToWorld extends groovy.lang.Script{ public static java.lang.Long __timeStamp; public static

Epilog:

What and Why?

3

Page 4: JavaScript on Java EE - Joker 2014 · Compiled from "helloToWorld.groovy" public class helloToWorld extends groovy.lang.Script{ public static java.lang.Long __timeStamp; public static

4

Page 5: JavaScript on Java EE - Joker 2014 · Compiled from "helloToWorld.groovy" public class helloToWorld extends groovy.lang.Script{ public static java.lang.Long __timeStamp; public static

Categories

5

• Glue Languages

• Job Control

• Application Specific

• Embedded Languages

Page 6: JavaScript on Java EE - Joker 2014 · Compiled from "helloToWorld.groovy" public class helloToWorld extends groovy.lang.Script{ public static java.lang.Long __timeStamp; public static

Static vs. Dynamic Typing

• Most scripting languages are dynamically typed

– explicit type declarations not required

– type information is attached to values, not to variables

• Java is static-typed

– require variable type (declaration time)

– only data of declared type

6

Page 7: JavaScript on Java EE - Joker 2014 · Compiled from "helloToWorld.groovy" public class helloToWorld extends groovy.lang.Script{ public static java.lang.Long __timeStamp; public static

Weak vs. Strong Typing

• Java is a static, strongly typed language

– strongest possible constraint on the type of object at declaration

time

– prevents mixing operations between mismatched types

• Many scripting languages are weakly typed

– allow operations on incompatible types

– implicit type conversion or ad-hoc polymorphism

7

Page 8: JavaScript on Java EE - Joker 2014 · Compiled from "helloToWorld.groovy" public class helloToWorld extends groovy.lang.Script{ public static java.lang.Long __timeStamp; public static

Quality and Performance

• Scripting languages are more compact and readable

– less lines of code

– weak typing not requiring the overhead of type declaration

• Fewer lines of code and less complexity means lower amounts

of bugs, thus reducing development and maintenance costs.

• The missing type information has some disadvantages.

– static, strongly typed languages ensure the robustness

– type errors will be detected at compile time

8

Page 9: JavaScript on Java EE - Joker 2014 · Compiled from "helloToWorld.groovy" public class helloToWorld extends groovy.lang.Script{ public static java.lang.Long __timeStamp; public static

Quality and Performance

• runtime performance

• extra overhead of the interpreter and

runtime checks

– not performed at compile time

9

htt

p://a

ttra

ctive

cha

os.g

ith

ub

.io

/plb

/

Page 10: JavaScript on Java EE - Joker 2014 · Compiled from "helloToWorld.groovy" public class helloToWorld extends groovy.lang.Script{ public static java.lang.Long __timeStamp; public static

On the rise

10

http://r

edm

onk.c

om

/sogra

dy/2

014/0

1/2

2/language

-rankin

gs-1

-14/

Page 11: JavaScript on Java EE - Joker 2014 · Compiled from "helloToWorld.groovy" public class helloToWorld extends groovy.lang.Script{ public static java.lang.Long __timeStamp; public static

Personal Motivation

• It’s cool

• Try something different

• It’s fun!

11

Page 12: JavaScript on Java EE - Joker 2014 · Compiled from "helloToWorld.groovy" public class helloToWorld extends groovy.lang.Script{ public static java.lang.Long __timeStamp; public static

How to combine

Java and JavaScript?

12

Page 13: JavaScript on Java EE - Joker 2014 · Compiled from "helloToWorld.groovy" public class helloToWorld extends groovy.lang.Script{ public static java.lang.Long __timeStamp; public static

General ways

• compile

• Bean Scripting Framework (BSF)

• JSR 223 – Scripting for the Java

Platform

13

Page 14: JavaScript on Java EE - Joker 2014 · Compiled from "helloToWorld.groovy" public class helloToWorld extends groovy.lang.Script{ public static java.lang.Long __timeStamp; public static

Compile (e.g. groovyc)

14

println "Hello, ${args[0]}, the time is ${new Date()}"

Compiled from "helloToWorld.groovy" public class helloToWorld extends

groovy.lang.Script{ public static java.lang.Long __timeStamp; public static

java.lang.Long __timeStamp__239_neverHappen1283320660787; public

helloToWorld();

Page 15: JavaScript on Java EE - Joker 2014 · Compiled from "helloToWorld.groovy" public class helloToWorld extends groovy.lang.Script{ public static java.lang.Long __timeStamp; public static

Script Engine

15

GroovyShell gs = new GroovyShell();String script = "return 42";int answer = (Integer) gs.evaluate(script);

Binding binding = new Binding();binding.setVariable("foo", new Integer(2));GroovyShell shell = new GroovyShell(binding);

Object value = shell.evaluate("println 'Hello World!'; x = 123; return foo * 10");

assert value.equals(new Integer(20));assert binding.getVariable("x").equals(new Integer(123));

http://groovy.codehaus.org/Embedding+Groovy

Page 16: JavaScript on Java EE - Joker 2014 · Compiled from "helloToWorld.groovy" public class helloToWorld extends groovy.lang.Script{ public static java.lang.Long __timeStamp; public static

Bean Scripting Framework (BSF)

• http://commons.apache.org/proper/commons-bsf/

• Bean Scripting Framework (BSF) is a set of Java

classes which provides scripting language support

within Java applications, and access to Java objects

and methods from scripting languages.

16

Page 17: JavaScript on Java EE - Joker 2014 · Compiled from "helloToWorld.groovy" public class helloToWorld extends groovy.lang.Script{ public static java.lang.Long __timeStamp; public static

Bean Scripting Framework (BSF) –

Example

17

BSFManager manager = new BSFManager();

manager.declareBean("a", 6, Integer.class);manager.declareBean("b", 7, Integer.class);

String script = "var answer = a * b;" +"bsf.registerBean(\"answer\", answer)";

manager.eval("javascript", "blah", 0, 0, script);

Double answer = (Double) manager.lookupBean("answer");assertEquals(42, answer.intValue());

Page 18: JavaScript on Java EE - Joker 2014 · Compiled from "helloToWorld.groovy" public class helloToWorld extends groovy.lang.Script{ public static java.lang.Long __timeStamp; public static

JSR-223 – Scripting for the Java

Platform• The specification describe mechanisms allowing scripting language programs to

access information developed in the Java Platform …

https://jcp.org/en/jsr/detail?id=223

• Java 1.6+

• Rhino JavaScript for Java version 1.6R2

• javax.script.*

• jrunscript

– http://docs.oracle.com/javase/6/docs/technotes/tools/share/jrunscript.html

18

Page 19: JavaScript on Java EE - Joker 2014 · Compiled from "helloToWorld.groovy" public class helloToWorld extends groovy.lang.Script{ public static java.lang.Long __timeStamp; public static

ServiceLoader

• Since: 1.6

• http://docs.oracle.com/javase/6/docs/api/java/util/ServiceLoader.html

– META-INF/services/javax.script.ScriptEngineFactory

– This file contains the single line:

de.torq.clojure.jsr223.ClojureScriptEngineFactory

• Clojure JSR 223 build.xml

– https://github.com/pmf/clojure-jsr223/blob/master/build.xml

19

<jar jarfile="${clojure_jsr223_bundle}" basedir="${build_osgi}"><service type="javax.script.ScriptEngineFactory“

provider="de.torq.clojure.jsr223.ClojureScriptEngineFactory"/>

Page 20: JavaScript on Java EE - Joker 2014 · Compiled from "helloToWorld.groovy" public class helloToWorld extends groovy.lang.Script{ public static java.lang.Long __timeStamp; public static

JSR-223 – script engines

20

Page 21: JavaScript on Java EE - Joker 2014 · Compiled from "helloToWorld.groovy" public class helloToWorld extends groovy.lang.Script{ public static java.lang.Long __timeStamp; public static

Nashorn

21

ECMAScript 5.1 compliant

Bundled with JDK 8

– Replaces Rhino

– Faster (2x – 10x)

– More secure

Seamless Java JavaScript interoperability

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

Page 22: JavaScript on Java EE - Joker 2014 · Compiled from "helloToWorld.groovy" public class helloToWorld extends groovy.lang.Script{ public static java.lang.Long __timeStamp; public static

Where does EE fit in?

22

Page 23: JavaScript on Java EE - Joker 2014 · Compiled from "helloToWorld.groovy" public class helloToWorld extends groovy.lang.Script{ public static java.lang.Long __timeStamp; public static

23

Page 24: JavaScript on Java EE - Joker 2014 · Compiled from "helloToWorld.groovy" public class helloToWorld extends groovy.lang.Script{ public static java.lang.Long __timeStamp; public static

Evolution of web

architecture

24

Page 25: JavaScript on Java EE - Joker 2014 · Compiled from "helloToWorld.groovy" public class helloToWorld extends groovy.lang.Script{ public static java.lang.Long __timeStamp; public static

25

ClientServer

Pre

se

nta

tio

n

(Se

rvle

t / JS

P)

En

terp

rise C

on

ne

ctivity

an

d B

usin

ess L

og

ic

• Request / Response

• Multi-Page Application

BrowserJava EE / JVM

Page 26: JavaScript on Java EE - Joker 2014 · Compiled from "helloToWorld.groovy" public class helloToWorld extends groovy.lang.Script{ public static java.lang.Long __timeStamp; public static

26

Pre

se

nta

tio

n

(Se

rvle

t/ J

SP,

JS

F)

Co

nn

ectivity

(RE

ST

SS

E)

En

terp

rise C

on

ne

ctivity

an

d B

usin

ess L

og

ic

Java EE / JVM

Multi-Page Application

In-page updates (AJAX)

JavaScript

BrowserClientServer

Page 27: JavaScript on Java EE - Joker 2014 · Compiled from "helloToWorld.groovy" public class helloToWorld extends groovy.lang.Script{ public static java.lang.Long __timeStamp; public static

27

Pre

se

nta

tio

n

(Se

rvle

t/ J

SP,

JS

F)

Co

nn

ectivity

(We

bS

ocke

t,

RE

ST, S

SE

)

En

terp

rise C

on

ne

ctivity

an

d B

usin

ess L

og

ic

Java EE / JVM

Single-page applications

View/Controller in browser

Model on server

Vie

w

Co

ntr

olle

r

JavaScript

BrowserClientServer

Page 28: JavaScript on Java EE - Joker 2014 · Compiled from "helloToWorld.groovy" public class helloToWorld extends groovy.lang.Script{ public static java.lang.Long __timeStamp; public static

Problems?

28

Page 29: JavaScript on Java EE - Joker 2014 · Compiled from "helloToWorld.groovy" public class helloToWorld extends groovy.lang.Script{ public static java.lang.Long __timeStamp; public static

29

• No reuse

• Duplicate implementations

• Mixed skills in projects

• Different cultures

• Communication overhead

• Client Team waits for Server

Team

• …

Page 30: JavaScript on Java EE - Joker 2014 · Compiled from "helloToWorld.groovy" public class helloToWorld extends groovy.lang.Script{ public static java.lang.Long __timeStamp; public static

Today?

30

Page 31: JavaScript on Java EE - Joker 2014 · Compiled from "helloToWorld.groovy" public class helloToWorld extends groovy.lang.Script{ public static java.lang.Long __timeStamp; public static

31

REST/SSE/WebSockets

JavaS

crip

t

Project-based end-to-end JavaScript

Rapid prototyping & API layer

Leverage backend resources

Aggregate & transform content

Return JSON to browser

Vie

w

Co

ntr

olle

r

JavaScript

Browser

ClientServer

Node

Java EE

Page 32: JavaScript on Java EE - Joker 2014 · Compiled from "helloToWorld.groovy" public class helloToWorld extends groovy.lang.Script{ public static java.lang.Long __timeStamp; public static

Tomorrow?

32

Page 33: JavaScript on Java EE - Joker 2014 · Compiled from "helloToWorld.groovy" public class helloToWorld extends groovy.lang.Script{ public static java.lang.Long __timeStamp; public static

33

ClientServer

En

terp

rise C

on

ne

ctivity

an

d B

usin

ess L

og

ic

Java EE / JVM

JavaS

cript

What if we could run JavaScript

alongside

Java EE in

the same JVM?

Vie

w

Co

ntr

olle

r

JavaScript

Browser

Page 34: JavaScript on Java EE - Joker 2014 · Compiled from "helloToWorld.groovy" public class helloToWorld extends groovy.lang.Script{ public static java.lang.Long __timeStamp; public static

34

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

JavaScript

java.lang.Thread

java.util.SortedSet

java.math.BigInteger

Node App

JVM Process

com.myorg.MyObj

require

(‘async’)

Page 35: JavaScript on Java EE - Joker 2014 · Compiled from "helloToWorld.groovy" public class helloToWorld extends groovy.lang.Script{ public static java.lang.Long __timeStamp; public static
Page 36: JavaScript on Java EE - Joker 2014 · Compiled from "helloToWorld.groovy" public class helloToWorld extends groovy.lang.Script{ public static java.lang.Long __timeStamp; public static

Your Ideas?

36