頑皮工坊
Sleepnova Inc. 創
 辦人暨執行長
     周立瑋
自我介紹

頑皮工坊 Sleepnova Inc. 創辦人暨執行長,KKBOX Android
技術顧問。
(Android 與雲端技術顧問公司)

感興趣的領域:
- 敏捷開發,尋找更符合人性的程式設計典範。
- 可應付高承載的延展性架構。
- 思考資訊人為社會做點什麼?
過去經歷

Talks:
 ● Javascript take-off to the clouds - Server-side Javascript

   @ COSCUP 2009
 ● The NoSQL movement: CouchDB as an example @

   COSCUP 2010
Projects:
 ● 東森大選即時開票系統,締造兩千兩百萬 req / 4 hr 記錄
最近 node.js 來勢洶洶, 怎麼辦? 別
 怕, 我們也有秘密武器 RingoJS!
簡介



簡介 RingoJS 的特色,如何使用 RingoJS 在 JVM
上面快速開發,以及如何在專案中同時保有
scripting 的靈活度以及使用既有的 Java Library。
Why JavaScript
The native language of
       the web
JavaScript 到底是不是個語言?
About JavaScript

●   Javascript vs. Java <恩怨情仇>
●   JavaScript 不能拿來寫大系統?
●   CommonJS module system
RingoJS overview

●   Developed by Hannes Wallnöfer (hns.github.com)
●   Based on Mozilla Rhino
●   Runs on JVM
●   Orignal named HelmaNG
●   Follow CommonJS
Node.js vs RingoJS

1.   VM maturaty
2.   Community size
3.   Library base
4.   Java interopability
5.   Single-thread vs. Multi-thread
Node.js vs RingoJS

●   VM optimize for browser          ●   Quite mature enterprise grade VM
●   Young community                  ●   Big community
●   Not much library but will grow   ●   Lots of Java library
●   No Java interoperability         ●   Very good Java interoperability
●   Single thread                    ●   Multi-thread
Rhino JavaScript Engine

●   JavaScript <-> Java inter-operation
     ○ Access Java packages and classes

     ○ Working with Java objects

     ○ Inherit class/interface

●   E4X
Accessing Java packages and
classes
js> Packages.java
[JavaPackage java]

js> java
[JavaPackage java]

js> java.io.File
[JavaClass java.io.File]

js> importPackage(java.io)
js> File
[JavaClass java.io.File]
Working with Java objects


js> new java.util.Date()
Thu Jan 24 16:18:17 EST 2002

var File = java.io.File;
var myfile = new File("sample.txt");
Working with Java objects


js> f = new java.io.File("test.txt")
test.txt
js> f.exists()
true
js> f.getName()
test.txt
Implementing Java Interfaces

js> obj = { run: function () { print("nrunning"); } }
[object Object]
js> obj.run()

running

js> r = new java.lang.Runnable(obj);
[object JavaObject]
Implementing Java Interfaces



js> r = new java.lang.Runnable({
   run: function () { print("nrunning"); }
   });
[object JavaObject]
Working with Java objects


  js> for (i in f) { print(i) }
  exists
  parentFile
  mkdir
  toString
  wait
特異功能 E4X
var sales = <sales vendor="John">
  <item type="peas" price="4" quantity="6"/>
  <item type="carrot" price="3" quantity="10"/>
  <item type="chips" price="5" quantity="3"/>
 </sales>;

alert( sales.item.(@type == "carrot").@quantity );
alert( sales.@vendor );
for each( var price in sales..@price ) {
  alert( price );
}
delete sales.item[0];
sales.item += <item type="oranges" price="4"/>;
sales.item.(@type == "oranges").@quantity = 4;
RingoJS


●   Minimal web app
●   Packages
●   Storage
●   AppEngine
Hello World!
exports.app = function(req) {
   return {
      status: 200,
      headers: {"Content-Type": "text/plain"},
      body: ["Hello World!"]
   };
};

if (require.main == module)
    require("ringo/httpserver").main(module.id);
Ringo Shell



>> var fs = require('fs');
>> var file = fs.open('README.md');
>> var lines = [line for (line in file)];
Handle HTTP Request
// actions.js
var Response = require('ringo/webapp/response');
var model = require('./model');

exports.index = function(req) {
   var posts = model.Post.query().select().slice(0,10);
   return Response.skin('skins/index.html', {
       posts: posts,
   });
};
Skins


