rservicebus 0.0.18 → 0.0.21
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rservicebus.rb +6 -2
- data/lib/rservicebus/{Agent.rb → Agent/Beanstalk.rb} +1 -1
- data/lib/rservicebus/Agent/Bunny.rb +49 -0
- data/lib/rservicebus/Config.rb +4 -5
- data/lib/rservicebus/ConfigureMQ.rb +29 -0
- data/lib/rservicebus/Host.rb +128 -138
- data/lib/rservicebus/MQ.rb +73 -0
- data/lib/rservicebus/MQ/Beanstalk.rb +64 -0
- data/lib/rservicebus/MQ/Bunny.rb +54 -0
- metadata +8 -3
data/lib/rservicebus.rb
CHANGED
@@ -1,15 +1,19 @@
|
|
1
|
+
#Add the currently running directory to the start of the load path
|
2
|
+
#$:.unshift File.dirname(__FILE__) + '/../../lib'
|
3
|
+
|
1
4
|
require "rubygems"
|
2
|
-
require 'beanstalk-client'
|
3
5
|
require "yaml"
|
4
6
|
require "uuidtools"
|
5
7
|
require "redis"
|
6
8
|
require "json"
|
7
9
|
|
8
10
|
require "rservicebus/helper_functions"
|
9
|
-
require "rservicebus/Agent"
|
11
|
+
require "rservicebus/Agent/Beanstalk"
|
12
|
+
require "rservicebus/Agent/Bunny"
|
10
13
|
require "rservicebus/ErrorMessage"
|
11
14
|
require "rservicebus/HandlerLoader"
|
12
15
|
require "rservicebus/ConfigureAppResource"
|
16
|
+
require "rservicebus/ConfigureMQ"
|
13
17
|
require "rservicebus/Host"
|
14
18
|
require "rservicebus/Config"
|
15
19
|
require "rservicebus/Stats"
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module RServiceBus
|
2
|
+
require 'bunny'
|
3
|
+
|
4
|
+
#A means for a stand-alone process to interact with the bus, without being a full
|
5
|
+
#rservicebus application
|
6
|
+
class Agent_Bunny
|
7
|
+
@bunny
|
8
|
+
|
9
|
+
def initialize(host='localhost')
|
10
|
+
@bunny = Bunny.new(:host=>host)
|
11
|
+
@bunny.start
|
12
|
+
@direct_exchange = @bunny.exchange('rservicebus.agent')
|
13
|
+
end
|
14
|
+
|
15
|
+
# Put a msg on the bus
|
16
|
+
#
|
17
|
+
# @param [Object] messageObj The msg to be sent
|
18
|
+
# @param [String] queueName the name of the queue to be send the msg to
|
19
|
+
# @param [String] returnAddress the name of a queue to send replies to
|
20
|
+
def sendMsg(messageObj, queueName, returnAddress=nil)
|
21
|
+
msg = RServiceBus::Message.new( messageObj, returnAddress )
|
22
|
+
serialized_object = YAML::dump(msg)
|
23
|
+
|
24
|
+
|
25
|
+
q = @bunny.queue(queueName)
|
26
|
+
q.bind(@direct_exchange)
|
27
|
+
#q.publish( serialized_object )
|
28
|
+
|
29
|
+
@direct_exchange.publish(serialized_object)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Gives an agent a mean to receive replies
|
33
|
+
#
|
34
|
+
# @param [String] queueName the name of the queue to monitor for messages
|
35
|
+
def checkForReply( queueName )
|
36
|
+
q = @bunny.queue(queueName)
|
37
|
+
|
38
|
+
loop = true
|
39
|
+
while loop do
|
40
|
+
msg = q.pop[:payload]
|
41
|
+
loop = ( msg == :queue_empty )
|
42
|
+
end
|
43
|
+
|
44
|
+
@msg = YAML::load(msg)
|
45
|
+
return @msg.msg
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
data/lib/rservicebus/Config.rb
CHANGED
@@ -2,7 +2,7 @@ module RServiceBus
|
|
2
2
|
|
3
3
|
#Marshals configuration information for an rservicebus host
|
4
4
|
class Config
|
5
|
-
attr_reader :appName, :messageEndpointMappings, :handlerPathList, :localQueueName, :errorQueueName, :maxRetries, :forwardReceivedMessagesTo, :verbose, :
|
5
|
+
attr_reader :appName, :messageEndpointMappings, :handlerPathList, :localQueueName, :errorQueueName, :maxRetries, :forwardReceivedMessagesTo, :verbose, :queueTimeout, :statOutputCountdown, :contractList, :libList, :auditQueueName, :mqHost
|
6
6
|
|
7
7
|
@appName
|
8
8
|
@messageEndpointMappings
|
@@ -17,7 +17,7 @@ class Config
|
|
17
17
|
|
18
18
|
@verbose
|
19
19
|
|
20
|
-
@
|
20
|
+
@mq
|
21
21
|
|
22
22
|
@queueTimeout
|
23
23
|
|
@@ -163,9 +163,8 @@ class Config
|
|
163
163
|
return self
|
164
164
|
end
|
165
165
|
|
166
|
-
def
|
167
|
-
@
|
168
|
-
|
166
|
+
def configureMq
|
167
|
+
@mqHost = self.getValue( "MQ", "beanstalk://localhost" )
|
169
168
|
return self
|
170
169
|
end
|
171
170
|
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module RServiceBus
|
2
|
+
|
3
|
+
require "uri"
|
4
|
+
|
5
|
+
#Configure AppResources for an rservicebus host
|
6
|
+
class ConfigureMQ
|
7
|
+
|
8
|
+
def get( string, timeout )
|
9
|
+
|
10
|
+
uri = URI.parse( string )
|
11
|
+
case uri.scheme
|
12
|
+
when "beanstalk"
|
13
|
+
require "rservicebus/MQ/Beanstalk"
|
14
|
+
mq = MQ_Beanstalk.new( uri, timeout )
|
15
|
+
|
16
|
+
when "bunny"
|
17
|
+
require "rservicebus/MQ/Bunny"
|
18
|
+
mq = MQ_Bunny.new( uri, timeout )
|
19
|
+
|
20
|
+
else
|
21
|
+
abort("Scheme, #{uri.scheme}, not recognised when configuring mq, #{string}");
|
22
|
+
end
|
23
|
+
|
24
|
+
return mq
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
data/lib/rservicebus/Host.rb
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
module RServiceBus
|
2
2
|
|
3
|
+
|
4
|
+
class NoMsgToProcess<StandardError
|
5
|
+
end
|
6
|
+
|
3
7
|
#Host process for rservicebus
|
4
8
|
class Host
|
5
9
|
|
@@ -10,7 +14,7 @@ module RServiceBus
|
|
10
14
|
|
11
15
|
@subscriptions
|
12
16
|
|
13
|
-
@
|
17
|
+
@mq
|
14
18
|
|
15
19
|
@appResources
|
16
20
|
|
@@ -33,21 +37,8 @@ module RServiceBus
|
|
33
37
|
return self;
|
34
38
|
end
|
35
39
|
|
36
|
-
def
|
37
|
-
|
38
|
-
@beanstalk = Beanstalk::Pool.new([@config.beanstalkHost])
|
39
|
-
rescue Exception => e
|
40
|
-
puts "Error connecting to Beanstalk"
|
41
|
-
puts "Host string, #{@config.beanstalkHost}"
|
42
|
-
if e.message == "Beanstalk::NotConnected" then
|
43
|
-
puts "***Most likely, beanstalk is not running. Start beanstalk, and try running this again."
|
44
|
-
puts "***If you still get this error, check beanstalk is running at, " + @config.beanstalkHost
|
45
|
-
else
|
46
|
-
puts e.message
|
47
|
-
puts e.backtrace
|
48
|
-
end
|
49
|
-
abort()
|
50
|
-
end
|
40
|
+
def connectToMq
|
41
|
+
@mq = ConfigureMQ.new.get( @config.mqHost + "/" + @config.localQueueName, @config.queueTimeout )
|
51
42
|
|
52
43
|
return self
|
53
44
|
end
|
@@ -82,7 +73,6 @@ module RServiceBus
|
|
82
73
|
return self
|
83
74
|
end
|
84
75
|
|
85
|
-
|
86
76
|
def loadContracts()
|
87
77
|
log "Load Contracts"
|
88
78
|
|
@@ -122,13 +112,12 @@ module RServiceBus
|
|
122
112
|
return self
|
123
113
|
end
|
124
114
|
|
125
|
-
|
126
115
|
def initialize()
|
127
116
|
|
128
117
|
@config = ConfigFromEnv.new
|
129
118
|
.configureLogging()
|
130
119
|
.loadHostSection()
|
131
|
-
.
|
120
|
+
.configureMq()
|
132
121
|
.loadContracts()
|
133
122
|
.loadMessageEndpointMappings()
|
134
123
|
.loadHandlerPathList()
|
@@ -137,7 +126,7 @@ module RServiceBus
|
|
137
126
|
|
138
127
|
self.configureStatistics()
|
139
128
|
.configureAppResource()
|
140
|
-
.
|
129
|
+
.connectToMq()
|
141
130
|
.loadHandlers()
|
142
131
|
.loadContracts()
|
143
132
|
.loadLibs()
|
@@ -151,7 +140,6 @@ module RServiceBus
|
|
151
140
|
log "Starting the Host"
|
152
141
|
|
153
142
|
log "Watching, " + @config.localQueueName
|
154
|
-
@beanstalk.watch( @config.localQueueName )
|
155
143
|
if !@config.forwardReceivedMessagesTo.nil? then
|
156
144
|
log "Forwarding all received messages to: " + @config.forwardReceivedMessagesTo.to_s
|
157
145
|
end
|
@@ -165,20 +153,19 @@ module RServiceBus
|
|
165
153
|
log "Waiting for messages. To exit press CTRL+C"
|
166
154
|
statOutputCountdown = 0
|
167
155
|
messageLoop = true
|
168
|
-
|
156
|
+
|
169
157
|
while messageLoop do
|
170
158
|
retries = @config.maxRetries
|
171
159
|
#Popping a msg off the queue should not be in the message handler, as it affects retry
|
172
160
|
begin
|
173
161
|
if statOutputCountdown == 0 then
|
174
|
-
log @stats.getForReporting
|
162
|
+
# log @stats.getForReporting
|
175
163
|
statOutputCountdown = @config.statOutputCountdown-1
|
176
164
|
else
|
177
165
|
statOutputCountdown = statOutputCountdown - 1
|
178
166
|
end
|
179
|
-
|
167
|
+
body = @mq.pop
|
180
168
|
begin
|
181
|
-
body = job.body
|
182
169
|
@stats.incTotalProcessed
|
183
170
|
@msg = YAML::load(body)
|
184
171
|
if @msg.msg.class.name == "RServiceBus::Message_Subscription" then
|
@@ -190,10 +177,12 @@ module RServiceBus
|
|
190
177
|
self._SendAlreadyWrappedAndSerialised(body,@config.forwardReceivedMessagesTo)
|
191
178
|
end
|
192
179
|
end
|
193
|
-
|
180
|
+
@mq.ack
|
194
181
|
rescue Exception => e
|
195
182
|
sleep 0.5
|
196
|
-
|
183
|
+
|
184
|
+
puts e.message
|
185
|
+
puts e.backtrace
|
197
186
|
|
198
187
|
@handlerList[@msg.msg.class.name].each do |handler|
|
199
188
|
@resourceByHandlerNameList[handler.class.name].each do |resource|
|
@@ -223,140 +212,141 @@ module RServiceBus
|
|
223
212
|
@msg.addErrorMsg( @config.localQueueName, errorString )
|
224
213
|
serialized_object = YAML::dump(@msg)
|
225
214
|
self._SendAlreadyWrappedAndSerialised(serialized_object, @config.errorQueueName)
|
226
|
-
|
215
|
+
@mq.ack
|
227
216
|
end
|
228
217
|
rescue SystemExit, Interrupt
|
229
218
|
puts "Exiting on request ..."
|
230
219
|
messageLoop = false
|
220
|
+
|
221
|
+
rescue NoMsgToProcess => e
|
222
|
+
#This exception is just saying there are no messages to process
|
223
|
+
statOutputCountdown = 0
|
231
224
|
rescue Exception => e
|
232
|
-
if e.message == "
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
messageLoop = false
|
241
|
-
puts "Message: " + e.message
|
242
|
-
puts e.backtrace
|
243
|
-
end
|
225
|
+
if e.message == "SIGTERM" then
|
226
|
+
puts "Exiting on request ..."
|
227
|
+
messageLoop = false
|
228
|
+
else
|
229
|
+
puts "*** This is really unexpected."
|
230
|
+
messageLoop = false
|
231
|
+
puts "Message: " + e.message
|
232
|
+
puts e.backtrace
|
244
233
|
end
|
245
234
|
end
|
246
235
|
end
|
236
|
+
end
|
237
|
+
|
238
|
+
#Send the current msg to the appropriate handlers
|
239
|
+
def HandleMessage()
|
240
|
+
msgName = @msg.msg.class.name
|
241
|
+
handlerList = @handlerList[msgName]
|
247
242
|
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
log "An error occured in Handler: " + handler.class.name
|
263
|
-
log e.message + ". " + e.backtrace[0]
|
264
|
-
raise e
|
265
|
-
end
|
243
|
+
if handlerList == nil then
|
244
|
+
log "No handler found for: " + msgName
|
245
|
+
puts "No handler found for: " + msgName
|
246
|
+
puts YAML::dump(@msg)
|
247
|
+
raise "No Handler Found"
|
248
|
+
else
|
249
|
+
log "Handler found for: " + msgName, true
|
250
|
+
handlerList.each do |handler|
|
251
|
+
begin
|
252
|
+
handler.Handle( @msg.msg )
|
253
|
+
rescue Exception => e
|
254
|
+
log "An error occured in Handler: " + handler.class.name
|
255
|
+
log e.message + ". " + e.backtrace[0]
|
256
|
+
raise e
|
266
257
|
end
|
267
258
|
end
|
268
259
|
end
|
260
|
+
end
|
261
|
+
|
262
|
+
#Sends a msg across the bus
|
263
|
+
#
|
264
|
+
# @param [String] serialized_object serialized RServiceBus::Message
|
265
|
+
# @param [String] queueName endpoint to which the msg will be sent
|
266
|
+
def _SendAlreadyWrappedAndSerialised( serialized_object, queueName )
|
267
|
+
log "Bus._SendAlreadyWrappedAndSerialised", true
|
269
268
|
|
270
|
-
|
271
|
-
|
272
|
-
# @param [String] serialized_object serialized RServiceBus::Message
|
273
|
-
# @param [String] queueName endpoint to which the msg will be sent
|
274
|
-
def _SendAlreadyWrappedAndSerialised( serialized_object, queueName )
|
275
|
-
log "Bus._SendAlreadyWrappedAndSerialised", true
|
276
|
-
|
277
|
-
if !@config.auditQueueName.nil? then
|
278
|
-
@beanstalk.use( @config.auditQueueName )
|
279
|
-
@beanstalk.put( serialized_object )
|
280
|
-
end
|
281
|
-
|
282
|
-
@beanstalk.use( queueName )
|
283
|
-
@beanstalk.put( serialized_object )
|
269
|
+
if !@config.auditQueueName.nil? then
|
270
|
+
@mq.send( @config.auditQueueName, serialized_object )
|
284
271
|
end
|
285
272
|
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
log "Sending: " + msg.class.name + " to: " + queueName, true
|
296
|
-
self._SendAlreadyWrappedAndSerialised( serialized_object, queueName )
|
297
|
-
end
|
273
|
+
@mq.send( queueName, serialized_object )
|
274
|
+
end
|
275
|
+
|
276
|
+
#Sends a msg across the bus
|
277
|
+
#
|
278
|
+
# @param [RServiceBus::Message] msg msg to be sent
|
279
|
+
# @param [String] queueName endpoint to which the msg will be sent
|
280
|
+
def _SendNeedsWrapping( msg, queueName )
|
281
|
+
log "Bus._SendNeedsWrapping", true
|
298
282
|
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
283
|
+
rMsg = RServiceBus::Message.new( msg, @config.localQueueName )
|
284
|
+
serialized_object = YAML::dump(rMsg)
|
285
|
+
log "Sending: " + msg.class.name + " to: " + queueName, true
|
286
|
+
self._SendAlreadyWrappedAndSerialised( serialized_object, queueName )
|
287
|
+
end
|
288
|
+
|
289
|
+
#Sends a msg back across the bus
|
290
|
+
#Reply queues are specified in each msg. It works like
|
291
|
+
#email, where the reply address can actually be anywhere
|
292
|
+
#
|
293
|
+
# @param [RServiceBus::Message] msg msg to be sent
|
294
|
+
def Reply( msg )
|
295
|
+
log "Reply with: " + msg.class.name + " To: " + @msg.returnAddress, true
|
296
|
+
@stats.incTotalReply
|
310
297
|
|
298
|
+
self._SendNeedsWrapping( msg, @msg.returnAddress )
|
299
|
+
end
|
300
|
+
|
301
|
+
|
302
|
+
#Send a msg across the bus
|
303
|
+
#msg destination is specified at the infrastructure level
|
304
|
+
#
|
305
|
+
# @param [RServiceBus::Message] msg msg to be sent
|
306
|
+
def Send( msg )
|
307
|
+
log "Bus.Send", true
|
308
|
+
@stats.incTotalSent
|
311
309
|
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
log "Bus.Send", true
|
318
|
-
@stats.incTotalSent
|
319
|
-
|
320
|
-
msgName = msg.class.name
|
321
|
-
if !@config.messageEndpointMappings.has_key?( msgName ) then
|
322
|
-
log "No end point mapping found for: " + msgName
|
323
|
-
log "**** Check in RServiceBus.yml that the section MessageEndpointMappings contains an entry named : " + msgName
|
324
|
-
raise "No end point mapping found for: " + msgName
|
325
|
-
end
|
326
|
-
|
327
|
-
queueName = @config.messageEndpointMappings[msgName]
|
328
|
-
|
329
|
-
self._SendNeedsWrapping( msg, queueName )
|
310
|
+
msgName = msg.class.name
|
311
|
+
if !@config.messageEndpointMappings.has_key?( msgName ) then
|
312
|
+
log "No end point mapping found for: " + msgName
|
313
|
+
log "**** Check in RServiceBus.yml that the section MessageEndpointMappings contains an entry named : " + msgName
|
314
|
+
raise "No end point mapping found for: " + msgName
|
330
315
|
end
|
331
316
|
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
end
|
317
|
+
queueName = @config.messageEndpointMappings[msgName]
|
318
|
+
|
319
|
+
self._SendNeedsWrapping( msg, queueName )
|
320
|
+
end
|
321
|
+
|
322
|
+
#Sends an event to all subscribers across the bus
|
323
|
+
#
|
324
|
+
# @param [RServiceBus::Message] msg msg to be sent
|
325
|
+
def Publish( msg )
|
326
|
+
log "Bus.Publish", true
|
327
|
+
@stats.incTotalPublished
|
345
328
|
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
def Subscribe( eventName )
|
350
|
-
log "Bus.Subscribe: " + eventName, true
|
351
|
-
|
352
|
-
|
353
|
-
queueName = @config.messageEndpointMappings[eventName]
|
354
|
-
subscription = Message_Subscription.new( eventName )
|
355
|
-
|
356
|
-
|
357
|
-
self._SendNeedsWrapping( subscription, queueName )
|
329
|
+
subscriptions = @subscriptionManager.get(msg.class.name)
|
330
|
+
subscriptions.each do |subscriber|
|
331
|
+
self._SendNeedsWrapping( msg, subscriber )
|
358
332
|
end
|
359
333
|
|
360
334
|
end
|
361
335
|
|
336
|
+
#Sends a subscription request across the Bus
|
337
|
+
#
|
338
|
+
# @param [String] eventName event to be subscribes to
|
339
|
+
def Subscribe( eventName )
|
340
|
+
log "Bus.Subscribe: " + eventName, true
|
341
|
+
|
342
|
+
|
343
|
+
queueName = @config.messageEndpointMappings[eventName]
|
344
|
+
subscription = Message_Subscription.new( eventName )
|
345
|
+
|
346
|
+
|
347
|
+
self._SendNeedsWrapping( subscription, queueName )
|
348
|
+
end
|
349
|
+
|
362
350
|
end
|
351
|
+
|
352
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
module RServiceBus
|
2
|
+
|
3
|
+
require "uri"
|
4
|
+
|
5
|
+
# Wrapper base class for Queue implementations available to the applications, allowing rservicebus to instatiate and configure
|
6
|
+
# queue implementations at startup
|
7
|
+
# - dependency injection.
|
8
|
+
#
|
9
|
+
class MQ
|
10
|
+
@uri
|
11
|
+
|
12
|
+
# Resources are attached resources, and can be specified using the URI syntax.
|
13
|
+
#
|
14
|
+
# @param [URI] uri the type and location of queue, eg bunny://127.0.0.1/foo
|
15
|
+
# @param [Integer] timeout the amount of time to wait for a msg to arrive
|
16
|
+
def initialize( uri, timeout )
|
17
|
+
@timeout = timeout
|
18
|
+
if uri.is_a? URI then
|
19
|
+
@uri = uri
|
20
|
+
else
|
21
|
+
puts "uri must be a valid URI"
|
22
|
+
abort()
|
23
|
+
end
|
24
|
+
|
25
|
+
host = uri.host
|
26
|
+
port = uri.port
|
27
|
+
queue = uri.path.sub( "/", "" )
|
28
|
+
|
29
|
+
if ( queue == "" )
|
30
|
+
puts "Queue name must be supplied "
|
31
|
+
puts "*** uri, #{uri}, needs to contain a queue name"
|
32
|
+
puts "*** the structure is scheme://host[:port]/queuename"
|
33
|
+
abort()
|
34
|
+
end
|
35
|
+
|
36
|
+
self.connect(uri.host, uri.port)
|
37
|
+
self.subscribe( queue )
|
38
|
+
end
|
39
|
+
|
40
|
+
# Connect to the broker
|
41
|
+
#
|
42
|
+
def connect( host, port )
|
43
|
+
raise "Method, connect, needs to be implemented"
|
44
|
+
end
|
45
|
+
|
46
|
+
# Connect to the queue
|
47
|
+
#
|
48
|
+
def subscribe( queuename )
|
49
|
+
raise "Method, subscribe, needs to be implemented"
|
50
|
+
end
|
51
|
+
|
52
|
+
# Get next msg from queue
|
53
|
+
def pop
|
54
|
+
raise "Method, pop, needs to be implemented"
|
55
|
+
end
|
56
|
+
|
57
|
+
# "Commit" queue
|
58
|
+
def ack
|
59
|
+
raise "Method, ack, needs to be implemented"
|
60
|
+
end
|
61
|
+
|
62
|
+
# At least called in the Host rescue block, to ensure all network links are healthy
|
63
|
+
def send( queueName, msg )
|
64
|
+
begin
|
65
|
+
@connection.close
|
66
|
+
rescue
|
67
|
+
puts "AppResource. An error was raised while closing connection to, " + @uri.to_s
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module RServiceBus
|
2
|
+
|
3
|
+
require "beanstalk-client"
|
4
|
+
require "rservicebus/MQ"
|
5
|
+
|
6
|
+
# Beanstalk client implementation.
|
7
|
+
#
|
8
|
+
class MQ_Beanstalk<MQ
|
9
|
+
|
10
|
+
# Connect to the broker
|
11
|
+
#
|
12
|
+
def connect( host, port )
|
13
|
+
port ||= 11300
|
14
|
+
string = "#{host}:#{port}"
|
15
|
+
|
16
|
+
begin
|
17
|
+
@beanstalk = Beanstalk::Pool.new([string])
|
18
|
+
rescue Exception => e
|
19
|
+
puts "Error connecting to Beanstalk"
|
20
|
+
puts "Host string, #{string}"
|
21
|
+
if e.message == "Beanstalk::NotConnected" then
|
22
|
+
puts "***Most likely, beanstalk is not running. Start beanstalk, and try running this again."
|
23
|
+
puts "***If you still get this error, check beanstalk is running at, #{string}"
|
24
|
+
else
|
25
|
+
puts e.message
|
26
|
+
puts e.backtrace
|
27
|
+
end
|
28
|
+
abort()
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# Connect to the queue
|
33
|
+
#
|
34
|
+
def subscribe( queuename )
|
35
|
+
@beanstalk.watch( queuename )
|
36
|
+
end
|
37
|
+
|
38
|
+
# Get next msg from queue
|
39
|
+
def pop
|
40
|
+
begin
|
41
|
+
@job = @beanstalk.reserve @timeout
|
42
|
+
rescue Exception => e
|
43
|
+
if e.message == "TIMED_OUT" then
|
44
|
+
raise NoMsgToProcess.new
|
45
|
+
end
|
46
|
+
raise e
|
47
|
+
end
|
48
|
+
return @job.body
|
49
|
+
end
|
50
|
+
|
51
|
+
# "Commit" queue
|
52
|
+
def ack
|
53
|
+
@job.delete
|
54
|
+
@job = nil;
|
55
|
+
end
|
56
|
+
|
57
|
+
# At least called in the Host rescue block, to ensure all network links are healthy
|
58
|
+
def send( queueName, msg )
|
59
|
+
@beanstalk.use( queueName )
|
60
|
+
@beanstalk.put( msg )
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module RServiceBus
|
2
|
+
|
3
|
+
require "bunny"
|
4
|
+
require "rservicebus/MQ"
|
5
|
+
|
6
|
+
# RabbitMQ client implementation.
|
7
|
+
#
|
8
|
+
class MQ_Bunny<MQ
|
9
|
+
@uri
|
10
|
+
|
11
|
+
# Connect to the broker
|
12
|
+
#
|
13
|
+
def connect( host, port )
|
14
|
+
port ||= 5672
|
15
|
+
|
16
|
+
@bunny = Bunny.new(:host=>host, :port=>port)
|
17
|
+
@bunny.start
|
18
|
+
@pop_exch = @bunny.exchange('rservicebus.pop')
|
19
|
+
@send_exch = @bunny.exchange('rservicebus.send')
|
20
|
+
end
|
21
|
+
|
22
|
+
# Connect to the queue
|
23
|
+
#
|
24
|
+
def subscribe( queueName )
|
25
|
+
@queue = @bunny.queue( queueName )
|
26
|
+
@queue.bind( @pop_exch );
|
27
|
+
end
|
28
|
+
|
29
|
+
# Get next msg from queue
|
30
|
+
def pop
|
31
|
+
msg = @queue.pop(:ack => true)[:payload]
|
32
|
+
|
33
|
+
if msg == :queue_empty then
|
34
|
+
raise NoMsgToProcess.new
|
35
|
+
end
|
36
|
+
|
37
|
+
return msg
|
38
|
+
end
|
39
|
+
|
40
|
+
# "Commit" the pop to the queue
|
41
|
+
def ack
|
42
|
+
@queue.ack
|
43
|
+
end
|
44
|
+
|
45
|
+
# Send a msg to a queue
|
46
|
+
def send( queueName, msg )
|
47
|
+
queue = @bunny.queue(queueName)
|
48
|
+
queue.bind(@send_exch)
|
49
|
+
@send_exch.publish(msg)
|
50
|
+
queue.unbind(@send_exch)
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rservicebus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.21
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-10-
|
12
|
+
date: 2012-10-10 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: A Ruby interpretation of NServiceBus
|
15
15
|
email: guy@guyirvine.com
|
@@ -19,7 +19,8 @@ executables:
|
|
19
19
|
extensions: []
|
20
20
|
extra_rdoc_files: []
|
21
21
|
files:
|
22
|
-
- lib/rservicebus/Agent.rb
|
22
|
+
- lib/rservicebus/Agent/Beanstalk.rb
|
23
|
+
- lib/rservicebus/Agent/Bunny.rb
|
23
24
|
- lib/rservicebus/AppResource/FluidDbMysql.rb
|
24
25
|
- lib/rservicebus/AppResource/FluidDbMysql2.rb
|
25
26
|
- lib/rservicebus/AppResource/FluidDbPgsql.rb
|
@@ -28,12 +29,16 @@ files:
|
|
28
29
|
- lib/rservicebus/AppResource.rb
|
29
30
|
- lib/rservicebus/Config.rb
|
30
31
|
- lib/rservicebus/ConfigureAppResource.rb
|
32
|
+
- lib/rservicebus/ConfigureMQ.rb
|
31
33
|
- lib/rservicebus/ErrorMessage.rb
|
32
34
|
- lib/rservicebus/HandlerLoader.rb
|
33
35
|
- lib/rservicebus/helper_functions.rb
|
34
36
|
- lib/rservicebus/Host.rb
|
35
37
|
- lib/rservicebus/Message/Subscription.rb
|
36
38
|
- lib/rservicebus/Message.rb
|
39
|
+
- lib/rservicebus/MQ/Beanstalk.rb
|
40
|
+
- lib/rservicebus/MQ/Bunny.rb
|
41
|
+
- lib/rservicebus/MQ.rb
|
37
42
|
- lib/rservicebus/Stats.rb
|
38
43
|
- lib/rservicebus/SubscriptionManager.rb
|
39
44
|
- lib/rservicebus/SubscriptionStorage/Redis.rb
|