rservicebus 0.0.13 → 0.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,26 +1,42 @@
|
|
1
1
|
module RServiceBus
|
2
|
-
|
3
|
-
require "uri"
|
4
|
-
|
5
|
-
# Wrapper base class for resources used by applications, allowing rservicebus to configure the resource
|
6
|
-
# - dependency injection.
|
7
|
-
#
|
8
|
-
class AppResource
|
9
|
-
|
10
|
-
|
11
|
-
# Resources are attached resources, and can be specified using the URI syntax.
|
12
|
-
#
|
13
|
-
# @param [String] uri a location for the resource to which we will attach, eg redis://127.0.0.1/foo
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
# The method which actually configures the resource.
|
19
|
-
#
|
20
|
-
# @return [Object] the configured object.
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
2
|
+
|
3
|
+
require "uri"
|
4
|
+
|
5
|
+
# Wrapper base class for resources used by applications, allowing rservicebus to configure the resource
|
6
|
+
# - dependency injection.
|
7
|
+
#
|
8
|
+
class AppResource
|
9
|
+
@uri
|
10
|
+
|
11
|
+
# Resources are attached resources, and can be specified using the URI syntax.
|
12
|
+
#
|
13
|
+
# @param [String] uri a location for the resource to which we will attach, eg redis://127.0.0.1/foo
|
14
|
+
def initialize( uri )
|
15
|
+
@uri = uri
|
16
|
+
end
|
17
|
+
|
18
|
+
# The method which actually configures the resource.
|
19
|
+
#
|
20
|
+
# @return [Object] the configured object.
|
21
|
+
def getResource
|
22
|
+
raise "Method, getResource, needs to be implemented for resource"
|
23
|
+
end
|
24
|
+
|
25
|
+
# A notification that ocurs after getResource, to allow cleanup
|
26
|
+
def finished
|
27
|
+
raise "Method, getResource, needs to be implemented for resource"
|
28
|
+
end
|
29
|
+
|
30
|
+
# At least called in the Host rescue block, to ensure all network links are healthy
|
31
|
+
def reconnect
|
32
|
+
begin
|
33
|
+
@connection.close
|
34
|
+
rescue
|
35
|
+
puts "AppResource. An error was raised while closing connection to, " + @uri.to_s
|
36
|
+
end
|
37
|
+
|
38
|
+
self.connect
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
26
42
|
end
|
@@ -1,12 +1,12 @@
|
|
1
1
|
module RServiceBus
|
2
2
|
|
3
3
|
require "FluidDb/Mysql"
|
4
|
-
|
4
|
+
|
5
5
|
#Implementation of an AppResource - Redis
|
6
6
|
class AppResource_FluidDbMysql<AppResource
|
7
7
|
|
8
8
|
@connection
|
9
|
-
|
9
|
+
|
10
10
|
def initialize( uri )
|
11
11
|
super(uri)
|
12
12
|
host = uri.host
|
@@ -16,7 +16,7 @@ module RServiceBus
|
|
16
16
|
@connection = FluidDb::Mysql.new( uri )
|
17
17
|
puts "AppResource_Mysql. Connected to, " + uri.to_s
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
def getResource
|
21
21
|
return @connection
|
22
22
|
end
|
@@ -6,23 +6,30 @@ module RServiceBus
|
|
6
6
|
class AppResource_Mysql<AppResource
|
7
7
|
|
8
8
|
@connection
|
9
|
-
|
10
|
-
def
|
11
|
-
|
9
|
+
|
10
|
+
def connect()
|
11
|
+
uri = self.uri
|
12
12
|
host = uri.host
|
13
13
|
database = uri.path.sub( "/", "" )
|
14
|
-
|
15
|
-
|
14
|
+
|
15
|
+
|
16
16
|
@connection = Mysql2::Client.new(:host => uri.host,
|
17
|
-
|
18
|
-
|
17
|
+
:database => uri.path.sub( "/", "" ),
|
18
|
+
:username => uri.user )
|
19
19
|
puts "AppResource_Mysql. Connected to, " + uri.to_s
|
20
20
|
end
|
21
21
|
|
22
|
+
def initialize( uri )
|
23
|
+
super(uri)
|
24
|
+
self.connect
|
25
|
+
end
|
26
|
+
|
22
27
|
def getResource
|
23
28
|
return @connection
|
24
29
|
end
|
25
|
-
|
30
|
+
|
26
31
|
end
|
27
32
|
|
28
33
|
end
|
34
|
+
|
35
|
+
end
|
@@ -21,6 +21,7 @@ class HandlerLoader
|
|
21
21
|
@handler
|
22
22
|
|
23
23
|
@handlerList
|
24
|
+
@resourceList
|
24
25
|
|
25
26
|
# Constructor
|
26
27
|
#
|
@@ -31,6 +32,7 @@ class HandlerLoader
|
|
31
32
|
@appResources = appResources
|
32
33
|
|
33
34
|
@handlerList = Hash.new
|
35
|
+
@resourceList = Hash.new
|
34
36
|
end
|
35
37
|
|
36
38
|
# Cleans the given path to ensure it can be used for as a parameter for the require statement.
|
@@ -93,6 +95,8 @@ class HandlerLoader
|
|
93
95
|
appResources.each do |k,v|
|
94
96
|
if handler.class.method_defined?( k ) then
|
95
97
|
handler.instance_variable_set( "@#{k}", v.getResource() )
|
98
|
+
@resourceList[handler.class.name] = Array.new if @resourceList[handler.class.name].nil?
|
99
|
+
@resourceList[handler.class.name] << v.getResource()
|
96
100
|
@host.log "App resource attribute, #{k}, set for: " + handler.class.name
|
97
101
|
end
|
98
102
|
end
|
data/lib/rservicebus/Host.rb
CHANGED
@@ -1,129 +1,130 @@
|
|
1
1
|
module RServiceBus
|
2
|
-
|
3
|
-
#Host process for rservicebus
|
4
|
-
class Host
|
5
|
-
|
6
|
-
@handlerList
|
7
|
-
|
8
|
-
@forwardReceivedMessagesToQueue
|
9
|
-
|
10
|
-
@subscriptions
|
11
|
-
|
12
|
-
@beanstalk
|
13
|
-
|
14
|
-
@appResources
|
15
|
-
|
16
|
-
@config
|
17
|
-
|
18
|
-
@subscriptionManager
|
19
|
-
|
20
|
-
@stats
|
21
|
-
|
22
|
-
def log(string, ver=false)
|
23
|
-
type = ver ? "VERB" : "INFO"
|
24
|
-
if @config.verbose || !ver then
|
25
|
-
timestamp = Time.new.strftime( "%Y-%m-%d %H:%M:%S" )
|
26
|
-
puts "[#{type}] #{timestamp} :: #{string}"
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
def configureAppResource
|
31
|
-
@appResources = ConfigureAppResource.new.getResources( ENV )
|
32
|
-
return self;
|
33
|
-
end
|
34
|
-
|
35
|
-
def connectToBeanstalk
|
36
|
-
begin
|
37
|
-
@beanstalk = Beanstalk::Pool.new([@config.beanstalkHost])
|
38
|
-
rescue Exception => e
|
39
|
-
puts "Error connecting to Beanstalk"
|
40
|
-
puts "Host string, #{@config.beanstalkHost}"
|
41
|
-
if e.message == "Beanstalk::NotConnected" then
|
42
|
-
puts "***Most likely, beanstalk is not running. Start beanstalk, and try running this again."
|
43
|
-
puts "***If you still get this error, check beanstalk is running at, " + @config.beanstalkHost
|
44
|
-
else
|
45
|
-
puts e.message
|
46
|
-
puts e.backtrace
|
47
|
-
end
|
48
|
-
abort()
|
49
|
-
end
|
50
|
-
|
51
|
-
return self
|
52
|
-
end
|
53
|
-
|
54
|
-
#Subscriptions are specified by adding events to the
|
55
|
-
#msg endpoint mapping
|
56
|
-
def sendSubscriptions
|
57
|
-
log "Send Subscriptions"
|
58
|
-
@config.messageEndpointMappings.each do |eventName,queueName|
|
59
|
-
log "Checking, " + eventName + " for Event", true
|
60
|
-
if eventName.end_with?( "Event" ) then
|
61
|
-
log eventName + ", is an event. About to send subscription to, " + queueName, true
|
62
|
-
self.Subscribe( eventName )
|
63
|
-
log "Subscribed to, " + eventName + " at, " + queueName
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
return self
|
68
|
-
end
|
69
|
-
|
70
|
-
def loadHandlers()
|
71
|
-
log "Load Message Handlers"
|
72
|
-
handlerLoader = HandlerLoader.new( self, @appResources )
|
73
|
-
|
74
|
-
@config.handlerPathList.each do |path|
|
75
|
-
handlerLoader.loadHandlersFromPath(path)
|
76
|
-
end
|
77
|
-
|
78
|
-
@handlerList = handlerLoader.handlerList
|
79
|
-
|
80
|
-
return self
|
81
|
-
end
|
82
|
-
|
83
2
|
|
84
|
-
|
85
|
-
|
3
|
+
#Host process for rservicebus
|
4
|
+
class Host
|
86
5
|
|
87
|
-
|
88
|
-
|
89
|
-
end
|
6
|
+
@handlerList
|
7
|
+
@resourceByHandlerNameList
|
90
8
|
|
91
|
-
|
92
|
-
end
|
93
|
-
|
94
|
-
def loadLibs()
|
95
|
-
log "Load Libs"
|
9
|
+
@forwardReceivedMessagesToQueue
|
96
10
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
11
|
+
@subscriptions
|
12
|
+
|
13
|
+
@beanstalk
|
14
|
+
|
15
|
+
@appResources
|
16
|
+
|
17
|
+
@config
|
18
|
+
|
19
|
+
@subscriptionManager
|
20
|
+
|
21
|
+
@stats
|
22
|
+
|
23
|
+
def log(string, ver=false)
|
24
|
+
type = ver ? "VERB" : "INFO"
|
25
|
+
if @config.verbose || !ver then
|
26
|
+
timestamp = Time.new.strftime( "%Y-%m-%d %H:%M:%S" )
|
27
|
+
puts "[#{type}] #{timestamp} :: #{string}"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def configureAppResource
|
32
|
+
@appResources = ConfigureAppResource.new.getResources( ENV )
|
33
|
+
return self;
|
34
|
+
end
|
35
|
+
|
36
|
+
def connectToBeanstalk
|
37
|
+
begin
|
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()
|
101
50
|
end
|
102
|
-
|
51
|
+
|
52
|
+
return self
|
53
|
+
end
|
54
|
+
|
55
|
+
#Subscriptions are specified by adding events to the
|
56
|
+
#msg endpoint mapping
|
57
|
+
def sendSubscriptions
|
58
|
+
log "Send Subscriptions"
|
59
|
+
@config.messageEndpointMappings.each do |eventName,queueName|
|
60
|
+
log "Checking, " + eventName + " for Event", true
|
61
|
+
if eventName.end_with?( "Event" ) then
|
62
|
+
log eventName + ", is an event. About to send subscription to, " + queueName, true
|
63
|
+
self.Subscribe( eventName )
|
64
|
+
log "Subscribed to, " + eventName + " at, " + queueName
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
return self
|
69
|
+
end
|
70
|
+
|
71
|
+
def loadHandlers()
|
72
|
+
log "Load Message Handlers"
|
73
|
+
handlerLoader = HandlerLoader.new( self, @appResources )
|
74
|
+
|
75
|
+
@config.handlerPathList.each do |path|
|
76
|
+
handlerLoader.loadHandlersFromPath(path)
|
77
|
+
end
|
78
|
+
|
79
|
+
@handlerList = handlerLoader.handlerList
|
80
|
+
|
81
|
+
return self
|
82
|
+
end
|
83
|
+
|
84
|
+
|
85
|
+
def loadContracts()
|
86
|
+
log "Load Contracts"
|
87
|
+
|
88
|
+
@config.contractList.each do |path|
|
103
89
|
require path
|
104
90
|
end
|
105
|
-
|
91
|
+
|
92
|
+
return self
|
93
|
+
end
|
94
|
+
|
95
|
+
def loadLibs()
|
96
|
+
log "Load Libs"
|
97
|
+
|
98
|
+
@config.libList.each do |path|
|
99
|
+
if Dir.exists?( path ) then
|
100
|
+
path = path.strip.chomp( "/" )
|
101
|
+
path = path + "/**/*.rb"
|
102
|
+
end
|
103
|
+
Dir.glob( path ).each do |path|
|
104
|
+
require path
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
return self
|
109
|
+
end
|
106
110
|
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
def initialize()
|
125
|
-
|
126
|
-
@config = ConfigFromEnv.new
|
111
|
+
def configureSubscriptions
|
112
|
+
subscriptionStorage = SubscriptionStorage_Redis.new( @config.appName, "uri" )
|
113
|
+
@subscriptionManager = SubscriptionManager.new( subscriptionStorage )
|
114
|
+
|
115
|
+
return self
|
116
|
+
end
|
117
|
+
|
118
|
+
def configureStatistics
|
119
|
+
@stats = Stats.new
|
120
|
+
|
121
|
+
return self
|
122
|
+
end
|
123
|
+
|
124
|
+
|
125
|
+
def initialize()
|
126
|
+
|
127
|
+
@config = ConfigFromEnv.new
|
127
128
|
.configureLogging()
|
128
129
|
.loadHostSection()
|
129
130
|
.configureBeanstalk()
|
@@ -132,8 +133,8 @@ class Host
|
|
132
133
|
.loadHandlerPathList()
|
133
134
|
.loadLibs()
|
134
135
|
.loadWorkingDirList();
|
135
|
-
|
136
|
-
|
136
|
+
|
137
|
+
self.configureStatistics()
|
137
138
|
.configureAppResource()
|
138
139
|
.connectToBeanstalk()
|
139
140
|
.loadHandlers()
|
@@ -141,212 +142,221 @@ class Host
|
|
141
142
|
.loadLibs()
|
142
143
|
.configureSubscriptions()
|
143
144
|
.sendSubscriptions()
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
#Receive a msg, prep it, and handle any errors that may occur
|
161
|
-
# - Most of this should be queue independant
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
#
|
277
|
-
#
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
#
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
145
|
+
|
146
|
+
return self
|
147
|
+
end
|
148
|
+
|
149
|
+
def run
|
150
|
+
log "Starting the Host"
|
151
|
+
|
152
|
+
log "Watching, " + @config.localQueueName
|
153
|
+
@beanstalk.watch( @config.localQueueName )
|
154
|
+
if !@config.forwardReceivedMessagesTo.nil? then
|
155
|
+
log "Forwarding all received messages to: " + @config.forwardReceivedMessagesTo.to_s
|
156
|
+
end
|
157
|
+
|
158
|
+
self.StartListeningToEndpoints
|
159
|
+
end
|
160
|
+
|
161
|
+
#Receive a msg, prep it, and handle any errors that may occur
|
162
|
+
# - Most of this should be queue independant
|
163
|
+
def StartListeningToEndpoints
|
164
|
+
log "Waiting for messages. To exit press CTRL+C"
|
165
|
+
statOutputCountdown = 0
|
166
|
+
messageLoop = true
|
167
|
+
|
168
|
+
while messageLoop do
|
169
|
+
retries = @config.maxRetries
|
170
|
+
#Popping a msg off the queue should not be in the message handler, as it affects retry
|
171
|
+
begin
|
172
|
+
if statOutputCountdown == 0 then
|
173
|
+
log @stats.getForReporting
|
174
|
+
statOutputCountdown = @config.statOutputCountdown-1
|
175
|
+
else
|
176
|
+
statOutputCountdown = statOutputCountdown - 1
|
177
|
+
end
|
178
|
+
job = @beanstalk.reserve @config.queueTimeout
|
179
|
+
begin
|
180
|
+
body = job.body
|
181
|
+
@stats.incTotalProcessed
|
182
|
+
@msg = YAML::load(body)
|
183
|
+
if @msg.msg.class.name == "RServiceBus::Message_Subscription" then
|
184
|
+
@subscriptionManager.add( @msg.msg.eventName, @msg.returnAddress )
|
185
|
+
|
186
|
+
else
|
187
|
+
self.HandleMessage()
|
188
|
+
if !@config.forwardReceivedMessagesTo.nil? then
|
189
|
+
self._SendAlreadyWrappedAndSerialised(body,@config.forwardReceivedMessagesTo)
|
190
|
+
end
|
191
|
+
end
|
192
|
+
job.delete
|
193
|
+
rescue Exception => e
|
194
|
+
sleep 0.5
|
195
|
+
|
196
|
+
|
197
|
+
@handlerList[@msg.msg.class.name].each do |handler|
|
198
|
+
resourceByHandlerNameList[handler.class.name].each do |resource|
|
199
|
+
resource.reconnect
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
|
204
|
+
retry if (retries -= 1) > 0
|
205
|
+
|
206
|
+
@stats.incTotalErrored
|
207
|
+
if e.class.name == "Beanstalk::NotConnected" then
|
208
|
+
puts "Lost connection to beanstalkd."
|
209
|
+
puts "*** Start or Restart beanstalkd and try again."
|
210
|
+
abort();
|
211
|
+
end
|
212
|
+
|
213
|
+
if e.class.name == "Redis::CannotConnectError" then
|
214
|
+
puts "Lost connection to redis."
|
215
|
+
puts "*** Start or Restart redis and try again."
|
216
|
+
abort();
|
217
|
+
end
|
218
|
+
|
219
|
+
errorString = e.message + ". " + e.backtrace[0]
|
220
|
+
if e.backtrace.length > 1 then
|
221
|
+
errorString = errorString + ". " + e.backtrace[1]
|
222
|
+
end
|
223
|
+
if e.backtrace.length > 2 then
|
224
|
+
errorString = errorString + ". " + e.backtrace[2]
|
225
|
+
end
|
226
|
+
log errorString
|
227
|
+
|
228
|
+
@msg.addErrorMsg( @config.localQueueName, errorString )
|
229
|
+
serialized_object = YAML::dump(@msg)
|
230
|
+
self._SendAlreadyWrappedAndSerialised(serialized_object, @config.errorQueueName)
|
231
|
+
job.delete
|
232
|
+
end
|
233
|
+
rescue SystemExit, Interrupt
|
234
|
+
puts "Exiting on request ..."
|
235
|
+
messageLoop = false
|
236
|
+
rescue Exception => e
|
237
|
+
if e.message == "TIMED_OUT" then
|
238
|
+
#This exception is just saying there are no messages to process
|
239
|
+
statOutputCountdown = 0
|
240
|
+
elsif e.message == "SIGTERM" then
|
241
|
+
puts "Exiting on request ..."
|
242
|
+
messageLoop = false
|
243
|
+
else
|
244
|
+
puts "*** This is really unexpected."
|
245
|
+
messageLoop = false
|
246
|
+
puts "Message: " + e.message
|
247
|
+
puts e.backtrace
|
248
|
+
end
|
249
|
+
end
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
253
|
+
#Send the current msg to the appropriate handlers
|
254
|
+
def HandleMessage()
|
255
|
+
msgName = @msg.msg.class.name
|
256
|
+
handlerList = @handlerList[msgName]
|
257
|
+
|
258
|
+
if handlerList == nil then
|
259
|
+
log "No handler found for: " + msgName
|
260
|
+
raise "No Handler Found"
|
261
|
+
else
|
262
|
+
log "Handler found for: " + msgName, true
|
263
|
+
handlerList.each do |handler|
|
264
|
+
begin
|
265
|
+
handler.Handle( @msg.msg )
|
266
|
+
rescue Exception => e
|
267
|
+
log "An error occured in Handler: " + handler.class.name
|
268
|
+
log e.message + ". " + e.backtrace[0]
|
269
|
+
raise e
|
270
|
+
end
|
271
|
+
end
|
272
|
+
end
|
273
|
+
end
|
274
|
+
|
275
|
+
#Sends a msg across the bus
|
276
|
+
#
|
277
|
+
# @param [String] serialized_object serialized RServiceBus::Message
|
278
|
+
# @param [String] queueName endpoint to which the msg will be sent
|
279
|
+
def _SendAlreadyWrappedAndSerialised( serialized_object, queueName )
|
280
|
+
log "Bus._SendAlreadyWrappedAndSerialised", true
|
281
|
+
|
282
|
+
@beanstalk.use( queueName )
|
283
|
+
@beanstalk.put( serialized_object )
|
284
|
+
end
|
285
|
+
|
286
|
+
#Sends a msg across the bus
|
287
|
+
#
|
288
|
+
# @param [RServiceBus::Message] msg msg to be sent
|
289
|
+
# @param [String] queueName endpoint to which the msg will be sent
|
290
|
+
def _SendNeedsWrapping( msg, queueName )
|
291
|
+
log "Bus._SendNeedsWrapping", true
|
292
|
+
|
293
|
+
rMsg = RServiceBus::Message.new( msg, @config.localQueueName )
|
294
|
+
serialized_object = YAML::dump(rMsg)
|
295
|
+
log "Sending: " + msg.class.name + " to: " + queueName, true
|
296
|
+
self._SendAlreadyWrappedAndSerialised( serialized_object, queueName )
|
297
|
+
end
|
298
|
+
|
299
|
+
#Sends a msg back across the bus
|
300
|
+
#Reply queues are specified in each msg. It works like
|
301
|
+
#email, where the reply address can actually be anywhere
|
302
|
+
#
|
303
|
+
# @param [RServiceBus::Message] msg msg to be sent
|
304
|
+
def Reply( msg )
|
305
|
+
log "Reply with: " + msg.class.name + " To: " + @msg.returnAddress, true
|
306
|
+
@stats.incTotalReply
|
307
|
+
|
308
|
+
self._SendNeedsWrapping( msg, @msg.returnAddress )
|
309
|
+
end
|
310
|
+
|
311
|
+
|
312
|
+
#Send a msg across the bus
|
313
|
+
#msg destination is specified at the infrastructure level
|
314
|
+
#
|
315
|
+
# @param [RServiceBus::Message] msg msg to be sent
|
316
|
+
def Send( msg )
|
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 )
|
330
|
+
end
|
331
|
+
|
332
|
+
#Sends an event to all subscribers across the bus
|
333
|
+
#
|
334
|
+
# @param [RServiceBus::Message] msg msg to be sent
|
335
|
+
def Publish( msg )
|
336
|
+
log "Bus.Publish", true
|
337
|
+
@stats.incTotalPublished
|
338
|
+
|
339
|
+
subscriptions = @subscriptionManager.get(msg.class.name)
|
340
|
+
subscriptions.each do |subscriber|
|
341
|
+
self._SendNeedsWrapping( msg, subscriber )
|
342
|
+
end
|
343
|
+
|
344
|
+
end
|
345
|
+
|
346
|
+
#Sends a subscription request across the Bus
|
347
|
+
#
|
348
|
+
# @param [String] eventName event to be subscribes to
|
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 )
|
358
|
+
end
|
359
|
+
|
360
|
+
end
|
361
|
+
|
362
|
+
end
|