Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Amqp properties 2 #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var InputBufferUnderrunError = thrift.InputBufferUnderrunError;
* @returns {object} - The Apache Thrift Multiplex Server.
*/

function AMQPServer(processor, _options) {
function AMQPServer(processor_factory, handler, _options) {

var self = this;

Expand All @@ -49,7 +49,8 @@ function AMQPServer(processor, _options) {
this.protocol = (options && options.protocol) ? options.protocol : TBinaryProtocol;

this.maxMessages = options.max_messages || 10;
this.processor = processor;
this.processor_factory = processor_factory;
this.handler = handler;

this.options = {
connectUrl: options.connectUrl || 'amqp://localhost',
Expand All @@ -63,6 +64,18 @@ util.inherits(AMQPServer, events.EventEmitter);//使这个类继承EventEmitter

exports.AMQPServer = AMQPServer;

// Create a Handler object that will pass along the specified
// RMQ properties when invoked as the first argument.
function createPropertiesHandler(baseHandler, properties) {
var propertiesHandler = {};
for (var method in baseHandler) {
propertiesHandler[method] = function() {
baseHandler[method].apply(baseHandler, [properties].concat(Array.prototype.slice.call(arguments)));
}
}
return propertiesHandler;
}

AMQPServer.prototype.onReceiveData = function (ch, data) {
var self = this;

Expand All @@ -75,7 +88,7 @@ AMQPServer.prototype.onReceiveData = function (ch, data) {
// console.log('ch', ch);
var transport = self.transport;
var protocol = self.protocol;
var processor = self.processor;
var processor = new self.processor_factory(createPropertiesHandler(self.handler, data.properties));

self.emit('call', data);

Expand Down Expand Up @@ -190,14 +203,14 @@ AMQPServer.prototype.run = function () {

/**
* Create a single service Apache Thrift server.
* @param {object} processor - A service class or processor function.
* @param {object} processor_factory - A service class or processor function.
* @param {ServerOptions} options - Optional additional server configuration.
* @returns {object} - The Apache Thrift Multiplex Server.
*/
exports.createServer = function (processor, handler, options) {
if (processor.Processor) {
processor = processor.Processor;
exports.createServer = function (processor_factory, handler, options) {
if (processor_factory.Processor) {
processor_factory = processor_factory.Processor;
}

return new AMQPServer(new processor(handler), options);
return new AMQPServer(processor_factory, handler, options);
};
4 changes: 3 additions & 1 deletion sample/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ var users = {};

var server = ThriftAmqp.createServer(EchoService, {

echo: function (msg, result) {
echo: function (properties, msg, result) {
console.log('msg:', msg);
var timeout = 100;//Math.random() * 1000 || 0;
setTimeout(function () {
console.log('messageId:', properties.messageId);
console.log('timestamp:', properties.timestamp);
return result(null, msg);
}, timeout);
},
Expand Down