本文整理匯總了TypeScript中kafka-node.Consumer.on方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript Consumer.on方法的具體用法?TypeScript Consumer.on怎麽用?TypeScript Consumer.on使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類kafka-node.Consumer
的用法示例。
在下文中一共展示了Consumer.on方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: Observable
return new Observable(o => {
consumer.on('message', m => {
if (m.value === '__done') {
return o.complete();
}
o.next(m.value)
});
consumer.on('error', err => o.error(err));
return () => consumer.close(() => {});
});
示例2: kit
kit('publishes to a kafka topic', (done) => {
let topic = 'gustavTest-publish';
let consumer = new kafka.Consumer(client, [{
topic
}]);
let obs = new Observable(o => {
setTimeout(() => o.next('hello'), 15);
});
gr.to(topic, obs);
consumer.on('message', (message) => {
expect(message.value).to.equal('hello');
done();
});
});
示例3: restore
/**
* Restore the system by re-reading Kafka messages.
* This base implementation restores documents from a set of
* ArangoDB database collections, using the chassis-srv database provider.
* @param topics list of Kafka topics to be restored
*/
async restore(payload: any): Promise<any> {
if (_.isEmpty(payload) || _.isEmpty(payload.data)) {
throw new errors.InvalidArgument('Invalid payload for restore command');
}
const restoreData: RestoreData[] = payload.data || [];
// the Kafka config should contains a key-value pair, mapping
// a label with the topic's name
const kafkaEventsCfg = this.config.events.kafka;
const kafkaCfg = this.config.events.kafka.topics;
if (_.isNil(kafkaCfg) || kafkaCfg.length == 0) {
throw new errors.Internal('Kafka topics config not available');
}
const topicLabels = _.keys(kafkaCfg).filter((elem, index) => {
return elem.includes('.resource');
}).map((elem) => {
return elem.replace('.resource', '');
});
const restoreSetup = {};
const restoreEventSetup = {};
restoreData.forEach((data) => {
const ignoreOffset = (data.ignore_offset || []).filter((offset) => {
const isNumber = Number(offset) != NaN;
if (!isNumber) {
this.logger.warn(`Invalid value for "ignore_offset" parameter in restore: ${offset}`);
}
return isNumber;
});
restoreSetup[data.entity] = {
baseOffset: Number(data.base_offset) || 0,
ignoreOffset
};
});
const restoreCollections = _.keys(restoreSetup);
try {
const dbCfgs = this.config.database;
const dbCfgNames = _.keys(dbCfgs);
for (let i = 0; i < dbCfgNames.length; i += 1) {
const dbCfgName = dbCfgNames[i];
const dbCfg = dbCfgs[dbCfgName];
const collections = dbCfg.collections;
let graphName;
if (this.config.graph) {
graphName = this.config.graph.graphName;
}
const db = await database.get(dbCfg, this.logger, graphName);
if (_.isNil(collections)) {
this.logger.warn('No collections found on DB config');
return {};
}
let intersection: string[] = _.intersection(restoreCollections, collections);
if (intersection.length > 0) {
intersection = _.intersection(intersection, topicLabels);
for (let resource of intersection) {
const topicName = kafkaCfg[`${resource}.resource`].topic;
restoreEventSetup[topicName] = {
topic: this.kafkaEvents.topic(topicName),
events: this.makeResourcesRestoreSetup(db, resource),
baseOffset: restoreSetup[resource].baseOffset,
ignoreOffset: restoreSetup[resource].ignoreOffset
};
}
}
}
if (_.isEmpty(restoreEventSetup)) {
this.logger.warn('No data was setup for the restore process.');
} else {
const that = this;
// Start the restore process
this.logger.warn('restoring data');
for (let topicName in restoreEventSetup) {
const topicSetup: any = restoreEventSetup[topicName];
const restoreTopic: Topic = topicSetup.topic;
const topicEvents: any = topicSetup.events;
// saving listeners for potentially subscribed events on this topic,
// so they don't get called during the restore process
const previousEvents: string[] = _.cloneDeep(restoreTopic.subscribed);
const listenersBackup = new Map<string, Function[]>();
for (let event of previousEvents) {
listenersBackup.set(event, (restoreTopic.emitter as EventEmitter).listeners(event));
await restoreTopic.removeAllListeners(event);
}
//.........這裏部分代碼省略.........