我在Eclipse中运行以下node-rdkafka代码作为Node.js应用程序。这是https://blizzard.github.io/node-rdkafka/current/tutorial-producer_.html
的示例代码我想在测试服务器中运行它并从iOS Mobile应用程序调用。 我知道在AWS中运行node.js应用程序。 问题我:在像Tomcat这样的免费测试服务器环境中是否还有其他选项可以运行? 问题II :即使我能够在服务器中运行此node.js应用程序,我如何从移动应用程序调用?我是否需要致电 producer.on('准备好',功能(arg)(或)我需要从移动应用程序调用哪些功能?
package:...
答案 0 :(得分:1)
首先,您需要一台HTTP服务器。可以使用ExpressJS。然后,基本上在最后处理Express代码,但将生产者循环移动到请求路由中。
所以,从你拥有的东西开始
var Kafka = require('node-rdkafka');
//console.log(Kafka.features);
//console.log(Kafka.librdkafkaVersion);
var producer = new Kafka.Producer({
'metadata.broker.list': 'localhost:9092',
'dr_cb': true
});
var topicName = 'MyTest';
//logging debug messages, if debug is enabled
producer.on('event.log', function(log) {
console.log(log);
});
//logging all errors
producer.on('event.error', function(err) {
console.error('Error from producer');
console.error(err);
});
producer.on('delivery-report', function(err, report) {
console.log('delivery-report: ' + JSON.stringify(report));
counter++;
});
//Wait for the ready event before producing
producer.on('ready', function(arg) {
console.log('producer ready.' + JSON.stringify(arg));
});
producer.on('disconnected', function(arg) {
console.log('producer disconnected. ' + JSON.stringify(arg));
});
//starting the producer
producer.connect();
然后,您可以将其添加到同一文件中。
var express = require('express')
var app = express()
app.get('/', (req, res) => res.send('Ready to send messages!'))
app.post('/:maxMessages', function (req, res) {
if (req.params.maxMessages) {
var maxMessages = parseInt(req.params.maxMessages);
for (var i = 0; i < maxMessages; i++) {
var value = new Buffer('MyProducerTest - value-' +i);
var key = "key-"+i;
// if partition is set to -1, librdkafka will use the default partitioner
var partition = -1;
producer.produce(topicName, partition, value, key);
} // end for
} // end if
}); // end app.post()
app.listen(3000, () => console.log('Example app listening on port 3000!'))
我不认为民意调查循环是必要的,因为你不再关心counter
了。
现在,将您的移动应用程序连接到http://<your server IP>:3000/
,然后将测试消息发送到http://<your server IP>:3000/10
的POST请求,并调整以更改要发送的消息数
答案 1 :(得分:0)
我可能迟到了,但这是我使用诺言的方式,发现它比超时等更好。
newRulesTable.after('*:criteriaTypeChange', function(o){
for(var i=0; i<newRulesTable.data.size();i++) {
if(newRulesTable.data.item(i).get('criteriaType') == getTypes().date) {
// TODO HERE
}
}
});
请注意,kafkaEvents包含我正在监听的事件的常量,它只是一个引用,例如kafkaEvents.LOG与const postMessageToPublisher = (req, res) => {
return new Promise((resolve, reject) => {
producer.connect();
producer.setPollInterval(globalConfigs.producerPollingTime);
const actualBody = requestBody.data;
const requestBody = req.body;
const topicName = req.body.topicName;
const key = requestBody.key || uuid();
const partition = requestBody.partition || undefined;
const data = Buffer.from(JSON.stringify(udpatedBody));
/**
* Actual messages are sent here when the producer is ready
*/
producer.on(kafkaEvents.READY, () => {
try {
producer.produce(
topic,
partition,
message,
key // setting key user provided or UUID
);
} catch (error) {
reject(error);
}
});
// Register listener for debug information; only invoked if debug option set in driver_options
producer.on(kafkaEvents.LOG, log => {
logger.info('Producer event log notification for debugging:', log);
});
// Register error listener
producer.on(kafkaEvents.ERROR, err => {
logger.error('Error from producer:' + JSON.stringify(err));
reject(err);
});
// Register delivery report listener
producer.on(kafkaEvents.PUBLISH_ACKNOWLEDGMENT, (err, ackMessage) => {
if (err) {
logger.error(
'Delivery report: Failed sending message ' + ackMessage.value
);
logger.error('and the error is :', err);
reject({ value: ackMessage.value, error: err });
} else {
resolve({
teamName: globalConfigs.TeamNameService,
topicName: ackMessage.topic,
key: ackMessage.key.toString()
});
}
});
});
};
,并且调用函数也期望这一承诺,因此我们使用event.log
和.then(data => 'send your response to user from here')
这就是我用诺言实现的方式