1.1.4 • Published 3 years ago
msek-rabbitmq-library v1.1.4
msek-rabbitmq-library
How to use?
Produce task from a microservice
// Service load the RabbitMQ connection string parameters from config, and build BrokerConnectionString in intialize phase.
// Intialize the MessageBroker for the service
const service = "user-service";
const broker = new MessageBroker(service, "localhost", 5672);
// Enqueue task
const context = { id: 1, name: "Moustafa" };
await broker.enqueueTask("user.new-user-created", context);
Consume task from a microservice
const service = "invoice-service";
const cs = new BrokerConnectionString("localhost", 5672);
const broker = new MessageBroker(cs, serviceName);
// Implement IBrokerConsumer to perform the task
class InvoiceConsumer implements IBrokerConsumer {
private static _instance: InvoiceConsumer;
static getInstance(): IBrokerConsumer {
if (!InvoiceConsumer._instance) {
this._instance = new InvoiceConsumer();
}
return this._instance;
}
perform(msg: IBrokerMessage): void {
// the recieved message
console.log(msg.context);
}
}
// In intialize phase register the consumers
await broker.registerConsumer(
InvoiceConsumer.getInstance(),
"user.new-user-created"
);