最近 node.js 來勢洶洶, 怎麼辦? 別怕, 我們也有秘密武器 ringojs!
Post on 18-Jun-2015
1.782 Views
Preview:
DESCRIPTION
TRANSCRIPT
頑皮工坊 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 2010Projects:
● 東森大選即時開票系統,締造兩千兩百萬 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 maturaty2. Community size3. Library base4. Java interopability5. Single-thread vs. Multi-thread
Node.js vs RingoJS
● VM optimize for browser● Young community● Not much library but will grow● No Java interoperability● Single thread
● Quite mature enterprise grade VM● Big community● Lots of Java library● Very good Java interoperability● 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 classesjs> 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.txtjs> f.exists()truejs> 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) }existsparentFilemkdirtoStringwait
特異功能 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.jsvar 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.jsexports.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();
MongodbstoreRetrieving 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.js19 Aug 16:13:33 - Score (version 5): 4090
$ java -Xmx265m -jar js.jar run.jsScore (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!
top related