Helma and RingoJS allow running JavaScript on the Java Virtual Machine by using Mozilla Rhino, with RingoJS being the successor to Helma; RingoJS provides features like CommonJS modules, full access to Java libraries, multithreaded programming using Workers without shared globals, and modules for tasks like HTTP, concurrency, and command line usage; examples are shown for calculating Fibonacci numbers across Workers and building a web server application using RingoJS and the stick framework.
5. Multithreaded JS?
• Ringo‘s concept: Workers
• You can still use an EventLoop
• No shared global object*
• Singletons instead of shared data
6. Ringo Workers
• Look like Web Workers in Browsers
• No JSON serialization needed = faster!
• Send a message with postMessage(e)
• React on message with onMessage(e)
• Return the result to the caller with
postMessage(e)
7. fibobuster.js
1. var {Worker} = require("ringo/worker");
2.
3. // Create 50 Workers = 50 Threads
4. for (var i = 50; i < 100; i++) {
5. var w = new Worker(module.resolve("./fibonacci"));
6.
7. // Callback for the output
8. w.onmessage = function(returnObj) {
9. console.log("Result: " + returnObj.data.result);
10. }
11.
12. // Start with the calculation
13. w.postMessage(i);
14. }
8. fibonacci.js
function onmessage(message) {
var limit = message.data;
console.log("Fibonacci #" + limit);
/* Calculate the fibonacci number */
var a = ..., b = ..., c = ...;
for (...) { ... }
message.source.postMessage({ result: c });
}
9. webserver.js
var response = require("ringo/jsgi/response");
var {Application} = require("stick");
var app = exports.app = new Application();
// Configure stick middlewares
app.configure("notfound", "route");
app.get("/", function(request) {
return response.html("<h1>I‘m a todo app!</h1>");
});
if (require.main == module) {
require("ringo/httpserver").main(module.id);
}