jack_rabbit 0.0.4-java

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ pkg
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Sebastian Ohm
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,5 @@
1
+ # JackRabbit
2
+
3
+ Convenience wrapper around AMQP client libraries.
4
+
5
+ This work in progress.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/jack_rabbit/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.name = 'jack_rabbit'
6
+ gem.version = JackRabbit::VERSION
7
+ gem.platform = 'java'
8
+ gem.summary = 'Convenience wrapper around AMQP client libraries'
9
+ gem.authors = [ 'Sebastian Ohm' ]
10
+ gem.email = [ 'ohm.sebastian@gmail.com' ]
11
+ gem.require_paths = [ 'lib' ]
12
+ gem.files = `git ls-files`.split($\)
13
+ gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
14
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
15
+
16
+ gem.add_dependency('hot_bunnies', '~> 1.3.0')
17
+ end
@@ -0,0 +1,77 @@
1
+ require 'hot_bunnies'
2
+ require 'jack_rabbit/message_header'
3
+
4
+ module JackRabbit
5
+ class Consumer
6
+ EXCHANGE_OPTIONS = { durable: true, type: :direct }
7
+ QUEUE_OPTIONS = { durable: true }
8
+ SUBSCRIBE_OPTIONS = { blocking: false }
9
+
10
+ def initialize
11
+ @connections = []
12
+ @channels = []
13
+ @subscriptions = []
14
+ end
15
+
16
+ def connect(uris, options = {})
17
+ uris.inject(@connections) do |memo, uri|
18
+ memo.push(open_connection(uri, options))
19
+ end
20
+ end
21
+
22
+ def subscribe(exchange, key, queue, options = {}, &block)
23
+ channels = open_channels(options[:prefetch])
24
+ bind_queues(channels, exchange, key, queue, options) do |meta, message|
25
+ block.call(MessageHeader.new(meta), message)
26
+ end
27
+ end
28
+
29
+ def disconnect
30
+ @subscriptions.each(&:shutdown!)
31
+ (@channels + @connections).each(&:close)
32
+ end
33
+
34
+ private
35
+
36
+ def open_connection(uri, options)
37
+ HotBunnies.connect(options.merge(
38
+ host: uri.host,
39
+ pass: uri.password,
40
+ port: uri.port,
41
+ user: uri.user,
42
+ vhost: uri.path
43
+ ))
44
+ end
45
+
46
+ def open_channels(prefetch)
47
+ @connections.inject(@channels) do |memo, connection|
48
+ channel =
49
+ connection.create_channel.tap do |channel|
50
+ channel.prefetch = prefetch if prefetch
51
+ end
52
+ memo.push(channel)
53
+ end
54
+ end
55
+
56
+ def bind_queues(channels, exchange_name, key, queue_name, options, &block)
57
+ channels.inject(@subscriptions) do |memo, channel|
58
+ exchange = declare_exchange(channel, exchange_name, options)
59
+ queue =
60
+ bind_queue(channel, exchange, queue_name, key, options).tap do |q|
61
+ q.subscribe(SUBSCRIBE_OPTIONS.merge(options), &block)
62
+ end
63
+ memo.push(queue)
64
+ end
65
+ end
66
+
67
+ def bind_queue(channel, exchange, name, key, options)
68
+ channel
69
+ .queue(name, QUEUE_OPTIONS.merge(Hash(options[:queue])))
70
+ .tap { |q| q.bind(exchange, { routing_key: key }) }
71
+ end
72
+
73
+ def declare_exchange(channel, name, options)
74
+ channel.exchange(name, EXCHANGE_OPTIONS.merge(Hash(options[:exchange])))
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,19 @@
1
+ module JackRabbit
2
+ class MessageHeader
3
+ attr_reader :metadata
4
+
5
+ def initialize(metadata)
6
+ @metadata = metadata
7
+ end
8
+
9
+ def content_type
10
+ @metadata.properties.content_type
11
+ end
12
+
13
+ private
14
+
15
+ def method_missing(method, *args, &block)
16
+ metadata.send(method, *args, &block)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,41 @@
1
+ require 'hot_bunnies'
2
+
3
+ module JackRabbit
4
+ class Producer
5
+ EXCHANGE_OPTIONS = { auto_delete: false, durable: true }
6
+
7
+ def connect(uri, options = {})
8
+ @channel = (@connection = open_connection(uri, options)).create_channel
9
+ end
10
+
11
+ def disconnect
12
+ [ @channel, @connection ].each(&:close)
13
+ end
14
+
15
+ def publish(exchange_name, exchange_type, routing_key, message, headers = {})
16
+ with_exchange(@channel, exchange_name, exchange_type) do |exchange|
17
+ exchange.publish(message, headers.merge(routing_key: routing_key))
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def open_connection(uri, options = {})
24
+ HotBunnies.connect(options.merge(
25
+ host: uri.host,
26
+ pass: uri.password,
27
+ port: uri.port,
28
+ user: uri.user,
29
+ vhost: uri.path
30
+ ))
31
+ end
32
+
33
+ def with_exchange(channel, exchange_name, exchange_type, &block)
34
+ block.call(
35
+ channel.exchange(exchange_name, EXCHANGE_OPTIONS.merge(
36
+ type: exchange_type
37
+ ))
38
+ )
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,3 @@
1
+ module JackRabbit
2
+ VERSION = '0.0.4'
3
+ end
@@ -0,0 +1,3 @@
1
+ require 'jack_rabbit/version'
2
+ require 'jack_rabbit/consumer'
3
+ require 'jack_rabbit/producer'
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jack_rabbit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ prerelease:
6
+ platform: java
7
+ authors:
8
+ - Sebastian Ohm
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-11 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.3.0
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.3.0
30
+ description:
31
+ email:
32
+ - ohm.sebastian@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - LICENSE
40
+ - README.md
41
+ - Rakefile
42
+ - jack_rabbit.gemspec
43
+ - lib/jack_rabbit.rb
44
+ - lib/jack_rabbit/consumer.rb
45
+ - lib/jack_rabbit/message_header.rb
46
+ - lib/jack_rabbit/producer.rb
47
+ - lib/jack_rabbit/version.rb
48
+ homepage:
49
+ licenses: []
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 1.8.23
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: Convenience wrapper around AMQP client libraries
72
+ test_files: []