rservicebus 0.1.48 → 0.1.49

Sign up to get free protection for your applications and to get access to all the features.
data/lib/rservicebus.rb CHANGED
@@ -15,7 +15,7 @@ require "rservicebus/ErrorMessage"
15
15
  require "rservicebus/HandlerLoader"
16
16
  require "rservicebus/HandlerManager"
17
17
  require "rservicebus/ConfigureAppResource"
18
- require "rservicebus/ConfigureMQ"
18
+ require "rservicebus/MQ"
19
19
  require "rservicebus/Host"
20
20
  require "rservicebus/Config"
21
21
  require "rservicebus/EndpointMapping"
@@ -1,17 +1,53 @@
1
1
  module RServiceBus
2
-
2
+
3
+ class QueueNotFoundForMsg<StandardError
4
+ end
5
+
3
6
  #A means for a stand-alone process to interact with the bus, without being a full
4
7
  #rservicebus application
5
8
  class Agent
6
9
 
7
- def getAgent( uri )
8
- if uri.scheme == "beanstalk" then
9
- require "rservicebus/Agent/Beanstalk"
10
- return Agent_Beanstalk.new()
10
+ @mq
11
+
12
+ def initialize
13
+ @mq = MQ.get
14
+ end
15
+
16
+ # Put a msg on the bus
17
+ #
18
+ # @param [Object] messageObj The msg to be sent
19
+ # @param [String] queueName the name of the queue to be send the msg to
20
+ # @param [String] returnAddress the name of a queue to send replies to
21
+ def sendMsg(messageObj, queueName, returnAddress=nil)
22
+ raise QueueNotFoundForMsg.new( messageObj.class.name ) if queueName.nil?
23
+
24
+ msg = RServiceBus::Message.new( messageObj, returnAddress )
25
+
26
+
27
+ if queueName.index( "@" ).nil? then
28
+ q = queueName
11
29
  else
12
- raise StandardError.new( "Scheme not recognised" )
13
-
30
+ parts = queueName.split( "@" )
31
+ msg.setRemoteQueueName( parts[0] )
32
+ msg.setRemoteHostName( parts[1] )
33
+ q = 'transport-out'
14
34
  end
35
+
36
+ serialized_object = YAML::dump(msg)
37
+
38
+ @mq.send( q, serialized_object )
39
+ end
40
+
41
+ # Gives an agent the means to receive a reply
42
+ #
43
+ # @param [String] queueName the name of the queue to monitor for messages
44
+ def checkForReply( queueName )
45
+ @mq.subscribe( queueName )
46
+ body = @mq.pop
47
+ @msg = YAML::load(body)
48
+ @mq.ack
49
+ return @msg.msg
15
50
  end
51
+
16
52
  end
17
53
  end
@@ -2,14 +2,13 @@ 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, :subscriptionUri, :queueTimeout, :statOutputCountdown, :contractList, :libList, :forwardSentMessagesTo, :mqHost
5
+ attr_reader :appName, :messageEndpointMappings, :handlerPathList, :errorQueueName, :maxRetries, :forwardReceivedMessagesTo, :subscriptionUri, :statOutputCountdown, :contractList, :libList, :forwardSentMessagesTo, :mqHost
6
6
 
7
7
  @appName
8
8
  @messageEndpointMappings
9
9
  @handlerPathList
10
10
  @contractList
11
11
 
12
- @localQueueName
13
12
  @errorQueueName
14
13
  @forwardSentMessagesTo
15
14
  @maxRetries
@@ -18,8 +17,6 @@ module RServiceBus
18
17
 
19
18
  @mq
20
19
 
21
- @queueTimeout
22
-
23
20
  def initialize()
24
21
  puts "Cannot instantiate config directly."
25
22
  puts "For production, use ConfigFromEnv."
@@ -56,10 +53,8 @@ module RServiceBus
56
53
 
57
54
  def loadHostSection()
58
55
  @appName = self.getValue( "APPNAME", "RServiceBus" )
59
- @localQueueName = self.getValue( "LOCAL_QUEUE_NAME", @appName )
60
56
  @errorQueueName = self.getValue( "ERROR_QUEUE_NAME", "error" )
