subscriptions-config 0.2.0
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.
- checksums.yaml +7 -0
- data/lib/subscriptions-config.rb +372 -0
- metadata +44 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: f24471ee1244174cf8a1f8a73d4a9a63d0cd0a8965127ce93907c2e58a066a78
|
|
4
|
+
data.tar.gz: a7b11ee16615955247750dbc4098d92d8c2f1e197bd712872ee27aab7dc42614
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: bb5944a032b719bb48a5db5d3f3cc80054563dda5ef32329d23770f34f5c51a4eaff2b44939a2618ace3a60802598fa7368b01f7a4c6f3eeae6d4fc3a93af789
|
|
7
|
+
data.tar.gz: b05dc1b80257ab59a0ede4e34e7516e9c6a5f7fbe572fc5347868a715e865a8c2fb583daf15b647ccf4edcf3a296f02621a629b20f019127d66a22fc4f407b83
|
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
class SubscriptionsConfig
|
|
2
|
+
puts 'subscriptions-config'
|
|
3
|
+
|
|
4
|
+
DEFAULT_CONFIG = {
|
|
5
|
+
hostname: 'localhost',
|
|
6
|
+
message_bus_type: 'IbmMQ',
|
|
7
|
+
exchange: 'dataDelivery',
|
|
8
|
+
routing_spec: "",
|
|
9
|
+
queue_base: "",
|
|
10
|
+
durable: false,
|
|
11
|
+
model: "",
|
|
12
|
+
extension: "",
|
|
13
|
+
port: "1414",
|
|
14
|
+
trace_level: "0",
|
|
15
|
+
credentials: "",
|
|
16
|
+
vhost: "",
|
|
17
|
+
keep_queue: "n",
|
|
18
|
+
sub_name: ""
|
|
19
|
+
}.freeze
|
|
20
|
+
|
|
21
|
+
def initialize(config)
|
|
22
|
+
@config = DEFAULT_CONFIG.merge(config)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def create!
|
|
26
|
+
create_rabbitmq! if message_bus_type == 'RabbitMQ'
|
|
27
|
+
create_ibmmq! if message_bus_type == 'IbmMQ'
|
|
28
|
+
self
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def delete!
|
|
32
|
+
delete_rabbit! if message_bus_type == 'RabbitMQ'
|
|
33
|
+
delete_ibmmq! if message_bus_type == 'IbmMQ'
|
|
34
|
+
self
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def show_topics
|
|
38
|
+
puts topics
|
|
39
|
+
self
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def show_bindings
|
|
43
|
+
puts bindings
|
|
44
|
+
self
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def show_environment
|
|
48
|
+
puts "Bus Type: #{message_bus_type}"
|
|
49
|
+
show_environment_ibmmq if message_bus_type == 'IbmMQ'
|
|
50
|
+
show_environment_rabbit if message_bus_type == 'RabbitMQ'
|
|
51
|
+
self
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def subscription_name(queue_name, routing_key)
|
|
55
|
+
if sub_name.nil?
|
|
56
|
+
routing_key_digest = Digest::SHA1.hexdigest(routing_key)[0..8]
|
|
57
|
+
"#{queue_name}.#{routing_key_digest}"
|
|
58
|
+
else
|
|
59
|
+
sub_name
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def show_keep_queue
|
|
64
|
+
puts keep_queue
|
|
65
|
+
self
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def show_sub_name
|
|
69
|
+
puts sub_name
|
|
70
|
+
self
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
private
|
|
74
|
+
def bindings
|
|
75
|
+
@bindings ||= begin
|
|
76
|
+
topics.map do |topic|
|
|
77
|
+
if message_bus_type == 'IbmMQ'
|
|
78
|
+
if (exchange.nil? && extension.nil?)
|
|
79
|
+
topic_str = topic.gsub(".","/")
|
|
80
|
+
elsif extension.nil?
|
|
81
|
+
topic_str = exchange+"/"+topic.gsub(".","/")
|
|
82
|
+
else
|
|
83
|
+
topic_str = extension+"/"+exchange+"/"+topic.gsub(".","/")
|
|
84
|
+
end
|
|
85
|
+
subscription_name = subscription_name(queue_name, topic_str)
|
|
86
|
+
hash = {:topic_str => topic_str, :subscription_name => subscription_name }
|
|
87
|
+
elsif message_bus_type == 'RabbitMQ'
|
|
88
|
+
topic_str=topic
|
|
89
|
+
subscription_name = queue_name+'_'+topic_str # not used in rabbit mq
|
|
90
|
+
hash = {:topic_str => topic_str, :subscription_name => subscription_name }
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def topics
|
|
97
|
+
@topics ||= @config[:routing_spec].split(",")
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def connection_name
|
|
101
|
+
if message_bus_type == 'RabbitMQ'
|
|
102
|
+
@connection_name = hostname.to_s+':'+port.to_s
|
|
103
|
+
else
|
|
104
|
+
@connection_name = hostname.to_s+'('+port.to_s+')'
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def mq_url
|
|
109
|
+
@mq_url="amqp://"+username+":"+password+"@"+connection_name+"/"+vhost
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def queue_name
|
|
113
|
+
if message_bus_type == 'RabbitMQ'
|
|
114
|
+
@queue_name = queue_base
|
|
115
|
+
elsif extension.nil?
|
|
116
|
+
@queue_name = queue_base
|
|
117
|
+
else
|
|
118
|
+
@queue_name = queue_base+'.'+extension
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def extension
|
|
123
|
+
@config[:extension]
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def exchange
|
|
127
|
+
@config[:exchange]
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def model
|
|
131
|
+
@config[:model]
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def durable
|
|
135
|
+
@config[:durable]
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def queue_base
|
|
139
|
+
@config[:queue_base]
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def sub_name
|
|
143
|
+
@config[:sub_name]
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def hostname
|
|
147
|
+
@config[:hostname]
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def channel
|
|
151
|
+
@config[:channel]
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def port
|
|
155
|
+
@config[:port]
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def message_bus_type
|
|
159
|
+
@config[:message_bus_type]
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
def routing_spec
|
|
163
|
+
@config[:routing_spec]
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def trace_level
|
|
167
|
+
@config[:trace_level].to_i
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def vhost
|
|
171
|
+
@config[:vhost]
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
def keep_queue
|
|
175
|
+
@config[:keep_queue]
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def credentials
|
|
179
|
+
@config[:credentials]
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
def username
|
|
183
|
+
items ||= credentials.split("/")
|
|
184
|
+
@username = items[0]
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
def password
|
|
188
|
+
items ||= credentials.split("/")
|
|
189
|
+
@password = items[1]
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
# RabbitMQ version of create!
|
|
193
|
+
def create_rabbitmq!
|
|
194
|
+
conn = Bunny.new(:host => hostname, :vhost => vhost, :user => username, :password => password)
|
|
195
|
+
|
|
196
|
+
#conn = Bunny.new("amqp://cdu-qa:cdu2018@r00xgjn0c.bnymellon.net:5672")
|
|
197
|
+
conn.start
|
|
198
|
+
|
|
199
|
+
ch = conn.create_channel
|
|
200
|
+
q = ch.queue(queue_name, :durable => true, :exclusive => false)
|
|
201
|
+
x = ch.topic(exchange, durable: true)
|
|
202
|
+
bindings.each do |binding|
|
|
203
|
+
if trace_level > 0
|
|
204
|
+
puts " -- Create Subscription for routing key: #{binding[:topic_str]} to queue: #{queue_name}"
|
|
205
|
+
end
|
|
206
|
+
q.bind(exchange, routing_key: binding[:topic_str])
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
q.subscribe do |delivery_info, metadata, payload|
|
|
210
|
+
puts "Received #{payload}"
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
bindings.each do |binding|
|
|
214
|
+
message_body="Test topic string: "+binding[:topic_str]
|
|
215
|
+
x.publish(message_body, routing_key: binding[:topic_str])
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
sleep 1.0
|
|
219
|
+
conn.close
|
|
220
|
+
self
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
def delete_rabbitmq!
|
|
224
|
+
conn = Bunny.new(:host => hostname, :vhost => vhost, :user => username, :password => password)
|
|
225
|
+
conn.start
|
|
226
|
+
ch = conn.create_channel
|
|
227
|
+
puts " -- remove queue #{queue_name} and all it's bindings" if trace_level > 0
|
|
228
|
+
ch.queue_delete(queue_name)
|
|
229
|
+
ch.close
|
|
230
|
+
conn.close
|
|
231
|
+
self
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
def show_environment_rabbit
|
|
235
|
+
puts "credentials=\"#{username}/#{password}\""
|
|
236
|
+
puts "MQ_SUBSCRIBER_URL=\"#{mq_url}\""
|
|
237
|
+
puts "MQ_SUBSCRIBER_EXCHANGE=\"#{exchange}\""
|
|
238
|
+
puts "MQ_QUEUE_NAME=#{queue_name}"
|
|
239
|
+
puts "MQ_ROUTING_KEY=#{routing_spec}"
|
|
240
|
+
self
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
def show_environment_ibmmq
|
|
244
|
+
puts "MQ_SUBSCRIBER_URL=\"#{channel}/TCP/#{connection_name}\""
|
|
245
|
+
if extension.nil?
|
|
246
|
+
puts "MQ_SUBSCRIBER_EXCHANGE=\"#{exchange}\""
|
|
247
|
+
else
|
|
248
|
+
puts "MQ_SUBSCRIBER_EXCHANGE=\"#{extension}/#{exchange}\""
|
|
249
|
+
end
|
|
250
|
+
puts "MQ_QUEUE_NAME=#{queue_name}"
|
|
251
|
+
puts "MQ_ROUTING_KEY=#{routing_spec}"
|
|
252
|
+
if keep_queue == "y" or keep_queue == "Y" then
|
|
253
|
+
puts "KEEP_QUEUE=YES"
|
|
254
|
+
end
|
|
255
|
+
self
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
def create_ibmmq!
|
|
259
|
+
if trace_level > 0
|
|
260
|
+
puts "Create Subscription Connection: #{channel}/TCP/#{connection_name} "
|
|
261
|
+
puts " -- Create Queue #{queue_name} from model #{model}"
|
|
262
|
+
end
|
|
263
|
+
WMQ::QueueManager.connect(
|
|
264
|
+
channel_name: channel,
|
|
265
|
+
trp_type: 'TCP',
|
|
266
|
+
connection_name: connection_name) do |qmgr|
|
|
267
|
+
begin
|
|
268
|
+
qmgr.open_queue(
|
|
269
|
+
q_name: model,
|
|
270
|
+
dynamic_q_name: queue_name,
|
|
271
|
+
mode: :input
|
|
272
|
+
) do |target_queue|
|
|
273
|
+
# puts 'Queue Open Status: '+target_queue.reason
|
|
274
|
+
# puts target_queue.inspect
|
|
275
|
+
# puts 'New Queue: '+target_queue.name
|
|
276
|
+
if trace_level > 0
|
|
277
|
+
puts "== queue == #{target_queue.name.to_s} status: #{target_queue.reason}"
|
|
278
|
+
# puts target_queue.reason
|
|
279
|
+
end
|
|
280
|
+
end
|
|
281
|
+
rescue WMQ::WMQException => mqe
|
|
282
|
+
exception_string="WMQ::Queue#open(). Error opening Queue:"+model.to_s+", reason:MQRC_OBJECT_ALREADY_EXISTS[2100]"
|
|
283
|
+
if mqe.message.eql?exception_string
|
|
284
|
+
puts "== queue == #{queue_name} exists"
|
|
285
|
+
else
|
|
286
|
+
puts "WARNING: there was a problem creating the queue #{mqe.to_s}"
|
|
287
|
+
puts "Exception Class: #{ mqe.class.name }"
|
|
288
|
+
puts "Exception Message: #{ mqe.message }"
|
|
289
|
+
puts "Exception Backtrace: #{ mqe.backtrace }"
|
|
290
|
+
end
|
|
291
|
+
end # end do target_queue
|
|
292
|
+
|
|
293
|
+
bindings.each do |binding|
|
|
294
|
+
if trace_level > 0
|
|
295
|
+
puts " -- Create Subscription Name: #{binding[:subscription_name]} for topic string: #{binding[:topic_str]} to queue: #{queue_name}"
|
|
296
|
+
end
|
|
297
|
+
sub = WMQ::Subscription.new( queue_manager: qmgr,
|
|
298
|
+
topic_string: binding[:topic_str],
|
|
299
|
+
subscription_name: binding[:subscription_name],
|
|
300
|
+
durable: true,
|
|
301
|
+
managed: false,
|
|
302
|
+
resume: true,
|
|
303
|
+
keep: true,
|
|
304
|
+
q_name: queue_name,
|
|
305
|
+
mode: :input)
|
|
306
|
+
sub.open
|
|
307
|
+
if trace_level > 0
|
|
308
|
+
# puts sub.reason
|
|
309
|
+
# puts "Subscription: Connect: #{hostname}:#{port} / #{channel}\n"
|
|
310
|
+
puts "== sub == status: #{sub.reason}"
|
|
311
|
+
end
|
|
312
|
+
sub.close
|
|
313
|
+
end # end do bindings
|
|
314
|
+
end # end do qmgr
|
|
315
|
+
self
|
|
316
|
+
end
|
|
317
|
+
|
|
318
|
+
def delete_ibmmq!
|
|
319
|
+
puts 'delete_ibmmq'
|
|
320
|
+
WMQ::QueueManager.connect(
|
|
321
|
+
channel_name: channel,
|
|
322
|
+
trp_type: 'TCP',
|
|
323
|
+
connection_name: connection_name) do |qmgr|
|
|
324
|
+
puts "4"
|
|
325
|
+
bindings.each do |binding|
|
|
326
|
+
puts "5"
|
|
327
|
+
subscription = WMQ::Subscription.new( queue_manager: qmgr,
|
|
328
|
+
topic_string: binding[:topic_str],
|
|
329
|
+
subscription_name: binding[:subscription_name],
|
|
330
|
+
durable: true,
|
|
331
|
+
managed: false,
|
|
332
|
+
resume: true,
|
|
333
|
+
keep: false,
|
|
334
|
+
q_name: queue_name,
|
|
335
|
+
mode: :input)
|
|
336
|
+
puts "6"
|
|
337
|
+
subscription.open
|
|
338
|
+
if trace_level > 0
|
|
339
|
+
puts " -- Delete Subscription Name: #{binding[:subscription_name]} for topic string: #{binding[:topic_str]} to queue: #{queue_name}"
|
|
340
|
+
# puts subscription.reason
|
|
341
|
+
end
|
|
342
|
+
subscription.close
|
|
343
|
+
end # end do bindings
|
|
344
|
+
|
|
345
|
+
if keep_queue != "y" and keep_queue != "Y" then
|
|
346
|
+
qmgr.open_queue(
|
|
347
|
+
q_name: queue_name,
|
|
348
|
+
mode: :input,
|
|
349
|
+
close_options: WMQ::MQCO_DELETE
|
|
350
|
+
) do |target_queue|
|
|
351
|
+
if trace_level > 0
|
|
352
|
+
puts 'Queue Open Status: '+target_queue.reason
|
|
353
|
+
puts 'Queue to delete: '+target_queue.name
|
|
354
|
+
puts target_queue.reason
|
|
355
|
+
end
|
|
356
|
+
puts "Clear old messages ..."
|
|
357
|
+
message_count=0
|
|
358
|
+
message = WMQ::Message.new
|
|
359
|
+
while target_queue.get(message: message, wait: 2000)
|
|
360
|
+
message.descriptor = {}
|
|
361
|
+
message_count += 1
|
|
362
|
+
end # end while get()
|
|
363
|
+
puts "Removed #{message_count} messages from queue #{target_queue.name} "
|
|
364
|
+
end # end do target_queue
|
|
365
|
+
else
|
|
366
|
+
puts "Subscription queue #{queue_name} preserved"
|
|
367
|
+
end
|
|
368
|
+
end # end do qmgr
|
|
369
|
+
self
|
|
370
|
+
end # end delete_subscription
|
|
371
|
+
|
|
372
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: subscriptions-config
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.2.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Bill Whiting
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2018-12-04 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Provides a class to create and destroy durable subscriptions in both
|
|
14
|
+
IBM-MQ and RabbitMQ
|
|
15
|
+
email: william.whiting@bnymellon.com
|
|
16
|
+
executables: []
|
|
17
|
+
extensions: []
|
|
18
|
+
extra_rdoc_files: []
|
|
19
|
+
files:
|
|
20
|
+
- lib/subscriptions-config.rb
|
|
21
|
+
homepage: https://gitlab.bnymellon.net/zmq/zmq-00/subscription-mgr
|
|
22
|
+
licenses:
|
|
23
|
+
- MIT
|
|
24
|
+
metadata: {}
|
|
25
|
+
post_install_message:
|
|
26
|
+
rdoc_options: []
|
|
27
|
+
require_paths:
|
|
28
|
+
- lib
|
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - ">="
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '0'
|
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
35
|
+
requirements:
|
|
36
|
+
- - ">="
|
|
37
|
+
- !ruby/object:Gem::Version
|
|
38
|
+
version: '0'
|
|
39
|
+
requirements: []
|
|
40
|
+
rubygems_version: 3.4.20
|
|
41
|
+
signing_key:
|
|
42
|
+
specification_version: 4
|
|
43
|
+
summary: create durable subscriptions
|
|
44
|
+
test_files: []
|