energizer_bunny 0.3.0-java
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.
- data/LICENSE.txt +20 -0
- data/README.md +27 -0
- data/lib/energizer_bunny.rb +2 -0
- data/lib/energizer_bunny/connection.rb +93 -0
- data/lib/energizer_bunny/subscription.rb +52 -0
- data/lib/energizer_bunny/version.rb +3 -0
- metadata +101 -0
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Joel Friedman
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
Energizer Bunny
|
2
|
+
===============
|
3
|
+
|
4
|
+
A thin wrapper around [hot_bunnies][], the JRuby wrapper around the
|
5
|
+
[RabbitMQ Java Client][api-guide].
|
6
|
+
|
7
|
+
Usage
|
8
|
+
-----
|
9
|
+
|
10
|
+
TODO
|
11
|
+
|
12
|
+
Contributing
|
13
|
+
------------
|
14
|
+
|
15
|
+
1. Fork it
|
16
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
17
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
18
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
19
|
+
5. Create new Pull Request
|
20
|
+
|
21
|
+
Copyright
|
22
|
+
---------
|
23
|
+
|
24
|
+
Copyright (c) 2012 Joel Friedman. See LICENSE.txt for further details.
|
25
|
+
|
26
|
+
[hot_bunnies]: <https://github.com/ruby-amqp/hot_bunnies>
|
27
|
+
[api-guide]: <http://www.rabbitmq.com/api-guide.html>
|
@@ -0,0 +1,93 @@
|
|
1
|
+
require_relative 'subscription'
|
2
|
+
|
3
|
+
module EnergizerBunny
|
4
|
+
class Connection
|
5
|
+
class ConfigurationError < StandardError ; end
|
6
|
+
|
7
|
+
def initialize config, logger = Rails.logger
|
8
|
+
@config = config
|
9
|
+
@logger = logger
|
10
|
+
return unless enabled?
|
11
|
+
at_exit { close }
|
12
|
+
@exchanges = Hash.new { |exchanges, topic_key| exchanges[topic_key] = create_exchange topic_key }
|
13
|
+
@queues = Hash.new { |queues, topic_key| queues[topic_key] = create_queue topic_key }
|
14
|
+
@subscriptions = []
|
15
|
+
create_connection
|
16
|
+
end
|
17
|
+
|
18
|
+
def subscribe topic_key, subscription_opts = {}, &block
|
19
|
+
return unless enabled?
|
20
|
+
@subscriptions << Subscription.new(@queues[topic_key], subscription_opts, @logger, block)
|
21
|
+
end
|
22
|
+
|
23
|
+
def publish topic_key, message, opts = {}
|
24
|
+
add_message_id_to_header! opts
|
25
|
+
@exchanges[topic_key].publish message, opts if enabled?
|
26
|
+
end
|
27
|
+
|
28
|
+
def connected?
|
29
|
+
@connection && @connection.open?
|
30
|
+
end
|
31
|
+
|
32
|
+
def enabled?
|
33
|
+
@config[:enabled]
|
34
|
+
end
|
35
|
+
|
36
|
+
def connection_url
|
37
|
+
@config[:broker][:url]
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
def create_exchange topic_key
|
42
|
+
exchange_hash = @config[:exchanges][topic_key]
|
43
|
+
channel.exchange exchange_hash[:name], exchange_hash[:opts]
|
44
|
+
end
|
45
|
+
|
46
|
+
def create_connection
|
47
|
+
url = connection_url
|
48
|
+
raise ConfigurationError.new('Please set a broker url for RabbitMQ') if enabled? && url.nil?
|
49
|
+
@logger.info "Connecting bunny to: #{url}"
|
50
|
+
@connection = HotBunnies.connect :uri => url
|
51
|
+
end
|
52
|
+
|
53
|
+
#TODO: is having one channel bad, should this be one per exchange?
|
54
|
+
def channel
|
55
|
+
@channel ||= @connection.create_channel.tap { |c| c.prefetch = (@config.delete(:prefetch_count) || 1) }
|
56
|
+
end
|
57
|
+
|
58
|
+
def create_queue topic_key
|
59
|
+
queue_hash = @config[:queues][topic_key]
|
60
|
+
raise ConfigurationError.new("Cannot find queue configuration for queue: #{topic_key}") if queue_hash.nil?
|
61
|
+
queue_opts = queue_hash[:opts]
|
62
|
+
queue_opts[:durable] = true unless queue_opts.key? :durable
|
63
|
+
channel.queue(queue_hash[:name], queue_opts).tap do |queue|
|
64
|
+
queue_bind_opts = queue_hash[:bind_opts] || {}
|
65
|
+
Array(queue_hash[:bindings]).each do |binding|
|
66
|
+
binding_opts = queue_bind_opts.merge(binding[:binding_opts] || {})
|
67
|
+
queue.bind @exchanges[binding[:exchange_topic_key]], binding_opts
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def close
|
73
|
+
@logger.info 'Closing rabbit connection'
|
74
|
+
@subscriptions.each &:close
|
75
|
+
return unless connected?
|
76
|
+
if @channel
|
77
|
+
@channel.close
|
78
|
+
@channel = nil
|
79
|
+
end
|
80
|
+
@connection.close
|
81
|
+
@connection = nil
|
82
|
+
end
|
83
|
+
|
84
|
+
def add_message_id_to_header!(opts)
|
85
|
+
opts[:headers] ||= {}
|
86
|
+
opts[:headers]['message_id'] ||= guid_generator.generate
|
87
|
+
end
|
88
|
+
|
89
|
+
def guid_generator
|
90
|
+
@guid_generator ||= UUID.new
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module EnergizerBunny
|
2
|
+
class Subscription
|
3
|
+
import java.util.concurrent.Executors
|
4
|
+
|
5
|
+
def initialize queue, subscription_opts, logger, handle_message
|
6
|
+
@queue, @subscription_opts, @logger, @handle_message = queue, subscription_opts.dup, logger, handle_message
|
7
|
+
@reraise_errors = @subscription_opts.delete(:reraise_errors)
|
8
|
+
@subscription_opts[:executor] = executor unless @subscription_opts[:executor]
|
9
|
+
listen!
|
10
|
+
end
|
11
|
+
|
12
|
+
def close
|
13
|
+
close_executor
|
14
|
+
return if @subscription.nil?
|
15
|
+
@subscription.shutdown!
|
16
|
+
@subscription = nil
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
def listen!
|
21
|
+
@subscription = @queue.subscribe(@subscription_opts) do |headers, msg|
|
22
|
+
begin
|
23
|
+
@handle_message.call headers.properties.headers, msg
|
24
|
+
headers.ack
|
25
|
+
rescue => e
|
26
|
+
@logger.error "!!!Error handling message: #{e.message}"
|
27
|
+
headers.reject
|
28
|
+
raise if @reraise_errors
|
29
|
+
end
|
30
|
+
end
|
31
|
+
self
|
32
|
+
end
|
33
|
+
|
34
|
+
def log_error e
|
35
|
+
@logger.error "An error has occured on another thread: #{e}#{$/}#{e.backtrace}"
|
36
|
+
end
|
37
|
+
|
38
|
+
def thread_pool_size
|
39
|
+
@thread_pool_size ||= (@subscription_opts.delete(:thread_pool_size) || 1)
|
40
|
+
end
|
41
|
+
|
42
|
+
def executor
|
43
|
+
@executor ||= Executors.new_fixed_thread_pool(thread_pool_size)
|
44
|
+
end
|
45
|
+
|
46
|
+
def close_executor
|
47
|
+
return unless @executor
|
48
|
+
@executor.shutdown_now
|
49
|
+
@executor = nil
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: energizer_bunny
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.0
|
5
|
+
prerelease:
|
6
|
+
platform: java
|
7
|
+
authors:
|
8
|
+
- Joel Friedman
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-05-14 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: hot_bunnies
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.4'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.4'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: uuid
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '2.0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '2.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: '2.11'
|
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: '2.11'
|
62
|
+
description: Extra hot_bunnies.
|
63
|
+
email: asher.friedman@gmail.com
|
64
|
+
executables: []
|
65
|
+
extensions: []
|
66
|
+
extra_rdoc_files:
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
files:
|
70
|
+
- lib/energizer_bunny/connection.rb
|
71
|
+
- lib/energizer_bunny/subscription.rb
|
72
|
+
- lib/energizer_bunny/version.rb
|
73
|
+
- lib/energizer_bunny.rb
|
74
|
+
- LICENSE.txt
|
75
|
+
- README.md
|
76
|
+
homepage: http://github.com/trunkclub/energizer_bunny
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
86
|
+
- - ! '>='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ! '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 1.8.24
|
98
|
+
signing_key:
|
99
|
+
specification_version: 3
|
100
|
+
summary: Declaratively configure hot_bunnies.
|
101
|
+
test_files: []
|