61
57
  @maxRetries = self.getValue( "MAX_RETRIES", "5" ).to_i
62
- @queueTimeout = self.getValue( "QUEUE_TIMEOUT", "5" ).to_i
63
58
  @statOutputCountdown = self.getValue( "STAT_OUTPUT_COUNTDOWN", "100" ).to_i
64
59
  @subscriptionUri = self.getValue( "SUBSCRIPTION_URI", "file:///tmp/#{appName}_subscriptions.yaml" )
65
60
 
@@ -192,7 +187,7 @@ module RServiceBus
192
187
  end
193
188
 
194
189
  class ConfigFromSetter<Config
195
- attr_writer :appName, :messageEndpointMappings, :handlerPathList, :localQueueName, :errorQueueName, :maxRetries, :forwardReceivedMessagesTo, :beanstalkHost
190
+ attr_writer :appName, :messageEndpointMappings, :handlerPathList, :errorQueueName, :maxRetries, :forwardReceivedMessagesTo, :beanstalkHost
196
191
 
197
192
  def initialize()
198
193
  end
@@ -72,7 +72,7 @@ module RServiceBus
72
72
  #Thin veneer for Configuring the Message Queue
73
73
  #
74
74
  def connectToMq
75
- @mq = ConfigureMQ.new.get( @config.mqHost + "/" + @config.localQueueName, @config.queueTimeout )
75
+ @mq = MQ.get
76
76
 
77
77
  return self
78
78
  end
@@ -153,13 +153,14 @@ module RServiceBus
153
153
  def initialize()
154
154
  @config = ConfigFromEnv.new
155
155
  .loadHostSection()
156
- .configureMq()
157
156
  .loadContracts()
158
157
  .loadHandlerPathList()
159
158
  .loadLibs()
160
159
  .loadWorkingDirList();
161
-
162
- @endpointMapping = EndpointMapping.new.Configure( @config.localQueueName )
160
+
161
+ self.connectToMq()
162
+
163
+ @endpointMapping = EndpointMapping.new.Configure( @mq.localQueueName )
163
164
 
164
165
  self.configureStatistics()
165
166
  .loadContracts()
@@ -170,7 +171,6 @@ module RServiceBus
170
171
  .configureMonitors()
171
172
  .loadHandlers()
172
173
  .configureCronManager()
173
- .connectToMq()
174
174
  .configureSubscriptions()
175
175
  .sendSubscriptions()
176
176
 
@@ -183,8 +183,8 @@ module RServiceBus
183
183
  def run
184
184
  log "Starting the Host"
185
185
 
186
- log "Watching, #{@config.localQueueName}"
187
- $0 = "rservicebus - #{@config.localQueueName}"
186
+ log "Watching, #{@mq.localQueueName}"
187
+ $0 = "rservicebus - #{@mq.localQueueName}"
188
188
  if !@config.forwardReceivedMessagesTo.nil? then
189
189
  log "Forwarding all received messages to: " + @config.forwardReceivedMessagesTo.to_s
190
190
  end
@@ -242,7 +242,7 @@ module RServiceBus
242
242
  puts "*** Class not found for msg, #{e.message}"
243
243
  puts "*** Ensure, #{e.message}, is defined in Contract.rb, most likely as 'Class #{e.message} end"
244
244
 
245
- @msg.addErrorMsg( @config.localQueueName, e.message )
245
+ @msg.addErrorMsg( @mq.localQueueName, e.message )
246
246
  serialized_object = YAML::dump(@msg)
247
247
  self._SendAlreadyWrappedAndSerialised(serialized_object, @config.errorQueueName)
248
248
  @mq.ack
@@ -251,7 +251,7 @@ module RServiceBus
251
251
  puts "*** Handler not found for msg, #{e.message}"
252
252
  puts "*** Ensure a handler named, #{e.message}, is present in the MessageHandler directory."
253
253
 
254
- @msg.addErrorMsg( @config.localQueueName, e.message )
254
+ @msg.addErrorMsg( @mq.localQueueName, e.message )
255
255
  serialized_object = YAML::dump(@msg)
