forked from ruby-amqp/bunny
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbunny.rb
75 lines (56 loc) · 1.5 KB
/
bunny.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# -*- encoding: utf-8; mode: ruby -*-
require "timeout"
require "bunny/version"
require "amq/protocol/client"
require "bunny/framing"
require "bunny/exceptions"
require "bunny/socket"
# Core entities: connection, channel, exchange, queue, consumer
require "bunny/session"
require "bunny/channel"
require "bunny/exchange"
require "bunny/queue"
require "bunny/consumer"
module Bunny
PROTOCOL_VERSION = AMQ::Protocol::PROTOCOL_VERSION
# unifies Ruby standard library's Timeout (which is not accurate on
# Ruby 1.8 and has other issues) and SystemTimer (the gem)
Timer = if RUBY_VERSION < "1.9"
begin
require "bunny/system_timer"
Bunny::SystemTimer
rescue LoadError
Timeout
end
else
Timeout
end
#
# API
#
def self.version
VERSION
end
def self.protocol_version
AMQ::Protocol::PROTOCOL_VERSION
end
def self.new(connection_string_or_opts = {}, opts = {}, &block)
if connection_string_or_opts.respond_to?(:keys) && opts.empty?
opts = connection_string_or_opts
end
conn = Session.new(connection_string_or_opts, opts)
@default_connection ||= conn
conn
end
def self.run(connection_string_or_opts = {}, opts = {}, &block)
raise ArgumentError, 'Bunny#run requires a block' unless block
client = Client.new(connection_string_or_opts, opts)
begin
client.start
block.call(client)
ensure
client.stop
end
:run_ok
end
end