Ubuntu 24.04
Sponsored Link

RabbitMQ : Ruby पर प्रयोग करें2024/07/22

 

यह Ruby पर RabbitMQ का उपयोग करने का एक उदाहरण है।

[1] आवश्यक पैकेज स्थापित करें।
root@dlp:~#
apt -y install ruby-bunny
[2] यह Ruby पर संदेश भेजने का एक उदाहरण है।
उदाहरण के लिए, एक उपयोगकर्ता [serverworld], वर्चुअलहोस्ट [my_vhost] के साथ [localhost] पर RabbitMQ से जुड़ें।
ubuntu@dlp:~$
vi send_msg.rb
require "bunny"

connection = Bunny.new(
    :hostname => "127.0.0.1",
    :port => 5672,
    :vhost => "/my_vhost",
    :user => "serverworld",
    :pass => "password",
)
connection.start

channel = connection.create_channel

q = channel.queue("Hello_World")
channel.default_exchange.publish("Hello RabbitMQ World!", :routing_key => q.name)
puts " [x] Sent 'Hello RabbitMQ World!'"

connection.close

ubuntu@dlp:~$
ruby send_msg.rb

 [x] Sent 'Hello RabbitMQ World!'
[3] यह Ruby पर संदेश प्राप्त करने का एक उदाहरण है।
ubuntu@node01:~$
vi receive_msg.rb
require "bunny"

Signal.trap(:INT){
    puts "Exited from receiving queues."
    exit(0)
}

connection = Bunny.new(
    :hostname => "10.0.0.30",
    :port => 5672,
    :vhost => "/my_vhost",
    :user => "serverworld",
    :pass => "password",
)
connection.start

channel = connection.create_channel
q = channel.queue("Hello_World")

puts " [*] Waiting for messages in #{q.name}. To exit press CTRL+C"
q.subscribe(:block => true) do |delivery_info, properties, body|
    puts " [x] Received #{body}"

    delivery_info.consumer.cancel
end

ubuntu@node01:~$
ruby receive_msg.rb

 [*] Waiting for messages in Hello_World. To exit press CTRL+C
 [x] Received Hello RabbitMQ World!
मिलान सामग्री