256
256
  self._SendAlreadyWrappedAndSerialised(serialized_object, @config.errorQueueName)
257
257
  @mq.ack
@@ -294,7 +294,7 @@ module RServiceBus
294
294
  errorString = e.message + ". " + e.backtrace.join( ". " )
295
295
  # log errorString
296
296
 
297
- @msg.addErrorMsg( @config.localQueueName, errorString )
297
+ @msg.addErrorMsg( @mq.localQueueName, errorString )
298
298
  serialized_object = YAML::dump(@msg)
299
299
  self._SendAlreadyWrappedAndSerialised(serialized_object, @config.errorQueueName)
300
300
  @mq.ack
@@ -397,7 +397,7 @@ module RServiceBus
397
397
  def _SendNeedsWrapping( msg, queueName )
398
398
  log "Bus._SendNeedsWrapping", true
399
399
 
400
- rMsg = RServiceBus::Message.new( msg, @config.localQueueName )
400
+ rMsg = RServiceBus::Message.new( msg, @mq.localQueueName )
401
401
  if queueName.index( "@" ).nil? then
402
402
  q = queueName
403
403
  log "Sending, #{msg.class.name} to, queueName", true
@@ -439,7 +439,7 @@ module RServiceBus
439
439
  queueName = @endpointMapping.get( msgName )
440
440
  return queueName unless queueName.nil?
441
441
 
442
- return @config.localQueueName if @handlerManager.canMsgBeHandledLocally(msgName)
442
+ return @mq.localQueueName if @handlerManager.canMsgBeHandledLocally(msgName)
443
443
 
444
444
  log "No end point mapping found for: " + msgName
445
445
  log "**** Check environment variable MessageEndpointMappings contains an entry named : " + msgName
@@ -10,34 +10,63 @@ module RServiceBus
10
10
  # - dependency injection.
11
11
  #
12
12
  class MQ
13
+
14
+ attr_reader :localQueueName
15
+
13
16
  @uri
14
17
 
18
+
19
+ def MQ.get
20
+ mqString = RServiceBus.getValue( "RSBMQ", "beanstalk://localhost" );
21
+ uri = URI.parse( mqString )
22
+
23
+ case uri.scheme
24
+ when "beanstalk"
25
+ require "rservicebus/MQ/Beanstalk"
26
+ mq = MQ_Beanstalk.new( uri )
27
+
28
+ when "redis"
29
+ require "rservicebus/MQ/Redis"
30
+ mq = MQ_Redis.new( uri )
31
+
32
+ else
33
+ abort("Scheme, #{uri.scheme}, not recognised when configuring mq, #{string}");
34
+ end
35
+
36
+ return mq
37
+ end
38
+
15
39
  # Resources are attached resources, and can be specified using the URI syntax.
16
40
  #
17
41
  # @param [URI] uri the type and location of queue, eg beanstalk://127.0.0.1/foo
18
42
  # @param [Integer] timeout the amount of time to wait for a msg to arrive
19
- def initialize( uri, timeout )
20
- @timeout = timeout
43
+ def initialize( uri )
44
+
21
45
  if uri.is_a? URI then
22
46
  @uri = uri
23
47
  else
24
48
  puts "uri must be a valid URI"
25
49
  abort()
26
50
  end
27
-
28
- host = uri.host
29
- port = uri.port
30
- queue = uri.path.sub( "/", "" )
31
-
32
- if ( queue == "" )
51
+
52
+ if uri.path == "" || uri.path == "/" then
53
+ @localQueueName = RServiceBus.getValue( "APPNAME", "RServiceBus" )
54
+ else
55
+ @localQueueName = uri.path
56
+ @localQueueName[0] = ""
57
+ end
58
+
59
+ if @localQueueName == "" then
60
+ puts "@localQueueName: #{@localQueueName}"
33
61
  puts "Queue name must be supplied "
34
62
  puts "*** uri, #{uri}, needs to contain a queue name"
35
63
  puts "*** the structure is scheme://host[:port]/queuename"
36
64
  abort()
37
65
  end
38
-
66
+
67
+ @timeout = RServiceBus.getValue( "QUEUE_TIMEOUT", "5" ).to_i
39
68
  self.connect(uri.host, uri.port)
