amqp 1.1.0.pre1 → 1.1.0.pre2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,223 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require "amqp/client"
4
-
5
- # Top-level namespace of amqp gem. Please refer to "See also" section below.
6
- #
7
- # @see AMQP.connect
8
- # @see AMQP.start
9
- # @see AMQP::Channel
10
- # @see AMQP::Exchange
11
- # @see AMQP::Queue
12
- module AMQP
13
-
14
- # Starts EventMachine event loop unless it is already running and connects
15
- # to AMQP broker using {AMQP.connect}. It is generally a good idea to
16
- # start EventMachine event loop in a separate thread and use {AMQP.connect}
17
- # (for Web applications that do not use Thin or Goliath, it is the only option).
18
- #
19
- # See {AMQP.connect} for information about arguments this method takes and
20
- # information about relevant topics such as authentication failure handling.
21
- #
22
- # @example Using AMQP.start to connect to AMQP broker, EventMachine loop isn't yet running
23
- # AMQP.start do |connection|
24
- # # default is to connect to localhost:5672, to root ("/") vhost as guest/guest
25
- #
26
- # # this block never exits unless either AMQP.stop or EM.stop
27
- # # is called.
28
- #
29
- # AMQP::Channel(connection) do |channel|
30
- # channel.queue("", :auto_delete => true).bind(channel.fanout("amq.fanout")).subscribe do |headers, payload|
31
- # # handle deliveries here
32
- # end
33
- # end
34
- # end
35
- #
36
- # @api public
37
- def self.start(connection_options_or_string = {}, other_options = {}, &block)
38
- EM.run do
39
- if !@connection || @connection.closed? || @connection.closing?
40
- @connection = connect(connection_options_or_string, other_options, &block)
41
- end
42
- @channel = Channel.new(@connection)
43
- @connection
44
- end
45
- end
46
-
47
- # Alias for {AMQP.start}
48
- # @api public
49
- def self.run(*args, &block)
50
- self.start(*args, &block)
51
- end
52
-
53
- # Properly closes default AMQP connection and then underlying TCP connection.
54
- # Pass it a block if you want a piece of code to be run once default connection
55
- # is successfully closed.
56
- #
57
- # @note If default connection was never established or is in the closing state already,
58
- # this method has no effect.
59
- # @api public
60
- def self.stop(reply_code = 200, reply_text = "Goodbye", &block)
61
- return if @connection.nil? || self.closing?
62
-
63
- EM.next_tick do
64
- if AMQP.channel and AMQP.channel.open? and AMQP.channel.connection.open?
65
- AMQP.channel.close
66
- end
67
- AMQP.channel = nil
68
-
69
-
70
- shim = Proc.new {
71
- block.call
72
-
73
- AMQP.connection = nil
74
- }
75
- @connection.disconnect(reply_code, reply_text, &shim)
76
- end
77
- end
78
-
79
- # Indicates that default connection is closing.
80
- #
81
- # @return [Boolean]
82
- # @api public
83
- def self.closing?
84
- @connection.closing?
85
- end
86
-
87
- # @return [Boolean] Current global logging value
88
- # @api public
89
- def self.logging
90
- self.settings[:logging]
91
- end
92
-
93
- # @return [Boolean] Sets current global logging value
94
- # @api public
95
- def self.logging=(value)
96
- self.settings[:logging] = !!value
97
- end
98
-
99
- # Default connection. When you do not pass connection instance to methods like
100
- # {Channel#initialize}, AMQP gem will use this default connection.
101
- #
102
- # @api public
103
- def self.connection
104
- @connection
105
- end
106
-
107
- # "Default channel". A placeholder for apps that only want to use one channel. This channel is not global, *not* used
108
- # under the hood by methods like {AMQP::Exchange#initialize} and only shared by exchanges/queues you decide on.
109
- # To reiterate: this is only a conventience accessor, since many apps (especially Web apps) can get by with just one
110
- # connection and one channel.
111
- #
112
- # @api public
113
- def self.channel
114
- @channel
115
- end
116
-
117
- # A placeholder for applications that only need one channel. If you use {AMQP.start} to set up default connection,
118
- # {AMQP.channel} is open on that connection, but can be replaced by your application.
119
- #
120
- #
121
- # @see AMQP.channel
122
- # @api public
123
- def self.channel=(value)
124
- @channel = value
125
- end
126
-
127
- # Sets global connection object.
128
- # @api public
129
- def self.connection=(value)
130
- @connection = value
131
- end
132
-
133
- # Alias for {AMQP.connection}
134
- # @deprecated
135
- # @api public
136
- def self.conn
137
- warn "AMQP.conn will be removed in 1.0. Please use AMQP.connection."
138
- @connection
139
- end
140
-
141
- # Alias for {AMQP.connection=}
142
- # @deprecated
143
- # @api public
144
- def self.conn=(value)
145
- warn "AMQP.conn= will be removed in 1.0. Please use AMQP.connection=(connection)."
146
- self.connection = value
147
- end
148
-
149
- # Connects to AMQP broker and yields connection object to the block as soon
150
- # as connection is considered open.
151
- #
152
- #
153
- # @example Using AMQP.connect with default connection settings
154
- #
155
- # AMQP.connect do |connection|
156
- # AMQP::Channel.new(connection) do |channel|
157
- # # channel is ready: set up your messaging flow by creating exchanges,
158
- # # queues, binding them together and so on.
159
- # end
160
- # end
161
- #
162
- # @example Using AMQP.connect to connect to a public RabbitMQ instance with connection settings given as a hash
163
- #
164
- # AMQP.connect(:host => "dev.rabbitmq.com", :username => "guest", :password => "guest") do |connection|
165
- # AMQP::Channel.new(connection) do |channel|
166
- # # ...
167
- # end
168
- # end
169
- #
170
- #
171
- # @example Using AMQP.connect to connect to a public RabbitMQ instance with connection settings given as a URI
172
- #
173
- # AMQP.connect "amqp://guest:guest@dev.rabbitmq.com:5672", :on_possible_authentication_failure => Proc.new { puts("Looks like authentication has failed") } do |connection|
174
- # AMQP::Channel.new(connection) do |channel|
175
- # # ...
176
- # end
177
- # end
178
- #
179
- #
180
- # @overload connect(connection_string, options = {})
181
- # Used to pass connection parameters as a connection string
182
- # @param [String] :connection_string AMQP connection URI, à la JDBC connection string. For example: amqp://bus.megacorp.internal:5877/qa
183
- #
184
- #
185
- # @overload connect(connection_options)
186
- # Used to pass connection options as a Hash.
187
- # @param [Hash] :connection_options AMQP connection options (:host, :port, :username, :vhost, :password)
188
- #
189
- # @option connection_options_or_string [String] :host ("localhost") Host to connect to.
190
- # @option connection_options_or_string [Integer] :port (5672) Port to connect to.
191
- # @option connection_options_or_string [String] :vhost ("/") Virtual host to connect to.
192
- # @option connection_options_or_string [String] :username ("guest") Username to use. Also can be specified as :user.
193
- # @option connection_options_or_string [String] :password ("guest") Password to use. Also can be specified as :pass.
194
- # @option connection_options_or_string [Hash] :ssl TLS (SSL) parameters to use.
195
- # @option connection_options_or_string [Fixnum] :heartbeat (0) Connection heartbeat, in seconds. 0 means no heartbeat. Can also be configured server-side starting with RabbitMQ 3.0.
196
- # @option connection_options_or_string [#call] :on_tcp_connection_failure A callable object that will be run if connection to server fails
197
- # @option connection_options_or_string [#call] :on_possible_authentication_failure A callable object that will be run if authentication fails (see Authentication failure section)
198
- #
199
- #
200
- # h2. Handling authentication failures
201
- #
202
- # AMQP 0.9.1 specification dictates that broker closes TCP connection when it detects that authentication
203
- # has failed. However, broker does exactly the same thing when other connection-level exception occurs
204
- # so there is no way to guarantee that connection was closed because of authentication failure.
205
- #
206
- # Because of that, AMQP gem follows Java client example and hints at _possibility_ of authentication failure.
207
- # To handle it, pass a callable object (a proc, a lambda, an instance of a class that responds to #call)
208
- # with :on_possible_authentication_failure option.
209
- #
210
- # @note This method assumes that EventMachine even loop is already running. If it is not the case or you are not sure, we recommend you use {AMQP.start} instead.
211
- # It takes exactly the same parameters.
212
- # @return [AMQP::Session]
213
- # @api public
214
- def self.connect(connection_options_or_string = {}, other_options = {}, &block)
215
- Client.connect(connection_options_or_string, other_options, &block)
216
- end
217
-
218
- # @return [Hash] Default AMQP connection settings. This hash may be modified.
219
- # @api public
220
- def self.settings
221
- @settings ||= AMQ::Client::Settings.default
222
- end
223
- end # AMQP