Niko Köbler (@dasniko)
{JavaScript}Training
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
L1-cache 3 cycles L2-cache 14 cycles RAM 250 cycles Disk 41 000 000 cycles Network 240 000 000 cycles
(undefined is not a function!)
Like it or not, JavaScript is everywhere these days - from browser to server to mobile - and now you, too, need to learn the language or dive deeper than you have.
Dr. Axel Rauschmayer
In four years ... Node.js has experienced phenomenal growth. Node.js is the language of choice for high performance, low latency applications and has been powering everything from robots to API engines to cloud stacks to mobile web sites.
closures, collections & for each, multi-line string literals, string interpolation, __noSuchProperty__, __noSuchMethod__, typed arrays, binding properties, error extensions, conditional catch clause, String functions, and many, many more...
...are similar than car and carpet are similar.
Command Line Client
$ $JAVA_HOME/bin/jjs
jjs> print('Hello Nashorn!');
Invoking JavaScript from Java
ScriptEngine engine = new ScriptEngineManager()
.getEngineByName("nashorn");
engine.eval("print('Hello Nashorn!');");
engine.eval(new FileReader("scriptfile.js"));
Invocable invocable = (Invocable) engine;
Object result = invocable.invokeFunction(
"jsSayHello", "Nashorn");
Invoking Java from JavaScript
package my.package;
public class MyJavaClass {
static String sayHello(String name) {
return String.format("Hello %s from Java!", name);
}
}
var MyJavaClass = Java.type('my.package.MyJavaClass');
var result = MyJavaClass.sayHello('Nashorn');
print(result); // Hello Nashorn from Java!
Listing Docker Containers via REST-Call from Shell
#!/usr/bin/jjs -fv
var host = "DOCKER_HOST"
var dockerUri="http://${host}:5555/containers/json";
var command = "curl ${dockerUri}";
$EXEC(command);
var containers = JSON.parse($OUT);
for each(container in containers){
print("${container.Image} /
${container.Names[0]} /
${container.Status}");
}
(by Adam Bien)
Inter-thread communication
Message Bus
Shared State (Map API: Key-Value-Store)
Model-Store-API
JPA (Eclipselink) / JDBC based
Netty is an asynchronous event-driven network application framework for rapid development of maintainable high performance protocol servers & clients.
more JavaScript than Node.js
Vert.x is a lightweight, high performance application platform for the JVM that's designed for modern mobile, web, and enterprise applications.
*) Java, JavaScript, Ruby, Groovy, Python, Scala, Clojure, Ceylon
**) using Hazelcast In-Memory Data Grid (hazelcast.org)
This module exposes the vert.x 2.x eventbus to node.js clients.
This supports running Node.js workloads inside of Vert.x.
var http = require("http");
var vertx = require("vertx2-core");
var registration = vertx.eventbus.register("bar", function(message) {
var amount = message.body.amount;
console.log("BAR: Someone ordered " + amount + " beer(s).");
message.reply({wait_time: amount * 1.75});
});
var server = http.createServer(function(request, response) {
var parts = request.url.split("/");
var amount = parts[1];
vertx.eventbus.send("bar", {amount: amount}, function(message) {
response.write(amount + " beer(s) will be ready in "
+ message.body.wait_time + " minutes");
response.end();
});
});
server.listen(9000, function() {
console.log( "Beer-Server is listening on port 9000" );
});
beer-as-a-service.js
run with nodyn
var http = require("http");
var vertx = require("vertx2-core");
var server = http.createServer(function(request, response) {
var parts = request.url.split("/");
var amount = parts[1];
vertx.eventbus.send("bar", {amount: amount}, function(message) {
response.write(amount + " beer(s) will be ready in "
+ message.body.wait_time + " minutes");
response.end();
});
});
server.listen(9000, function() {
console.log( "Beer-Server is listening on port 9000" );
});
beer-web.js
beer-bar.js
var eventBus = require("vertx/event_bus");
eventBus.registerHandler("bar", function(message, replier) {
java.lang.System.err.println("BAR: Someone ordered " + message.amount + " beer(s)");
replier({wait_time: message.amount * 1.75});
});
java.lang.System.err.println("The BAR is open!");
run with vertx in cluster mode