// in a skin
<% for post in <% posts %> render 'postOverview' %>

<% subskin 'postOverview' %>
  <h2><% post.title %></h2>
URL mapping



// config.js
exports.urls = [
    ['/', './actions'],
];
Database


●   ringo-sqlstore
●   berkeleystore
●   cassandrastore
●   mongodbstore
●   redis-ringojs-client
●   Google App Engine Datastore API
Mongodbstore
Initializing the store:
include('ringo/storage/mongodbstore');
store = new Store('server', 27017, 'dbName');

Creating a new Storable class:
Book = store.defineEntity('book');

Creating and saving a new Storable instance:
var b = new Book({title: "DBs for dummies"});
b.save();
Mongodbstore
Retrieving all objects from a db:
var books = Book.all();

Retrieving an object by id:
var book = Book.get(id);

Deleting an object from the db:
book.remove();

Running a query on the database:
Book.query().equals('prop', value).select();
Berkeleystore
Initializing the store:
include('ringo/storage/berkeleystore');
store = new Store(dbpath);

Creating a new Storable class:
Book = store.defineEntity('book');

Creating and saving a new Storable instance:
var b = new Book({title: "DBs for dummies"});
b.save();
天下沒有白痴的午餐
天下沒有白吃的午餐
Performance characteristic

●   Translation to JVM bytecode
●   JVM is not a perfect dynamic language VM
    yet!
Performance characteristic

$ node v8bench.js
19 Aug 16:13:33 - Score (version 5): 4090

$ java -Xmx265m -jar js.jar run.js
Score (version 5): 120

[larger is better]
~1/34
Node.js vs RingoJS GC Benchmark




                   http://hns.github.com/2010/09/29/benchmark2.html
Future of JavaScript Engine

JavaScript engine
● Nashorn, dyn.js
● InvokeDynamic
JavaScript territory

●   Titanium
●   CouchDB
●   Unity
●   node.js + webkit
●   RingoJS + SWT
●   ES operating system
●   Ubuntu Desktop
Stay Hungry, Stay Foolish!

