13. Rhino
• built-in since Java6
• Oldies but Goodies
• Slooooow but Elegant
• 참고: https://
developer.mozilla.org/en-
US/docs/Mozilla/Projects/
Rhino
14. Nashorn
• built-in since Java8
• Fast & Easy
• try `jrunscript`
• JSR-223 Scripting API
• 참고: http://
openjdk.java.net/projects/
nashorn/
15. Hello World
with Nashorn
function greeting(name) {
return "hello," + name + "!";
}
ScriptEngine engine = new
ScriptEngineManager().getEngineByName("nashorn");
engine.eval(new FileReader("hello.js"));
Invocable invocable = (Invocable)engine;
Object result = invocable.invokeFunction(
"greeting", "World");
System.out.println(result);
16. React Isomorphic Rendering
with Nashorn
tomcatapache
webapp
(war)
MySQL
Nashorn
mod_jk
AJPHTTP
JVM
glue
script
ReactDOMServer.renderToString()
자바 기반 템플릿 엔진
17. React Isomorphic Rendering
with Nashorn
function renderToString(type, props) {
return ReactDOMServer.renderToString(type, props);
}
<div id="main">${reactHtml}</div>
<script src="bundle.js"></script>
$.getJSON('/apis/v1/comments', function (data) {
ReactDOM.render(<CommentList comments={ data }/>,
document.getElementById('main'));
});
22. ScriptTemplateView
with ejs/Nashorn
var _compiledTemplates = {};
function render(template, model, url) {
var compiledTemplate = templates[url];
if(!_compiledTemplates[url]) {
_compiledTemplates[url] = compiledTemplate
= ejs.compile(template);
}
return compiledTemplate(toJsonObject(model));
}
function toJsonObject(javaObj) {
var jsonObj = {};
for (var k in javaObj) {
if(javaObj[k] instanceof Java.type("java.lang.Iterable")) {
jsonObj[k] = Java.from(javaObj[k]);
} else {
jsonObj[k] = javaObj[k];
}
}
return jsonObj;
}
23. ScriptTemplateView
with ejs/Nashorn
@Configuration
public class EjsConfig {
@Bean
public ScriptTemplateConfigurer scriptTemplateConfigurer() {
ScriptTemplateConfigurer bean = new ScriptTemplateConfigurer();
bean.setEngineName("nashorn");
bean.setScripts(
"/server-scripts/nashorn-polyfill.js",
"/META-INF/resources/webjars/ejs/2.4.1/ejs-v2.4.1/ejs.min.js",
"/server-scripts/nashorn-ejs-render.js"
);
bean.setRenderFunction("render");
bean.setSharedEngine(true);
return bean;
}
@Bean
public ViewResolver scriptTemplateViewResolver() {
ScriptTemplateViewResolver bean = new ScriptTemplateViewResolver();
bean.setPrefix("classpath:/templates/ejs/");
bean.setSuffix(".ejs");
return bean;
}
}
24. Node.js
• Polyglot, MSA or Hype?
• Fast & Easy
• but Another World
• npm에 좋은거 많다던데…
• 참고: https://nodejs.org
25. React Isomorphic Rendering
with Node.js
tomcatapache
webapp
(war)
MySQL
express
mod_jk
AJPHTTP
JVM
app.js
ReactDOMServer.renderToString()
자바 기반 템플릿 엔진
HTTP
Node.js
HTTP
28. J2V8
• Open Source Java bindings
for V8
• Licensed under the EPL
• Faaaaast Fresh!
• Run on Java6
• not only V8 but also NodeJS
• Linux, Windows, Mac OS X,
Android ARM, Android x86
• 참고: https://github.com/
eclipsesource/J2V8
29. J2V8 - TODO
• Marshalling
• JNI performance bottleneck
• Memory Management & GC
• Exception Handling
• Debugger Integration
• JSR-223 Scripting Engine
• Best Practice & Antipatterns
30. Rhino vs Nashorn vs V8
출처: https://ariya.io/2014/03/nashorn-the-new-rhino-on-the-block
31. Hello World
with V8/J2V8
V8 v8 = V8.createV8Runtime();
v8.executeScript(FileUtils.readFileToString("hello.js"));
V8Array args = new V8Array(v8).push("World");
String result = v8.executeStringFunction("greeting", args);
System.out.println(result);
args.release();
v8.release();
function greeting(name) {
return "Hello," + name + "!";
}
32. Hello World
with NodeJS/J2V8
NodeJS nodeJS = NodeJS.createNodeJS();
V8Object hello = nodeJS.require("./hello");
V8Array args = new
V8Array(hello.getRuntime()).push("World");
String result = hello.executeJSFunction("greeting", args);
while (nodeJS.isRunning()) {
nodeJS.handleMessage();
}
System.out.println(result);
args.release();
hello.release();
nodeJS.release();
function greeting(name) {
return "Hello," + name + "!";
}
33. Putting it all togethter
with J2V8
tomcatapache
webapp
(war)
MySQL
J2V8
mod_jk
AJPHTTP
JVM
server &
shared
scripts
ReactDOMServer.renderToString()
자바스크립트 기반 템플릿 엔진
JNI
npm
37. ab on my machine
• ab -n 100 -c 4
• MacBook Pro (Retina, 15-
inch, Mid 2014)
• OS X El Capitan 10.11.6
• 2.5 GHz Intel Core i7, 16GB
1600 MHz DDR3, 500GB
Apple SSD
38. THE TRUTH IS
OUT THERE
0
75
150
225
300
Velocity Freemarker Thymeleaf JSP ScriptTemplateView ejs handlebars J2V8TemplateView ejs
Server Only React Nashorn J2V8 Custom J2V8
39. THE TRUTH IS
OUT THERE
Server Only
React Custom
Nashorn J2V8 J2V8
Velocity 248.15 24.91 83.98 -
Freemarker 216.40 24.44 82.59 -
Thymeleaf 138.60 25.21 89.73 -
JSP 117.05 24.88 93.10 -
Script
TemplateView
ejs 84.40 30.10 86.39 -
handlebars 26.59 38.56 95.62 -
J2V8
TemplateView
ejs 102.65 25.37 207.70 280.90