40
- self.subscribe( queue )
69
+ self.subscribe( @localQueueName )
41
70
  end
42
71
 
43
72
  # Connect to the broker
@@ -0,0 +1,57 @@
1
+ module RServiceBus
2
+
3
+ require "rservicebus/MQ"
4
+ require "redis"
5
+
6
+ # Beanstalk client implementation.
7
+ #
8
+ class MQ_Redis<MQ
9
+
10
+ # Connect to the broker
11
+ #
12
+ def connect( host, port )
13
+ port ||= 6379
14
+ string = "#{host}:#{port}"
15
+
16
+ begin
17
+ @redis = Redis.new(:host => host, :port => port)
18
+
19
+ rescue Exception => e
20
+ puts e.message
21
+ puts "Error connecting to Redis for mq"
22
+ puts "Host string, #{string}"
23
+ abort()
24
+ end
25
+ end
26
+
27
+ # Connect to the queue
28
+ #
29
+ def subscribe( queuename )
30
+ @queuename = queuename
31
+ end
32
+
33
+ # Get next msg from queue
34
+ def pop
35
+ if @redis.llen( @queuename ) == 0 then
36
+ sleep @timeout
37
+ raise NoMsgToProcess.new
38
+ end
39
+
40
+ return @redis.lindex @queuename, 0
41
+ end
42
+
43
+ def returnToQueue
44
+ end
45
+
46
+ # "Commit" queue
47
+ def ack
48
+ @redis.lpop @queuename
49
+ end
50
+
51
+ # At least called in the Host rescue block, to ensure all network links are healthy
52
+ def send( queueName, msg )
53
+ @redis.rpush queueName, msg
54
+ end
55
+
56
+ end
57
+ end
@@ -42,7 +42,8 @@ module RServiceBus
42
42
  endpointMapping.Configure
43
43
  queueName = endpointMapping.get( msg.class.name )
44
44
 
45
- agent = RServiceBus::Agent.new.getAgent( URI.parse( "beanstalk://127.0.0.1:11300" ) )
45
+ ENV["RSBMQ"] = "beanstalk://localhost" if ENV["RSBMQ"].nil?
46
+ agent = RServiceBus::Agent.new
46
47
  Audit.new( agent ).audit( msg )
47
48
  agent.sendMsg(msg, queueName, responseQueue)
48
49
 
@@ -60,7 +61,8 @@ module RServiceBus
60
61
  endpointMapping.Configure
61
62
  queueName = endpointMapping.get( msg.class.name )
62
63
 
63
- agent = RServiceBus::Agent.new.getAgent( URI.parse( "beanstalk://127.0.0.1:11300" ) )
64
+ ENV["RSBMQ"] = "beanstalk://localhost" if ENV["RSBMQ"].nil?
65
+ agent = RServiceBus::Agent.new
64
66
  Audit.new( agent ).auditOutgoing( msg )
65
67
  agent.sendMsg(msg, queueName, responseQueue)
66
68
 
@@ -73,7 +75,8 @@ module RServiceBus
73
75
  end
74
76
 
75
77
  def RServiceBus.checkForReply( queueName )
76
- agent = RServiceBus::Agent.new.getAgent( URI.parse( "beanstalk://127.0.0.1:11300" ) )
78
+ ENV["RSBMQ"] = "beanstalk://localhost" if ENV["RSBMQ"].nil?
79
+ agent = RServiceBus::Agent.new
77
80
  msg = agent.checkForReply( queueName )
78
81
  Audit.new( agent ).auditIncoming( msg )
79
82
 
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.1.48
4
+ version: 0.1.49
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2013-10-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: uuidtools
16
- requirement: &70114846310520 !ruby/object:Gem::Requirement
16
+ requirement: &70296757940520 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70114846310520
24
+ version_requirements: *70296757940520
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: json
27
- requirement: &70114846309440 !ruby/object:Gem::Requirement
27
+ requirement: &70296757940060 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70114846309440
35
+ version_requirements: *70296757940060
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: beanstalk-client
38
- requirement: &70114846308800 !ruby/object:Gem::Requirement
38
+ requirement: &70296757939360 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70114846308800
46
+ version_requirements: *70296757939360
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: fluiddb
49
- requirement: &70114846308260 !ruby/object:Gem::Requirement
49
+ requirement: &70296757938880 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *70114846308260
57
+ version_requirements: *70296757938880
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: parse-cron
60
- requirement: &70114846307660 !ruby/object:Gem::Requirement
60
+ requirement: &70296757928940 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: '0'
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *70114846307660
68
+ version_requirements: *70296757928940
69
69
  description: A Ruby interpretation of NServiceBus
