網頁

2014年10月15日 星期三

node.js queue

redis:内部的列表支持对队列的操作,其中List的blpop是阻塞式的,满足消息队列的要求。


/kue



Kue Build Status NPM version Stories in Ready

Kue is a priority job queue backed by redis, built for node.js.
1.安裝 Redis
Redis is an open source, BSD licensed, advanced key-value cache and store. It is often referred to as a data structure server since keys can contain stringshasheslistssetssorted setsbitmaps and hyperloglogs.
[wins] 安裝 redis / bin / release / redis-2.8.17.zip View Raw
To run Redis with the default configuration just type:

    % cd src
    % ./redis-server

[wins] 執行 redis-server.exe

2. node.js 安裝 NPM kue
$ npm install kue

Implementing a Job Queue with Node.js


Before moving on, ensure that Redis is installed and a redis-server is running on your system.

Job queue for Node.js: kue

you can use the following syntax to create a job:
jobs.create('email', {
    title: 'welcome email for tj'
  , to: 'tj@learnboost.com'
  , template: 'welcome-email'
}).save();

Picking up a job to process is also really easy on Kue. On a different machine, connected to the same redis instance, you can do the following
jobs.process('email', function(job, done) {
  email({
    address: job.data.to,
    title: job.data.title,
    content: job.data.template
  }, done);
});
Notice all the data in the second argument of job.create() now resides in job.data.
用 NODE.JS 實作多個 PROCESS 監聽並處理同一個 PORT
master-worker  master.js  worker.js
啟動伺服器: [CMD] C:\nodejs>node master.js
[Chrome] http://127.0.0.1:8090/
Worker 1 or 2 or 3 or 4


(完成了一个可以工作的 HTTP 服务器。
接下来,打开浏览器访问 http://127.0.0.1:8090/,你会看到一个写着 "Worker 3"的网页。)

Implementing a Job Queue with Node.js

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。