Update ruby tutorial one

This commit is contained in:
Caique Hitoshi Mitsuoka 2018-02-19 16:19:52 -03:00
parent a2da52fbdb
commit 7a4de0393f
3 changed files with 27 additions and 33 deletions

View File

@ -1,21 +1,19 @@
#!/usr/bin/env ruby
# encoding: utf-8
require 'bunny'
require "bunny"
connection = Bunny.new(automatically_recover: false)
connection.start
conn = Bunny.new(:automatically_recover => false)
conn.start
ch = conn.create_channel
q = ch.queue("hello")
channel = connection.create_channel
queue = channel.queue('hello')
begin
puts " [*] Waiting for messages. To exit press CTRL+C"
q.subscribe(:block => true) do |delivery_info, properties, body|
puts ' [*] Waiting for messages. To exit press CTRL+C'
queue.subscribe(block: true) do |_delivery_info, _properties, body|
puts " [x] Received #{body}"
end
rescue Interrupt => _
conn.close
connection.close
exit(0)
end

View File

@ -1,15 +1,13 @@
#!/usr/bin/env ruby
# encoding: utf-8
require 'bunny'
require "bunny"
connection = Bunny.new(automatically_recover: false)
connection.start
conn = Bunny.new(:automatically_recover => false)
conn.start
channel = connection.create_channel
queue = channel.queue('hello')
ch = conn.create_channel
q = ch.queue("hello")
ch.default_exchange.publish("Hello World!", :routing_key => q.name)
channel.default_exchange.publish('Hello World!', routing_key: queue.name)
puts " [x] Sent 'Hello World!'"
conn.close
connection.close

View File

@ -1,25 +1,23 @@
#!/usr/bin/env ruby
# encoding: utf-8
require 'bunny'
require "bunny"
connection = Bunny.new(automatically_recover: false)
connection.start
conn = Bunny.new(:automatically_recover => false)
conn.start
channel = connection.create_channel
queue = channel.queue('task_queue', durable: true)
ch = conn.create_channel
q = ch.queue("task_queue", :durable => true)
ch.prefetch(1)
puts " [*] Waiting for messages. To exit press CTRL+C"
channel.prefetch(1)
puts ' [*] Waiting for messages. To exit press CTRL+C'
begin
q.subscribe(:manual_ack => true, :block => true) do |delivery_info, properties, body|
queue.subscribe(manual_ack: true, block: true) do |delivery_info, _properties, body|
puts " [x] Received '#{body}'"
# imitate some work
sleep body.count(".").to_i
puts " [x] Done"
ch.ack(delivery_info.delivery_tag)
sleep body.count('.').to_i
puts ' [x] Done'
channel.ack(delivery_info.delivery_tag)
end
rescue Interrupt => _
conn.close
connection.close
end