Mensajes de test de rabbit
This commit is contained in:
@@ -14,4 +14,5 @@ export const env = {
|
||||
RABBITMQ_TTL: process.env.RABBITMQ_TTL,
|
||||
RABBITMQ_SECURE: process.env.RABBITMQ_SECURE,
|
||||
RABBITMQ_RETRY_INTERVAL: process.env.RABBITMQ_INTERVAL,
|
||||
RABBITMQ_VHOST: process.env.RABBITMQ_VHOST,
|
||||
};
|
||||
|
||||
@@ -1,43 +1,83 @@
|
||||
import client, { ChannelModel, connect, Connection } from "amqplib"
|
||||
import client, { ChannelModel, ConfirmChannel, connect, Connection } from "amqplib"
|
||||
import { env } from "config/env"
|
||||
import { Channel } from "node:diagnostics_channel"
|
||||
|
||||
const rmqUser = env.RABBITMQ_USER
|
||||
const rmqPass = env.RABBITMQ_PASSWORD
|
||||
const rmqHost = env.RABBITMQ_HOST
|
||||
const rmqPort = Number(env.RABBITMQ_PORT)
|
||||
const rmqSecure = env.RABBITMQ_SECURE
|
||||
const rmqSecure = false
|
||||
const rmqVhost = env.RABBITMQ_VHOST
|
||||
|
||||
|
||||
const PREFETCH_LIMIT = 1
|
||||
|
||||
class RabbitConnection {
|
||||
connection?: Connection
|
||||
channel?: Channel
|
||||
connection?: ChannelModel
|
||||
channel?: ConfirmChannel
|
||||
connected: Boolean = false
|
||||
|
||||
public async connect() {
|
||||
this.connection = await this.createConnection();
|
||||
if (this.connection == undefined) throw new Error("[RMQ] Error crecreando la conexion")
|
||||
this.channel = await this.createConfirmChannel()
|
||||
}
|
||||
|
||||
protected async createConnection(): Promise<ChannelModel> {
|
||||
protected async createConnection() {
|
||||
const { hostname, port, secure } = { hostname: rmqHost, port: rmqPort, secure: rmqSecure }
|
||||
const { username, password } = { username: rmqUser, password: rmqPass };
|
||||
const protocol = secure ? 'amqps' : 'amqp';
|
||||
const vhost = rmqVhost
|
||||
|
||||
const connection = await connect({
|
||||
const connection = await client.connect({
|
||||
protocol,
|
||||
hostname,
|
||||
port,
|
||||
username,
|
||||
password
|
||||
password,
|
||||
vhost
|
||||
});
|
||||
|
||||
connection.on('error', (error: unknown) => {
|
||||
console.error(`[EVENT BUS] Rabbitmq connection error :: ${error}`);
|
||||
console.error(`[RMQ] Rabbitmq connection error :: ${error}`);
|
||||
Promise.reject(error);
|
||||
});
|
||||
|
||||
return connection;
|
||||
}
|
||||
|
||||
protected async createConfirmChannel() {
|
||||
const channel = await this.connection?.createConfirmChannel()
|
||||
await channel?.prefetch(PREFETCH_LIMIT)
|
||||
|
||||
if (channel == undefined) throw new Error("[RMQ] Error crecreando el canal")
|
||||
|
||||
channel.on('error', (error: unknown) => {
|
||||
console.error(`[RMQ] Rabbitmq channel error :: ${error}`);
|
||||
Promise.reject(error);
|
||||
});
|
||||
|
||||
return channel;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
while (true) {
|
||||
console.log("test")
|
||||
async function test() {
|
||||
const rbmq = new RabbitConnection()
|
||||
await rbmq.connect()
|
||||
|
||||
console.log("enviando")
|
||||
|
||||
rbmq.channel?.sendToQueue("sim.queue", Buffer.from("test"), {},
|
||||
function (err, ok) {
|
||||
if (err !== null)
|
||||
console.warn('Message nacked!');
|
||||
else
|
||||
console.log('Message acked');
|
||||
}
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
test()
|
||||
|
||||
export default {}
|
||||
|
||||
Reference in New Issue
Block a user