Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Concurrent SSJS on the JVM

Concurrent SSJS on the JVM

Presentation from the Fronteers Jam Session 2012 in Amsterdam.

Philipp Naderer

February 19, 2014
Tweet

More Decks by Philipp Naderer

Other Decks in Programming

Transcript

  1. RingoJS • JavaScript on the JVM • Based on Mozilla

    Rhino • Part of Java SE 6 and Java SE 7 • Successor / Rewrite of Helma • 14 years of SSJS experience!
  2. Features • CommonJS modules & packages • Full access to

    Java libraries • ECMAScript 5 and JS 1.8 support • Lots of „ringo/“ modules • dates, http, concurrency, logging, subprocesses, jsgi, command line, ...
  3. Threads and SSJS? • Ringo‘s concept: Workers • Implicit multithreaded

    code • You can use an EventLoop • No shared global object* • Singletons instead of shared data
  4. 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>Servas Amsterdam!</h1>"); }); ! if (require.main == module) { require("ringo/httpserver").main(module.id); }
  5. „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)
  6. 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. }
  7. 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 }); }
  8. RingoJS infos • ringojs.org – basic information • github.com/ringojs –

    source + examples • @ringojs – official account • @hannesw – Hannes‘ account • IRC: irc.freenode.net #ringojs