Slide 1

Slide 1 text

JavaScript on the Server Ringo JS Philipp Naderer @botic ! Web Developer bei ORF.at

Slide 2

Slide 2 text

dev.ORF.at ! @devORFat

Slide 3

Slide 3 text

JavaScript am Server (seit 1998)

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

RingoJS? • Anders als Node! • Blocking & Non-Blocking • JavaScript on the JVM • Multithreaded JavaScript • Basiert auf Mozilla Rhino • Module (CommonJS-Art) • CommonJS Module • JSGI Web Services

Slide 6

Slide 6 text

Scopes

Slide 7

Slide 7 text

Theads?!?! • Fundamentales Konzept der JVM • Threads sind nicht teuer in der JVM • Deadlocks & Race Conditions sind möglich • Shared Resources vermeiden • Ringo verwendet keine gewöhnlichen Threads

Slide 8

Slide 8 text

Philosophien • Event Loops (u.a. NodeJS)
 • Multi-Threading (traditionelles Java, C++, …)
 • Message Passing / Actors / Shared-Nothing- Threads / Workers / …

Slide 9

Slide 9 text

myWebApp
 Module myWebApp
 Module waiting running running Event Loop

Slide 10

Slide 10 text

Aber wenn man will … (function () { var x = false; // you will never write code like this … (new java.lang.Thread({ run: function() { x = true; } })).start(); ! while(x) { /* do nothing */ } ! return x; })();

Slide 11

Slide 11 text

Eher so … addToClasspath("./twitter4j.jar"); importPackage(Packages.twitter4j); ! exports.get("/stories/:id", function(id) { var story = store.query("…", {id: id}); ! var twtr = TwitterFactory().getInstance(); twtr.updateStatus(new StatusUpdate("Story#" + id); ! return env.renderResponse("page.html", { story: story } ); }; * das hier ist Demo-Code, einige Teile fehlen

Slide 12

Slide 12 text

Web Server serving 10.000 HTTP requests

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

Destructoring Assignments // Extract escapeHtml & isUrl properties // from ringo/utils/strings ! var {escapeHtml, isUrl} =
 require("ringo/utils/strings"); ! escapeHtml("

Foo

"); isUrl("so much url");

Slide 15

Slide 15 text

rp ~ npm • ringo-sqlstore (ORM) • reinhardt (Django-like templates) • gestalt (easy configuration) • stick (JSGI Web framework) • ringo-xom (XML) • ringo-ehcache (Caching) • ringo-mongodb • httpclient-jetty • underscore

Slide 16

Slide 16 text

rp benutzen •rp install •rp update •rp update •rp uninstall

Slide 17

Slide 17 text

JVM & Java? • Einfacher Zugriff auf Java-Libraries • var now = java.util.Calendar.getInstance(); • var y = new Packages.at.orf.MeineSuperKlasse() • Für Java gibt es fast alles: • ORM, J2EE, JDBC-Driver, Imaging, XML, I/O,
 Spring, LDAP, Apache Commons, …

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

Ringo on Google App Engine • Easy to integrate since Ringo runs on the JVM • No compiling needed, hot reload of scripts • Access all the Java APIs from GAE • Datastore API • Cloud SQL • Service APIs (OpenID, Images, …)

Slide 20

Slide 20 text

App Engine Demo // Note: packages prefixed with com.google. importPackage(appengine.api.users); importPackage(appengine.api.images); importPackage(appengine.api.datastore); importPackage(appengine.api.blobstore); ! var userService = UserServiceFactory.getUserService(); ! userService.isUserLoggedIn(); userService.createLoginURL(requestURL);

Slide 21

Slide 21 text

App Engine Demo var datastore = DatastoreServiceFactory.getDatastoreService(); var bs = new Entity("Bookshelf"); bs.setProperty("title", req.postParams.title); bs.setProperty("created", new java.util.Date()); bs.setProperty("creator", credentials.currentUser); ! datastore.put(bs); return response.redirect("/bookshelf/" + bs.getKey().getId());

Slide 22

Slide 22 text

App Engine Demo var book; try { book = datastore.get(bookKey); ! } catch (e if e.javaException instanceof EntityNotFoundException) { ! // return 404 - File not Found ! } catch (e) { // return 500 - Internal Error }

Slide 23

Slide 23 text

ImageService Blobstore Datastore User Auth / OpenID

Slide 24

Slide 24 text

botic/ringo-bookshelf Running demo on appspot.com:
 http://ringo-bookshelf.appspot.com http://goo.gl/5LECVy

Slide 25

Slide 25 text

• Deployment wie App Engine Java-SDK / WEB-INF • Ich habe ant für Dependency-Download genommen • App Engine SDK, Rhino, Ringo, JAFFL, JNR-POSIX • Ringo Core Modules • Packages (stick Web Framework, etc.) • In unter einer Stunde war alles erledigt!

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

Wahl-App „Wahl 13“

Slide 28

Slide 28 text

No content