70
70
  email: guy@guyirvine.com
71
71
  executables:
@@ -77,7 +77,6 @@ executables:
77
77
  extensions: []
78
78
  extra_rdoc_files: []
79
79
  files:
80
- - lib/rservicebus/Agent/Beanstalk.rb
81
80
  - lib/rservicebus/Agent.rb
82
81
  - lib/rservicebus/AppResource/Dir.rb
83
82
  - lib/rservicebus/AppResource/File.rb
@@ -99,7 +98,6 @@ files:
99
98
  - lib/rservicebus/Config.rb
100
99
  - lib/rservicebus/ConfigureAppResource.rb
101
100
  - lib/rservicebus/ConfigureMonitor.rb
102
- - lib/rservicebus/ConfigureMQ.rb
103
101
  - lib/rservicebus/ConfigureSubscriptionStorage.rb
104
102
  - lib/rservicebus/CronManager.rb
105
103
  - lib/rservicebus/EndpointMapping.rb
@@ -120,6 +118,7 @@ files:
120
118
  - lib/rservicebus/Monitor/XmlDir.rb
121
119
  - lib/rservicebus/Monitor.rb
122
120
  - lib/rservicebus/MQ/Beanstalk.rb
121
+ - lib/rservicebus/MQ/Redis.rb
123
122
  - lib/rservicebus/MQ.rb
124
123
  - lib/rservicebus/Saga.rb
125
124
  - lib/rservicebus/StateManager.rb
@@ -1,57 +0,0 @@
1
- module RServiceBus
2
- require 'beanstalk-client'
3
-
4
- class QueueNotFoundForMsg<StandardError
5
- end
6
-
7
-
8
- #A means for a stand-alone process to interact with the bus, without being a full
9
- #rservicebus application
10
- class Agent_Beanstalk
11
- @beanstalk
12
-
13
- def initialize(url=['localhost:11300'])
14
- @beanstalk = Beanstalk::Pool.new(url)
15
- end
16
-
17
- # Put a msg on the bus
18
- #
19
- # @param [Object] messageObj The msg to be sent
20
- # @param [String] queueName the name of the queue to be send the msg to
21
- # @param [String] returnAddress the name of a queue to send replies to
22
- def sendMsg(messageObj, queueName, returnAddress=nil)
23
- raise QueueNotFoundForMsg.new( messageObj.class.name ) if queueName.nil?
24
-
25
- msg = RServiceBus::Message.new( messageObj, returnAddress )
26
-
27
-
28
- if queueName.index( "@" ).nil? then
29
- q = queueName
30
- else
31
- parts = queueName.split( "@" )
32
- msg.setRemoteQueueName( parts[0] )
33
- msg.setRemoteHostName( parts[1] )
34
- q = 'transport-out'
35
- end
36
-
37
- serialized_object = YAML::dump(msg)
38
-
39
- @beanstalk.use( q )
40
- @beanstalk.put( serialized_object )
41
- end
42
-
43
- # Gives an agent the means to receive a reply
44
- #
45
- # @param [String] queueName the name of the queue to monitor for messages
46
- def checkForReply( queueName )
47
- @beanstalk.watch queueName
48
- job = @beanstalk.reserve
49
- body = job.body
50
- job.delete
51
-
52
- @msg = YAML::load(body)
53
- return @msg.msg
54
- end
55
- end
56
-
57
- end
@@ -1,25 +0,0 @@
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
- else
17
- abort("Scheme, #{uri.scheme}, not recognised when configuring mq, #{string}");
18
- end
19
-
20
- return mq
21
- end
22
-
23
- end
24
-
25
- end