Skip to content

Commit a1fca85

Browse files
author
Michael Klishin
committed
Tutorial 2 ported to Bunny
1 parent e9d2c78 commit a1fca85

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

ruby/new_task.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env ruby
2+
# encoding: utf-8
3+
4+
require "bunny"
5+
6+
conn = Bunny.new
7+
conn.start
8+
9+
ch = conn.create_channel
10+
q = ch.queue("task_queue", :durable => true)
11+
12+
msg = ARGV.empty? ? "Hello World!" : ARGV.join(" ")
13+
14+
q.publish(msg, :persistent => true)
15+
puts " [x] Published #{msg}"
16+
17+
sleep 1.0
18+
conn.close

ruby/worker.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env ruby
2+
# encoding: utf-8
3+
4+
require "bunny"
5+
6+
conn = Bunny.new
7+
conn.start
8+
9+
ch = conn.create_channel
10+
q = ch.queue("task_queue", :durable => true)
11+
12+
ch.prefetch(1)
13+
puts " [*] Waiting for messages. To exit press CTRL+C"
14+
15+
begin
16+
q.subscribe(:ack => true, :block => true) do |delivery_info, properties, body|
17+
puts " [x] Received #{body}"
18+
# imitate some work
19+
sleep 1.0
20+
puts " [x] Done"
21+
22+
ch.ack(delivery_info.delivery_tag)
23+
end
24+
rescue Interrupt => _
25+
puts " [*] Shutting down..."
26+
end

0 commit comments

Comments
 (0)