messaging-adapter 1.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5c2dec583c312d39a8b1576b630713dc3c437a23
4
+ data.tar.gz: a0d6c9eff0c8121b7bc17abd427c4096b1941a13
5
+ SHA512:
6
+ metadata.gz: 5834124a249b35ff6c4445441104f4520d39728c4f5b50c15c0acf73fda21cf50c6d333965a9ce8d9f425276e703cee3e8cbc75a59996da66cfdcb3f1f4085ba
7
+ data.tar.gz: 0b6fa1c849cb4d4d15c5de6afff10a580d1a306aa2ab6be479b25780b5d33a4afd08b04af8f4f08c7734d443615b6f82d26f6fd8ec598ac34b1b6d9c859628b0
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MessageBrokerAdapter
4
+ # Adapter implements Apache Kafka
5
+ class Kafka
6
+ def self.publish(topic, payload)
7
+ puts "Not implemented yet!! publish{#{topic} [#{payload}]}"
8
+ end
9
+
10
+ def self.subscribe(topic)
11
+ puts "Not implemented yet!! subscribe{#{topic}}"
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,99 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bunny'
4
+ require 'json'
5
+
6
+ module MessageBrokerAdapter
7
+ # Adapter implements RabbitMQ Exchange
8
+ class RabbitMQ
9
+ @pub_conn_locker = Mutex.new
10
+ @pub_channel_locker = Mutex.new
11
+ @sub_conn_locker = Mutex.new
12
+ @sub_channel_locker = Mutex.new
13
+ @pub_connection = nil
14
+ @sub_connection = nil
15
+
16
+ class << self
17
+ attr_reader :pub_conn_locker
18
+ attr_reader :pub_channel_locker
19
+ attr_reader :sub_conn_locker
20
+ attr_reader :sub_channel_locker
21
+ end
22
+
23
+ def self.publish(topic, payload)
24
+ exchange = publisher_channel.fanout(topic)
25
+ exchange.publish(payload.to_json)
26
+
27
+ puts '- RMQ: Message published successfully on topic ' \
28
+ "#{topic} [#{payload}]"
29
+ end
30
+
31
+ def self.subscribe(queue)
32
+ q = subscriber_channel.queue(queue, durable: true)
33
+ q.subscribe(block: blocking_subscriber?) do |_delivery_info, _properties, payload|
34
+ yield(payload)
35
+ end
36
+
37
+ puts "- RMQ: Subscribed successfully to the topic #{queue}"
38
+ end
39
+
40
+ def self.connection_configs
41
+ {
42
+ host: ENV['MessageBroker_Host'] || 'localhost',
43
+ port: ENV['MessageBroker_Port'] || '5672',
44
+ username: ENV['MessageBroker_User'] || 'guest',
45
+ password: ENV['MessageBroker_Pass'] || 'guest'
46
+ }
47
+ end
48
+
49
+ def self.blocking_subscriber?
50
+ (ENV['MessageBroker_RabbitMQ_Block'] || 'true') == 'true'
51
+ end
52
+
53
+ def self.recover_connection(conn, locker)
54
+ locker.synchronize { conn.start } unless conn.open?
55
+ end
56
+
57
+ def self.publisher_connection
58
+ if @pub_connection.nil?
59
+ pub_conn_locker.synchronize do
60
+ @pub_connection ||= Bunny.new(connection_configs).tap(&:start)
61
+ end
62
+ else
63
+ recover_connection(@pub_connection, pub_conn_locker)
64
+ @pub_connection
65
+ end
66
+ end
67
+
68
+ def self.publisher_channel
69
+ if Thread.current[:rmq_pub_channel].nil?
70
+ pub_channel_locker.synchronize do
71
+ Thread.current[:rmq_pub_channel] ||= publisher_connection.create_channel
72
+ end
73
+ else
74
+ Thread.current[:rmq_pub_channel]
75
+ end
76
+ end
77
+
78
+ def self.subscriber_connection
79
+ if @sub_connection.nil?
80
+ sub_conn_locker.synchronize do
81
+ @sub_connection ||= Bunny.new(connection_configs).tap(&:start)
82
+ end
83
+ else
84
+ recover_connection(@sub_connection, sub_conn_locker)
85
+ @sub_connection
86
+ end
87
+ end
88
+
89
+ def self.subscriber_channel
90
+ if Thread.current[:rmq_sub_channel].nil?
91
+ sub_channel_locker.synchronize do
92
+ Thread.current[:rmq_sub_channel] ||= subscriber_connection.create_channel
93
+ end
94
+ else
95
+ Thread.current[:rmq_sub_channel]
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ # require the adapters
4
+ Dir[File.dirname(__FILE__) + '/messaging_adapter/adapters/*.rb'].each { |file| require file }
5
+
6
+ module MessagingAdapter
7
+ # MessageBroker class to create broker objects specifying the adapter type
8
+ class MessageBroker
9
+ include MessageBrokerAdapter
10
+
11
+ def initialize(adapter = :RabbitMQ)
12
+ @adapter = MessageBrokerAdapter.const_get(adapter.to_s)
13
+ end
14
+
15
+ def publish(topic, payload)
16
+ @adapter.publish(topic, payload)
17
+ end
18
+
19
+ def subscribe(topic)
20
+ @adapter.subscribe(topic) { |payload| yield(payload) }
21
+ end
22
+
23
+ def self.available_adapters
24
+ MessageBrokerAdapter.constants.map(&:to_sym)
25
+ end
26
+ end
27
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: messaging-adapter
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.6
5
+ platform: ruby
6
+ authors:
7
+ - Ayyoub Jadoo @ TAM
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-10-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bunny
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.12'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bunny
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.12'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.12'
55
+ - !ruby/object:Gem::Dependency
56
+ name: json
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.1'
69
+ description: Makes it easy to pub/sup with many message brokers.
70
+ email: ayyoubjadoo@gmail.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - lib/messaging_adapter.rb
76
+ - lib/messaging_adapter/adapters/kafka.rb
77
+ - lib/messaging_adapter/adapters/rabbitmq.rb
78
+ homepage: http://rubygems.org/gems/messaging-adapter
79
+ licenses:
80
+ - MIT
81
+ metadata:
82
+ documentation_uri: https://github.com/tamhub/messaging-adapter
83
+ homepage_uri: https://github.com/tamhub/messaging-adapter
84
+ source_code_uri: https://github.com/tamhub/messaging-adapter
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.6.14
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Messaging adapter for Ruby.
105
+ test_files: []