lolitra 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Binary file
Binary file
@@ -1,4 +1,7 @@
1
1
  require 'singleton'
2
+ require 'amqp'
3
+ require 'amqp/utilities/event_loop_helper'
4
+ require 'json'
2
5
 
3
6
  module Lolitra
4
7
  module MessageHandler
@@ -175,7 +178,9 @@ module Lolitra
175
178
  end
176
179
 
177
180
  def marshall
178
- JSON.generate(self)
181
+ hash = {}
182
+ self.instance_variables.each {|var| hash[var.to_s.delete("@")] = self.instance_variable_get(var) }
183
+ JSON.generate(hash)
179
184
  end
180
185
  end
181
186
 
@@ -207,25 +212,34 @@ module Lolitra
207
212
 
208
213
  class AmqpBus
209
214
  attr_accessor :queue_prefix
215
+ attr_accessor :connection
210
216
  attr_accessor :exchange
211
217
 
212
218
  def initialize(hash = {})
213
- self.queue_prefix = hash[:queue_prefix]
214
- self.exchange = hash[:exchange]
219
+ params = hash.reject { |key, value| !value }
220
+ raise "no :exchange specified" unless hash[:exchange]
221
+
222
+ self.queue_prefix = hash[:queue_prefix]||""
223
+
224
+ AMQP::Utilities::EventLoopHelper.run do
225
+ self.connection = AMQP.connect(params)
226
+ self.exchange = AMQP::Channel.new(self.connection).topic(params[:exchange], :durable => true)
227
+ end
215
228
  end
216
229
 
217
230
  def subscribe(message_class, handler_class)
218
231
  EM.next_tick do
219
- AmqpNotifier.new(exchange, queue_prefix + MessageHandler::Helper.underscore(handler_class.name).subscribe(message_class.message_key)) do |info, payload|
220
- #all message with the same handler goes here to try to maintain the order in the same queue (not ensured)
232
+ channel = AMQP::Channel.new(self.connection)
233
+ channel.prefetch(1).queue(queue_prefix + MessageHandler::Helpers.underscore(handler_class.name), :dureable => true).bind(self.exchange, :routing_key => message_class.message_key).subscribe do |info, payload|
221
234
  message_class_tmp = handler_class.message_class_by_key[info.routing_key]
222
235
  handler_class.handle(message_class_tmp.unmarshall(payload))
223
- end
236
+ end
224
237
  end
225
238
  end
226
239
 
227
240
  def publish(message)
228
- AmqpNotifier.new(exchange).publish(message.class.message_key, message.marshall)
241
+ self.exchange.publish(message.marshall, :routing_key => message.class.message_key, :timestamp => Time.now.to_i)
229
242
  end
243
+
230
244
  end
231
245
  end
@@ -1,3 +1,3 @@
1
1
  module Lolitra
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lolitra.gemspec CHANGED
@@ -16,4 +16,6 @@ Gem::Specification.new do |gem|
16
16
  gem.version = Lolitra::VERSION
17
17
 
18
18
  gem.add_development_dependency("rspec")
19
+ gem.add_dependency("amqp")
20
+ gem.add_dependency("json")
19
21
  end
Binary file
data/spec/lolitra_spec.rb CHANGED
@@ -66,8 +66,13 @@ class TestAmqpMessageHandler
66
66
  message_handler TestMessage1
67
67
 
68
68
  stateful false
69
+
70
+ def test_message(message)
71
+ EM.stop { exit }
72
+ end
69
73
  end
70
74
 
75
+
71
76
  describe Lolitra::MessageHandler,'#create_handler' do
72
77
  it "returns old instance of the handler for the same message id" do
73
78
  handler = TestMessageHandler.new
@@ -171,6 +176,26 @@ describe Lolitra::AmqpMessageHandler, '#message_class_by_key' do
171
176
  end
172
177
  end
173
178
 
179
+ describe Lolitra::AmqpBus do
180
+ it "should recive message when publish a message" do
181
+ Lolitra::MessageHandlerManager.bus = Lolitra::AmqpBus.new(:host => "127.0.0.1", :exchange => "test_exchange")
182
+ Lolitra::MessageHandlerManager.register(TestAmqpMessageHandler)
183
+
184
+ TestAmqpMessageHandler.should_receive(:handle) do |message|
185
+ message.should be_an_instance_of TestMessage
186
+ EM.stop { exit }
187
+ end
188
+
189
+ Lolitra::MessageHandlerManager.publish(TestMessage.new)
174
190
 
175
- #TODO
176
- #add test to Lolitra::AmqpMessageHandler and AmqpBus with evented-spec
191
+ EM.next_tick{ EM.add_timer(60) { EM.stop { exit } }}
192
+ AMQP::Utilities::EventLoopHelper.eventmachine_thread.join
193
+ end
194
+ end
195
+
196
+ describe Lolitra::AmqpBus do
197
+ it "should return a unable to connect when specify a wrong amqp host" do
198
+ expect { Lolitra::AmqpBus.new(:host => "192.168.123.123") }.to raise_error(UnableToConnect) #TODO
199
+ pending
200
+ end
201
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 2
9
- version: 0.0.2
8
+ - 3
9
+ version: 0.0.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Hugo Freire
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2012-09-19 00:00:00 +02:00
17
+ date: 2012-05-26 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -30,6 +30,32 @@ dependencies:
30
30
  version: "0"
31
31
  type: :development
32
32
  version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: amqp
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ type: :runtime
45
+ version_requirements: *id002
46
+ - !ruby/object:Gem::Dependency
47
+ name: json
48
+ prerelease: false
49
+ requirement: &id003 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ type: :runtime
58
+ version_requirements: *id003
33
59
  description: Lolitra, build Sagas, a kind of Long Live Transaction (LLT), in less lines
34
60
  email:
35
61
  - hfreire@abajar.com
@@ -47,9 +73,12 @@ files:
47
73
  - README.md
48
74
  - Rakefile
49
75
  - lib/lolitra.rb
76
+ - lib/lolitra/.handler_base.rb.swp
77
+ - lib/lolitra/.version.rb.swp
50
78
  - lib/lolitra/handler_base.rb
51
79
  - lib/lolitra/version.rb
52
80
  - lolitra.gemspec
81
+ - spec/.lolitra_spec.rb.swp
53
82
  - spec/lolitra_spec.rb
54
83
  - spec/spec_helper.rb
55
84
  has_rdoc: true
@@ -85,5 +114,6 @@ signing_key:
85
114
  specification_version: 3
86
115
  summary: Lolitra, build Sagas, a kind of Long Live Transaction (LLT), in less lines
87
116
  test_files:
117
+ - spec/.lolitra_spec.rb.swp
88
118
  - spec/lolitra_spec.rb
89
119
  - spec/spec_helper.rb