2017-02-02 2 views
0

ich tsoa bin mit für Prahlerei Definition zu erzeugen und zu versuchen, api Anfrage zu senden i RabbitMQ bin mit facundoolano Implementierung rabbitmqPrahlerei api rabbitmq Antwortwarteschlange Objekt

import { configuration, random, uuid, amqp } from '../config'; 
const EventEmitter = require('events'); 

const REPLY_QUEUE = 'amq.rabbitmq.reply-to'; 
const createClient =() => amqp.connect(configuration.amqpUrl) 
    .then((conn) => conn.createChannel()) 
    .then((channel: any) => { 
     // create an event emitter where rpc responses will be published by correlationId 
     channel.responseEmitter = new EventEmitter(); 
     channel.responseEmitter.setMaxListeners(0); 
     channel.consume(REPLY_QUEUE, 
      (msg: any) => channel.responseEmitter.emit(msg.properties.correlationId, msg.content), 
      { noAck: true }); 
     return channel; 
    }); 

const sendRPCMessage = (channel: any, message: any, rpcQueue: any) => new Promise((resolve, reject) => { 
    const correlationId = uuid.v4(); 
    // listen for the content emitted on the correlationId event 
    try { 
     channel.responseEmitter.once(correlationId, resolve); 
     channel.sendToQueue(rpcQueue, new Buffer(JSON.stringify(message)), { correlationId, replyTo: REPLY_QUEUE }) 
    } catch (error) { reject(error) } 

}); 

zu verbinden, aber das Problem wurde habe ich keine Ahnung wie man die Antwort der Anfrage zurückgibt. mein Controller habe ich versucht Warteschlange Antwort wie diese

@Post() 
    public async postSurvey(survey: Survey): Promise<Survey> { 
     var obj: Survey; 
     await createClient().then((channel) => { 
      sendRPCMessage(channel, survey, "survey_q_req").then((data) => { 
       obj = data as Survey; 
      }).catch((error) => { console.log(error) }); 
     }).catch((error) => { console.log(error) }); 
     return obj 
    } 

wie zurückzukehren Antwort von der Antwortwarteschlange von rabbitmq richtigen Weg

Antwort

1
@Post() 
public async postSurvey(survey: Survey): Promise<Survey> { 
    return createClient() 
    .then(channel => { 
     return sendRPCMessage(channel, survey, "survey_q_req") 
    }) 
    .then(data => { 
     return data; 
    }) 
    .catch((error) => { console.log(error) }); 
} 
zurückzukehren
Verwandte Themen