最近 node.js 來勢洶洶, 怎麼辦? 別怕, 我們也有秘密武器 RingoJS!

  • 1.
    頑皮工坊 Sleepnova Inc. 創 辦人暨執行長 周立瑋
  • 2.
    自我介紹 頑皮工坊 Sleepnova Inc.創辦人暨執行長,KKBOX Android 技術顧問。 (Android 與雲端技術顧問公司) 感興趣的領域: - 敏捷開發,尋找更符合人性的程式設計典範。 - 可應付高承載的延展性架構。 - 思考資訊人為社會做點什麼?
  • 3.
    過去經歷 Talks: ● Javascripttake-off to the clouds - Server-side Javascript @ COSCUP 2009 ● The NoSQL movement: CouchDB as an example @ COSCUP 2010 Projects: ● 東森大選即時開票系統,締造兩千兩百萬 req / 4 hr 記錄
  • 4.
    最近 node.js 來勢洶洶,怎麼辦? 別 怕, 我們也有秘密武器 RingoJS!
  • 5.
    簡介 簡介 RingoJS 的特色,如何使用RingoJS 在 JVM 上面快速開發,以及如何在專案中同時保有 scripting 的靈活度以及使用既有的 Java Library。
  • 6.
  • 7.
  • 8.
  • 9.
    About JavaScript ● Javascript vs. Java <恩怨情仇> ● JavaScript 不能拿來寫大系統? ● CommonJS module system
  • 10.
    RingoJS overview ● Developed by Hannes Wallnöfer (hns.github.com) ● Based on Mozilla Rhino ● Runs on JVM ● Orignal named HelmaNG ● Follow CommonJS
  • 11.
    Node.js vs RingoJS 1. VM maturaty 2. Community size 3. Library base 4. Java interopability 5. Single-thread vs. Multi-thread
  • 12.
    Node.js vs RingoJS ● VM optimize for browser ● Quite mature enterprise grade VM ● Young community ● Big community ● Not much library but will grow ● Lots of Java library ● No Java interoperability ● Very good Java interoperability ● Single thread ● Multi-thread
  • 13.
    Rhino JavaScript Engine ● JavaScript <-> Java inter-operation ○ Access Java packages and classes ○ Working with Java objects ○ Inherit class/interface ● E4X
  • 14.
    Accessing Java packagesand classes js> Packages.java [JavaPackage java] js> java [JavaPackage java] js> java.io.File [JavaClass java.io.File] js> importPackage(java.io) js> File [JavaClass java.io.File]
  • 15.
    Working with Javaobjects js> new java.util.Date() Thu Jan 24 16:18:17 EST 2002 var File = java.io.File; var myfile = new File("sample.txt");
  • 16.
    Working with Javaobjects js> f = new java.io.File("test.txt") test.txt js> f.exists() true js> f.getName() test.txt
  • 17.
    Implementing Java Interfaces js>obj = { run: function () { print("nrunning"); } } [object Object] js> obj.run() running js> r = new java.lang.Runnable(obj); [object JavaObject]
  • 18.
    Implementing Java Interfaces js>r = new java.lang.Runnable({ run: function () { print("nrunning"); } }); [object JavaObject]
  • 19.
    Working with Javaobjects js> for (i in f) { print(i) } exists parentFile mkdir toString wait
  • 20.
    特異功能 E4X var sales= <sales vendor="John"> <item type="peas" price="4" quantity="6"/> <item type="carrot" price="3" quantity="10"/> <item type="chips" price="5" quantity="3"/> </sales>; alert( sales.item.(@type == "carrot").@quantity ); alert( sales.@vendor ); for each( var price in sales..@price ) { alert( price ); } delete sales.item[0]; sales.item += <item type="oranges" price="4"/>; sales.item.(@type == "oranges").@quantity = 4;
  • 21.
    RingoJS ● Minimal web app ● Packages ● Storage ● AppEngine
  • 22.
    Hello World! exports.app =function(req) { return { status: 200, headers: {"Content-Type": "text/plain"}, body: ["Hello World!"] }; }; if (require.main == module) require("ringo/httpserver").main(module.id);
  • 23.
    Ringo Shell >> varfs = require('fs'); >> var file = fs.open('README.md'); >> var lines = [line for (line in file)];
  • 24.
    Handle HTTP Request //actions.js var Response = require('ringo/webapp/response'); var model = require('./model'); exports.index = function(req) { var posts = model.Post.query().select().slice(0,10); return Response.skin('skins/index.html', { posts: posts, }); };
  • 25.
    Skins // in askin <% for post in <% posts %> render 'postOverview' %> <% subskin 'postOverview' %> <h2><% post.title %></h2>
  • 26.
    URL mapping // config.js exports.urls= [ ['/', './actions'], ];
  • 27.
    Database ● ringo-sqlstore ● berkeleystore ● cassandrastore ● mongodbstore ● redis-ringojs-client ● Google App Engine Datastore API
  • 28.
    Mongodbstore Initializing the store: include('ringo/storage/mongodbstore'); store= new Store('server', 27017, 'dbName'); Creating a new Storable class: Book = store.defineEntity('book'); Creating and saving a new Storable instance: var b = new Book({title: "DBs for dummies"}); b.save();
  • 29.
    Mongodbstore Retrieving all objectsfrom a db: var books = Book.all(); Retrieving an object by id: var book = Book.get(id); Deleting an object from the db: book.remove(); Running a query on the database: Book.query().equals('prop', value).select();
  • 30.
    Berkeleystore Initializing the store: include('ringo/storage/berkeleystore'); store= new Store(dbpath); Creating a new Storable class: Book = store.defineEntity('book'); Creating and saving a new Storable instance: var b = new Book({title: "DBs for dummies"}); b.save();
  • 31.
  • 32.
  • 33.
    Performance characteristic ● Translation to JVM bytecode ● JVM is not a perfect dynamic language VM yet!
  • 34.
    Performance characteristic $ nodev8bench.js 19 Aug 16:13:33 - Score (version 5): 4090 $ java -Xmx265m -jar js.jar run.js Score (version 5): 120 [larger is better]
  • 35.
  • 36.
    Node.js vs RingoJSGC Benchmark http://hns.github.com/2010/09/29/benchmark2.html
  • 37.
    Future of JavaScriptEngine JavaScript engine ● Nashorn, dyn.js ● InvokeDynamic
  • 38.
    JavaScript territory ● Titanium ● CouchDB ● Unity ● node.js + webkit ● RingoJS + SWT ● ES operating system ● Ubuntu Desktop
  • 39.