JAVASCRIPT, AND JAVA ME REACT.JS APPS WITH JAVA ......APPS SOUND AWESOME... ISOMORPHIC I'M IN A...

41
JAVASCRIPT, AND .JS APPS WITH JAVA 1.0 Niko Köbler Soʿware-Architect, Developer & Trainer | | JAVA ME REACT MVC [email protected] www.n-k.de @dasniko

Transcript of JAVASCRIPT, AND JAVA ME REACT.JS APPS WITH JAVA ......APPS SOUND AWESOME... ISOMORPHIC I'M IN A...

Page 4: JAVASCRIPT, AND JAVA ME REACT.JS APPS WITH JAVA ......APPS SOUND AWESOME... ISOMORPHIC I'M IN A ENVIRONMENT! And I don't want to have more complicated deployments and performance overhead
Page 5: JAVASCRIPT, AND JAVA ME REACT.JS APPS WITH JAVA ......APPS SOUND AWESOME... ISOMORPHIC I'M IN A ENVIRONMENT! And I don't want to have more complicated deployments and performance overhead

ISOMORPHIC JAVASCRIPTIf you look at the same entity ( ) in two

different contexts ( ), you should get

the same thing ( ).

code

client & server

result, html, DOM, ...

Page 6: JAVASCRIPT, AND JAVA ME REACT.JS APPS WITH JAVA ......APPS SOUND AWESOME... ISOMORPHIC I'M IN A ENVIRONMENT! And I don't want to have more complicated deployments and performance overhead
Page 7: JAVASCRIPT, AND JAVA ME REACT.JS APPS WITH JAVA ......APPS SOUND AWESOME... ISOMORPHIC I'M IN A ENVIRONMENT! And I don't want to have more complicated deployments and performance overhead

WHY ON CLIENT AND SERVER!?

DRY principleshare same logicone codebase / maintenancesingle point of truth (or failure)

single technology

SAME CODE

Page 9: JAVASCRIPT, AND JAVA ME REACT.JS APPS WITH JAVA ......APPS SOUND AWESOME... ISOMORPHIC I'M IN A ENVIRONMENT! And I don't want to have more complicated deployments and performance overhead
Page 11: JAVASCRIPT, AND JAVA ME REACT.JS APPS WITH JAVA ......APPS SOUND AWESOME... ISOMORPHIC I'M IN A ENVIRONMENT! And I don't want to have more complicated deployments and performance overhead
Page 12: JAVASCRIPT, AND JAVA ME REACT.JS APPS WITH JAVA ......APPS SOUND AWESOME... ISOMORPHIC I'M IN A ENVIRONMENT! And I don't want to have more complicated deployments and performance overhead

I'M IN A ENVIRONMENT!

And I don't want to have more complicated deployments and

performance overhead of interacting with external Node

processes!

JAVA

Page 14: JAVASCRIPT, AND JAVA ME REACT.JS APPS WITH JAVA ......APPS SOUND AWESOME... ISOMORPHIC I'M IN A ENVIRONMENT! And I don't want to have more complicated deployments and performance overhead

NASHORN &

Browsers: no simultaneous execution of JavaScript codeNashorn itself is not thread-safe by designThread-safety depends on your code!Use a ThreadLocal<ScriptEnginge> when your JScode is not thread-safe (i.e. React.js, Handlebars, etc.)

CONCURRENCY

Page 15: JAVASCRIPT, AND JAVA ME REACT.JS APPS WITH JAVA ......APPS SOUND AWESOME... ISOMORPHIC I'M IN A ENVIRONMENT! And I don't want to have more complicated deployments and performance overhead

REACT.invented by Facebook

component basednot a full-stack frameworkjust the "V" in MVCvirtual DOM (updates via diffs - no flickering)supports server-side rendering

JS"JavaScript library for building user interfaces"

http://reactjs.org

Page 17: JAVASCRIPT, AND JAVA ME REACT.JS APPS WITH JAVA ......APPS SOUND AWESOME... ISOMORPHIC I'M IN A ENVIRONMENT! And I don't want to have more complicated deployments and performance overhead

JS

JS

Xclass Book extends React.Component { render() { return ( <div className="book"> <h3>{this.props.author}</h3> <div>{this.props.title}</div> </div> ); } }

var Book = React.createClass({displayName: "Book", render: function () { return ( React.createElement("div", {className: "book"}, React.createElement("h3", null, this.props.author), React.createElement("div", null, this.props.title) )); } });

Page 18: JAVASCRIPT, AND JAVA ME REACT.JS APPS WITH JAVA ......APPS SOUND AWESOME... ISOMORPHIC I'M IN A ENVIRONMENT! And I don't want to have more complicated deployments and performance overhead

HTML

Transpile JSX to JS with Babel

<div class="book" data-reactid=".1.$0"> <h3 data-reactid=".1.$0.0">George Orwell</h3> <div class="lead" data-reactid=".1.$0.1">1984</div> </div>

https://babeljs.io

Page 19: JAVASCRIPT, AND JAVA ME REACT.JS APPS WITH JAVA ......APPS SOUND AWESOME... ISOMORPHIC I'M IN A ENVIRONMENT! And I don't want to have more complicated deployments and performance overhead
Page 20: JAVASCRIPT, AND JAVA ME REACT.JS APPS WITH JAVA ......APPS SOUND AWESOME... ISOMORPHIC I'M IN A ENVIRONMENT! And I don't want to have more complicated deployments and performance overhead

