1.2.3 • Published 2 months ago
amqplib-base-adapter v1.2.3
Overview
Base adapter for amqplib
With all connection logic and logs!
As logger it uses winston.
You can find producer and consumer example in /examples directory
Consumer example
const ConsumerExampleConfig: BaseConsumerConfig = {
queue: 'example.queue',
exchange: 'example_exchange',
exchangeType: 'topic',
routingKey: 'example_route',
prefetch: 1,
rmq: {
host: env.RMQ_CLUSTER_ADDRESS,
password: env.RMQ_CLUSTER_PASSWORD,
port: env.RMQ_CLUSTER_PORT,
username: env.RMQ_CLUSTER_USERNAME,
},
environment: env.ENVIRONMENT,
};
class ConsumerExample extends BaseConsumer {
async handleMessage(message: any) {
try {
this.logger.info({ message });
} catch (err) {
this.logger.error('[ConsumerExample] error handle message', err);
}
}
onClose() {
this.logger.error('[ConsumerExample] Connection closed, reconnecting', { errorCode: this.errorCode });
}
onError(error: any) {
this.logger.error('[ConsumerExample] Connection error', error, { errorCode: this.errorCode });
}
onConnectionFailed(error: Error) {
this.logger.error('[ConsumerExample] Connection failed:', error);
}
}
const consumerExample = new ConsumerExample(ConsumerExampleConfig);
void consumerExample.run();
Producer example
const ProducerExampleConfig = {
exchange: 'example_exchange',
exchangeType: 'topic',
routingKey: 'example_route',
rmq: {
host: env.RMQ_CLUSTER_ADDRESS,
password: env.RMQ_CLUSTER_PASSWORD,
port: env.RMQ_CLUSTER_PORT,
username: env.RMQ_CLUSTER_USERNAME,
},
environment: env.ENVIRONMENT,
};
class ProducerExample extends BaseProducer {
async publish() {
try {
const message: Buffer = Buffer.from(JSON.stringify({
test: 'testdata1',
}));
const result = this.channel?.publish(this.exchange, this.routingKey, message);
this.logger.info('[ProducerExample] publish result: ', { result, message });
} catch (error) {
this.logger.error('[ProducerExample] error publish messages', error);
}
}
onClose() {
this.logger.error('[ProducerExample] Connection closed, reconnecting', { errorCode: this.errorCode });
}
onError(error: any) {
this.logger.error('[ProducerExample] Connection error', error, { errorCode: this.errorCode });
}
onConnectionFailed(error: Error) {
this.logger.error('[ProducerExample] Connection failed:', error);
}
}
const producerExample = new ProducerExample(ProducerExampleConfig);
void producerExample.run();
1.2.3
2 months ago
1.2.2
2 months ago
1.2.1
2 months ago
1.2.0
5 months ago
1.1.0
10 months ago
1.0.17
1 year ago
1.0.16
1 year ago
1.0.15
1 year ago
1.0.14
1 year ago
1.0.13
1 year ago
1.0.12
1 year ago
1.0.11
1 year ago
1.0.10
1 year ago
1.0.9
1 year ago
1.0.8
1 year ago
1.0.7
1 year ago
1.0.6
1 year ago
1.0.4
1 year ago
1.0.3
1 year ago
1.0.2
1 year ago
1.0.1
1 year ago
1.0.0
1 year ago