0

SOLVED PROBLEM.

Just create 2 different queues, like rpc.queue and pubsub.queue. Then you can use multiple messaging pattern in one service without any problem.


I create one rails service using Bunny and ConnectionPool Gem. This service "in my mind (cause not yet implemented)" handle multiple RMQ pattern such as Direct Messaging and RPC. These patterns initialized with different object of Connection Class and defined inside initalizer folder.

Initializer looks like this:

# RMQ Initializer for RabbitMQ Connection
class RMQ
  include Lontara::RMQ

  # NOTE: Call 2 Server caused errors
  def self.start(url:, queue:, rpc_exchange:, pubsub_exchange:)
    # Then start the consumer and subscriber
    Server::RPCConsumer.new(Connection.new(url:), queue:, exchange: rpc_exchange).consume
    Server::Subscriber.new(Connection.new(url:), queue:, exchange: pubsub_exchange).subscribe
  end
end

RMQ.start(
  url: ENV.fetch('RABBITMQ_URL', 'amqp://guest:guest@rmqserver:5672'),
  queue: ENV.fetch('RABBITMQ_QUEUE_VOUCHER', 'lontara-dev.voucher'),
  rpc_exchange: ENV.fetch('RABBITMQ_EXCHANGE_RPC', 'lontara-dev.rpc'),
  pubsub_exchange: ENV.fetch('RABBITMQ_EXCHANGE_PUBSUB', 'lontara-dev.pubsub')
)

and Connection class:

module Lontara
  module RMQ
    # Class Connection initializing the connection to RabbitMQ.
    class Connection
      def initialize(url: ENV['RABBITMQ_URL'])
        @connection = Bunny.new(url)
        connection.start

        @channel = channel_pool.with(&:create_channel)

        yield self if block_given?
      end

      def close
        channel.close
        connection.close
      end

      attr_reader :connection, :channel

      private

      def channel_pool
        @channel_pool ||= ConnectionPool.new { @connection }
      end
    end
  end
end

The problem goes whenever these 2 Server:: (RPC and Subscriber) activated. Impacted only when use RPC as messaging, the problem is RPC Publisher does not get response from Consumer.

These steps (when RPC produce error) are:

  1. Run Rails server
  2. Open new terminal, and open rails console in same project
  3. Create Request to Consumer using RPCPublisher
  4. Publisher get response. Then send request again... On this step not get response.
  5. Job is pending, i push ctrl+c to terminate job. Send request again, and get response...
  6. Try again like step 4, and error...

But, if Server::Publisher not initialized on initializer, nothing error happened.

I assumed this error happened cause of thread... But i don't really get helped from other articles on internet.

My expectation is so simple:

RPC Connection requested for Get related (because RPC can reply this request) or other action requires response. And Pub/Sub (Direct) request for Create, Update, Delete since this type didn't need it.

Your answer really help me... Thankyou !

0 Answers0