MVC 1.0

Page 21: JAVASCRIPT, AND JAVA ME REACT.JS APPS WITH JAVA ......APPS SOUND AWESOME... ISOMORPHIC I'M IN A ENVIRONMENT! And I don't want to have more complicated deployments and performance overhead

MVC 1.0Action-based Web-Framework,

formerly part of Java EE 8

JSR-371

OZARK (RI)

SPEC LEAD TRANSFER

Oracle -> Ivar Grimstad

https://jcp.org/en/jsr/detail?id=371https://mvc-spec.java.net/

https://ozark.java.net/

Page 22: JAVASCRIPT, AND JAVA ME REACT.JS APPS WITH JAVA ......APPS SOUND AWESOME... ISOMORPHIC I'M IN A ENVIRONMENT! And I don't want to have more complicated deployments and performance overhead

OZARK-ReactJS-based ViewEngine for MVC 1.0

REACThttps://github.com/dasniko/ozark-react

Page 35: JAVASCRIPT, AND JAVA ME REACT.JS APPS WITH JAVA ......APPS SOUND AWESOME... ISOMORPHIC I'M IN A ENVIRONMENT! And I don't want to have more complicated deployments and performance overhead

MVC REACT CONTROLLER@Controller @Path("/react") public class ReactController { @Inject private Models models;

@Inject private BookService service;

@GET public String index() throws Exception { List<Book> books = service.getBooks(); models.put("data", books); return "react:react.jsp?function=renderServer"; } }

https://github.com/dasniko/ozark-react/blob/master/src/main/java/dasniko/ozark/react/ReactController.java

Page 36: JAVASCRIPT, AND JAVA ME REACT.JS APPS WITH JAVA ......APPS SOUND AWESOME... ISOMORPHIC I'M IN A ENVIRONMENT! And I don't want to have more complicated deployments and performance overhead

REACT ENGINEVIEWpublic class ReactViewEngine extends ServletViewEngine { @Override public void processView(ViewEngineContext context) throws ViewEngineException // parse view and extract the actual template and the react.js function to call String view = context.getView(); // react:react.jsp?function=renderServer String template = view.substring("react:".length(), view.indexOf("?" String function = view.substring(view.indexOf("function=") + 9);

// get "data" from model Models models = context.getModels(); Object data = models.get("data");

// call given js function on data String content = react.render(function, data);

// and put results as string in model models.put("content", content); models.put("data", mapper.writeValueAsString(data));

// create a new context with the actual view and forward to ServletViewEngine ViewEngineContext ctx = new ViewEngineContextImpl(template, models, ...);

try { forwardRequest(ctx, "*.jsp", "*.jspx"); } catch (ServletException | IOException e) {

Page 37: JAVASCRIPT, AND JAVA ME REACT.JS APPS WITH JAVA ......APPS SOUND AWESOME... ISOMORPHIC I'M IN A ENVIRONMENT! And I don't want to have more complicated deployments and performance overhead

REACT RENDERERprivate ThreadLocal<ScriptEngine> engineHolder = ThreadLocal.withInitial(() -> { ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn" engine.eval(read("nashorn-polyfill.js")); engine.eval(read("app.js")); return engine; )};

public String render(String function, Object object) { Object html = engineHolder.get().invokeFunction(function, object); return String.valueOf(html); }

https://github.com/dasniko/ozark-react/blob/master/src/main/java/dasniko/ozark/react/React.java

Page 38: JAVASCRIPT, AND JAVA ME REACT.JS APPS WITH JAVA ......APPS SOUND AWESOME... ISOMORPHIC I'M IN A ENVIRONMENT! And I don't want to have more complicated deployments and performance overhead

JS CODEX... var renderClient = function (books) { var data = books || []; React.render( <BookBox data={data} url='books.json' pollInterval={5000} />, document.getElementById("content") ); };

var renderServer = function (books) { var data = Java.from(books); return React.renderToString( <BookBox data={data} url='books.json' pollInterval={5000} /> ); };

https://github.com/dasniko/ozark-react/blob/master/src/main/resources/jsx/index.jsx

Page 39: JAVASCRIPT, AND JAVA ME REACT.JS APPS WITH JAVA ......APPS SOUND AWESOME... ISOMORPHIC I'M IN A ENVIRONMENT! And I don't want to have more complicated deployments and performance overhead

JSP

https://github.com/dasniko/ozark-react/blob/master/src/main/webapp/WEB-INF/views/react.jsp

Page 40: JAVASCRIPT, AND JAVA ME REACT.JS APPS WITH JAVA ......APPS SOUND AWESOME... ISOMORPHIC I'M IN A ENVIRONMENT! And I don't want to have more complicated deployments and performance overhead

WHAT ?ES6 (Java 9)TypeScriptAngular Universal supportRouting

NEXT

Page 41: JAVASCRIPT, AND JAVA ME REACT.JS APPS WITH JAVA ......APPS SOUND AWESOME... ISOMORPHIC I'M IN A ENVIRONMENT! And I don't want to have more complicated deployments and performance overhead

THANK !ANY ?

Slides:

Niko Köbler So�ware-Architect, Developer & Trainer

| |

YOUQUESTIONS

bit.ly/isomorphic-javaland

[email protected] www.n-k.de @dasniko

https://github.com/dasniko/ozark-react