RabbitMQConnector.js 867 B

12345678910111213141516171819202122232425262728293031323334353637
  1. const amqp = require('amqplib');
  2. class RabbitMQConnector {
  3. constructor() {
  4. this.connection = null;
  5. this.channel = null;
  6. }
  7. async connect() {
  8. try {
  9. this.connection = await amqp.connect('amqp://username:password@messageBroker');
  10. this.channel = await this.connection.createChannel();
  11. console.log('Connected to RabbitMQ');
  12. } catch (error) {
  13. console.error('Error connecting to RabbitMQ:', error);
  14. }
  15. }
  16. async disconnect() {
  17. try {
  18. if (this.channel) {
  19. await this.channel.close();
  20. console.log('Channel closed');
  21. }
  22. if (this.connection) {
  23. await this.connection.close();
  24. console.log('Connection closed');
  25. }
  26. } catch (error) {
  27. console.error('Error disconnecting from RabbitMQ:', error);
  28. throw error;
  29. }
  30. }
  31. }
  32. module.exports = RabbitMQConnector;