stomp_actors 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ log/*
19
+ tmp/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in stomp_actors.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Emilien Taque
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,85 @@
1
+ # StompActors
2
+
3
+ A suite of Celluloid actors to interact with Stomp protocol, using OnStomp gem.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'stomp_actors'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install stomp_actors
18
+
19
+ ## Usage
20
+
21
+ ### Consumers
22
+
23
+ Inherit from `StompActors::Consumer` then define `#queue` and `#uri`
24
+ values, and `#receive(msg)` method.
25
+
26
+ Define `#subscribe_opts` to customize subscription options.
27
+
28
+ Use `ack(msg)` to acknowledge message.
29
+
30
+ Example:
31
+
32
+ ```ruby
33
+ class MyConsumer < StompActors::Consumer
34
+ def uri
35
+ "stomp://127.0.0.1:61613"
36
+ end
37
+
38
+ def queue
39
+ "/queue/foo"
40
+ end
41
+
42
+ def subscribe_opts
43
+ { ack: 'client' }
44
+ end
45
+
46
+ def receive(msg)
47
+ # do something
48
+ ack(msg)
49
+ end
50
+ end
51
+
52
+ MyConsumer.new # your actor is now subscribed and will process messages.
53
+ ```
54
+
55
+ ### Producers
56
+
57
+ Inherit from `StompActors::Producer` then define `#queue` and `#uri`
58
+ values.
59
+
60
+ Use `emit(msg)` to send message to the defined queue.
61
+
62
+ Example:
63
+
64
+ ```ruby
65
+ class MyProducer < StompActors::Producer
66
+ def uri
67
+ "stomp://127.0.0.1:61613"
68
+ end
69
+
70
+ def queue
71
+ "/queue/foo"
72
+ end
73
+ end
74
+
75
+ p = MyProducer.new
76
+ p.emit("stuff") # message sent by actor
77
+ ```
78
+
79
+ ## Contributing
80
+
81
+ 1. Fork it
82
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
83
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
84
+ 4. Push to the branch (`git push origin my-new-feature`)
85
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ task :default => :spec
4
+
@@ -0,0 +1,51 @@
1
+ module StompActors
2
+ module Client
3
+
4
+ class DisconnectionError < StandardError; end
5
+ class BrokerError < StandardError; end
6
+
7
+ def self.included(base)
8
+ base.extend ClassMethods
9
+
10
+ base.class_eval do
11
+ attr_accessor :client
12
+ end
13
+ end
14
+
15
+ module ClassMethods
16
+ end
17
+
18
+ def connect
19
+ @client = OnStomp.open(uri)
20
+
21
+ me = current_actor
22
+
23
+ @client.on_connection_closed do |_, _, msg|
24
+ me.async.connection_closed(msg)
25
+ end
26
+
27
+ @client.on_error do |e, *_|
28
+ me.async.broker_error(e)
29
+ end
30
+ end
31
+
32
+ def connection_closed(msg)
33
+ unless @disconnecting
34
+ error "Connection closed: #{msg}" if respond_to?(:error)
35
+ raise DisconnectionError.new(msg)
36
+ end
37
+ end
38
+
39
+ def broker_error(e)
40
+ error("Broker error: #{e.inspect}") if respond_to?(:error)
41
+ raise BrokerError.new(e[:message])
42
+ end
43
+
44
+ def disconnect
45
+ @disconnecting = true
46
+ @client.disconnect if @client && @client.connected?
47
+ end
48
+
49
+ end
50
+ end
51
+
@@ -0,0 +1,41 @@
1
+ module StompActors
2
+ class Consumer
3
+ include Celluloid
4
+ include Celluloid::Logger
5
+
6
+ include Client
7
+
8
+ attr_accessor :subscription_id
9
+
10
+ def initialize
11
+ connect
12
+ subscribe
13
+ end
14
+
15
+ def subscribe
16
+ me = current_actor
17
+
18
+ @subscription_id = client.subscribe(queue, subscribe_opts) do |msg|
19
+ me.receive(msg)
20
+ end
21
+ end
22
+
23
+ def subscribe_opts
24
+ {}
25
+ end
26
+
27
+ def ack(msg)
28
+ client.ack(msg)
29
+ end
30
+
31
+ def finalize
32
+ unsubscribe
33
+ disconnect
34
+ end
35
+
36
+ def unsubscribe
37
+ client.unsubscribe(@subscription_id) if @subscription_id
38
+ end
39
+ end
40
+ end
41
+
@@ -0,0 +1,22 @@
1
+ module StompActors
2
+ class Producer
3
+ include Celluloid
4
+ include Celluloid::Logger
5
+
6
+ include Client
7
+
8
+ def initialize
9
+ connect
10
+ end
11
+
12
+ def emit(msg)
13
+ client.send(queue, msg)
14
+ end
15
+
16
+ def finalize
17
+ disconnect
18
+ end
19
+ end
20
+ end
21
+
22
+
@@ -0,0 +1,3 @@
1
+ module StompActors
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,7 @@
1
+ require "stomp_actors/version"
2
+ require "stomp_actors/client"
3
+ require "stomp_actors/producer"
4
+ require "stomp_actors/consumer"
5
+
6
+ module StompActors
7
+ end
@@ -0,0 +1,19 @@
1
+ require 'bundler'
2
+
3
+ require 'celluloid'
4
+ require 'stomp_droid'
5
+ require 'onstomp'
6
+ require 'pry'
7
+
8
+ require 'stomp_actors'
9
+
10
+ logfile = File.open(File.expand_path("../../log/test.log", __FILE__), 'a')
11
+ Celluloid.logger = Logger.new(logfile)
12
+
13
+ RSpec.configure do |config|
14
+ config.before :each do
15
+ end
16
+ end
17
+
18
+
19
+
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe StompActors::Client do
4
+ let(:host) { '127.0.0.1' }
5
+ let(:port) { 61623 }
6
+
7
+ before do
8
+ class MyActor
9
+ include Celluloid
10
+ include StompActors::Client
11
+
12
+ def uri
13
+ "stomp://127.0.0.1:61623"
14
+ end
15
+ end
16
+
17
+ @server_thread = StompDroid::Server.start(host, port)
18
+ sleep 0.1 # let start
19
+ end
20
+
21
+ after do
22
+ @server_thread.terminate
23
+ end
24
+
25
+ it "should connect and disconnect" do
26
+ actor = MyActor.new
27
+
28
+ actor.connect
29
+ actor.client.connected?.should be_true
30
+
31
+ actor.disconnect
32
+ actor.client.connected?.should be_false
33
+ end
34
+
35
+ pending "should handle errors"
36
+ end
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+
3
+ describe StompActors::Consumer do
4
+ let(:host) { '127.0.0.1' }
5
+ let(:port) { 61623 }
6
+ let(:queue) { "/queue/foo" }
7
+ let(:msg) { "something" }
8
+
9
+ before do
10
+ class MyConsumer < StompActors::Consumer
11
+ attr_reader :queue
12
+
13
+ def initialize(host, port, queue)
14
+ @host = host
15
+ @port = port
16
+ @queue = queue
17
+ super()
18
+ end
19
+
20
+ def uri
21
+ "stomp://#{@host}:#{@port}"
22
+ end
23
+
24
+ def receive(msg)
25
+ do_something
26
+ ack(msg)
27
+ end
28
+
29
+ def do_something; end
30
+ end
31
+
32
+ @server_thread = StompDroid::Server.start(host, port, queue_name: queue, message: msg)
33
+ sleep 0.1 # let start
34
+ end
35
+
36
+ after do
37
+ @server_thread.terminate
38
+ end
39
+
40
+ it "should subscribe, handle and ack message on connect" do
41
+ MyConsumer.any_instance.should_receive(:do_something)
42
+ consumer = MyConsumer.new(host, port, queue)
43
+ sleep 0.1
44
+ end
45
+
46
+ it "should unsubscribe on finalize" do
47
+ consumer = MyConsumer.new(host, port, queue)
48
+ sleep 0.1
49
+ consumer.finalize
50
+ end
51
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe StompActors::Producer do
4
+ let(:host) { '127.0.0.1' }
5
+ let(:port) { 61623 }
6
+ let(:queue) { "/queue/foo" }
7
+ let(:message_dir) { 'tmp/messages' }
8
+
9
+ before do
10
+ class MyProducer < StompActors::Producer
11
+ def uri
12
+ "stomp://127.0.0.1:61623"
13
+ end
14
+ def queue
15
+ '/queue/foo'
16
+ end
17
+ end
18
+
19
+ @server_thread = StompDroid::Server.start(host, port, queue_name: queue, sent_message_dir: message_dir)
20
+ sleep 0.1 # let start
21
+ end
22
+
23
+ after do
24
+ @server_thread.terminate
25
+ FileUtils.rm(Dir.glob("#{message_dir}/*.msg"))
26
+ end
27
+
28
+ it "should send message" do
29
+ producer = MyProducer.new
30
+ expect {
31
+ producer.emit("message")
32
+ sleep 0.5
33
+ }.to change { Dir["#{message_dir}/*.msg"].to_a.length }.by(1)
34
+ end
35
+ end
36
+
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'stomp_actors/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "stomp_actors"
8
+ spec.version = StompActors::VERSION
9
+ spec.authors = ["Emilien Taque"]
10
+ spec.email = ["e.taque@alphalink.fr"]
11
+ spec.description = %q{Celluloid actors for Stomp}
12
+ spec.summary = %q{Build Stomp consumers and publishers with Celluloid actors framework and OnStomp lib.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency 'rspec'
24
+ spec.add_development_dependency 'stomp_droid'
25
+ spec.add_development_dependency 'pry'
26
+
27
+ spec.add_runtime_dependency 'celluloid', '~> 0.12'
28
+ spec.add_runtime_dependency 'onstomp', '~> 1.0'
29
+ end
metadata ADDED
@@ -0,0 +1,178 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stomp_actors
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Emilien Taque
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: stomp_droid
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: pry
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: celluloid
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: '0.12'
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: '0.12'
110
+ - !ruby/object:Gem::Dependency
111
+ name: onstomp
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: '1.0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ~>
124
+ - !ruby/object:Gem::Version
125
+ version: '1.0'
126
+ description: Celluloid actors for Stomp
127
+ email:
128
+ - e.taque@alphalink.fr
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - .gitignore
134
+ - Gemfile
135
+ - LICENSE.txt
136
+ - README.md
137
+ - Rakefile
138
+ - lib/stomp_actors.rb
139
+ - lib/stomp_actors/client.rb
140
+ - lib/stomp_actors/consumer.rb
141
+ - lib/stomp_actors/producer.rb
142
+ - lib/stomp_actors/version.rb
143
+ - spec/spec_helper.rb
144
+ - spec/stomp_actors/client_spec.rb
145
+ - spec/stomp_actors/consumer_spec.rb
146
+ - spec/stomp_actors/producer_spec.rb
147
+ - stomp_actors.gemspec
148
+ homepage: ''
149
+ licenses:
150
+ - MIT
151
+ post_install_message:
152
+ rdoc_options: []
153
+ require_paths:
154
+ - lib
155
+ required_ruby_version: !ruby/object:Gem::Requirement
156
+ none: false
157
+ requirements:
158
+ - - ! '>='
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ required_rubygems_version: !ruby/object:Gem::Requirement
162
+ none: false
163
+ requirements:
164
+ - - ! '>='
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ requirements: []
168
+ rubyforge_project:
169
+ rubygems_version: 1.8.25
170
+ signing_key:
171
+ specification_version: 3
172
+ summary: Build Stomp consumers and publishers with Celluloid actors framework and
173
+ OnStomp lib.
174
+ test_files:
175
+ - spec/spec_helper.rb
176
+ - spec/stomp_actors/client_spec.rb
177
+ - spec/stomp_actors/consumer_spec.rb
178
+ - spec/stomp_actors/producer_spec.rb