God
God

Reputation: 684

How to send value with message on bunny queue on rabbitmp

i want to send values with message in bunny message queue on rabbit server. is there any possibility of doing like that. thanks in advance.

Upvotes: 1

Views: 100

Answers (1)

God
God

Reputation: 684

finally i found it.

In Queue we can able to publish with headers properties like this,

in send.rb

conn = Bunny.new
conn.start

ch= conn.create_channel
q = ch.queue("QueueName")

msg  = "Message want to send" 

q.publish(
    msg, 
    :persistent => true, 
    :headers => { :user_id => "10", :user_name => "xxx"} 
    )

in receive.rb

conn = Bunny.new
conn.start

 ch = conn.create_channel
 q = ch.queue("QueueName")

 ch.prefetch(1)

 begin

q.subscribe(:ack => true, :block => true) do |delivery_info, properties, body|

  puts "Message : #{body}"
  puts "To UserId : #{properties[:headers]["user_id"].to_s}\n"
  puts "To UserId : #{properties[:headers]["user_name"].to_s}\n"

  ch.ack(delivery_info.delivery_tag)

end

rescue => e

puts "Error #{e.to_s}"
conn.close

end

We can get userId 10 and username "XXX" in receiver end.

Upvotes: 2

Related Questions