Class: LontaraUtilities::RMQ::Client::RPCProducer

Inherits:
Object
  • Object
show all
Defined in:
lib/lontara_utilities/rmq/client/rpc_producer.rb

Overview

Client for AMQ on RPC Pattern

Instance Method Summary collapse

Constructor Details

#initialize(connection, queue:, reply_queue: 'amq.rabbitmq.reply-to') ⇒ RPCProducer

Returns a new instance of RPCProducer.



8
9
10
11
12
13
14
15
16
17
# File 'lib/lontara_utilities/rmq/client/rpc_producer.rb', line 8

def initialize(connection, queue:, reply_queue: 'amq.rabbitmq.reply-to')
  @channel = connection.channel
  @exchange = connection.exchange
  @queue = channel.queue(queue, durable: true)
  @reply_queue = channel.queue(reply_queue, durable: true)

  # Consumer must be initialized to Reply Queue
  # before publishing the message.
  consume_reply
end

Instance Method Details

#publish(&block) ⇒ Object

Publish message to the queue.

This method will yield the block to get the message. Message published to NestJS Service must contain ‘id` key.

Example:

“‘ client.publish do

{
  id: 'message_1',
  pattern: 'voucher.voucher.find_one',
  data: { id: 1 }
}

end “‘



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/lontara_utilities/rmq/client/rpc_producer.rb', line 35

def publish(&block) # rubocop:disable Metrics/AbcSize
  @request = block.call
  @request_id = SecureRandom.uuid

  exchange.publish(
    request.to_json,
    routing_key: queue.name,
    correlation_id: request_id,
    reply_to: reply_queue.name
  )

  # waits for the signal from #consume_reply
  lock.synchronize { condition.wait(lock) }

  response
end