Class: Publisher
- Inherits:
-
Object
- Object
- Publisher
- Defined in:
- lib/bunny-pub-sub/publisher.rb
Instance Attribute Summary collapse
-
#channel ⇒ Object
readonly
Returns the value of attribute channel.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#exchange ⇒ Object
readonly
Returns the value of attribute exchange.
Instance Method Summary collapse
- #close_channel ⇒ Object
- #close_connection ⇒ Object
- #connect_publisher(publisher_confirms = true) ⇒ Object
- #create_channel ⇒ Object
- #default_routing_key=(routing_key) ⇒ Object
- #disconnect_publisher ⇒ Object
-
#initialize(config) ⇒ Publisher
constructor
A new instance of Publisher.
- #publish_message(msg, routing_key = nil) ⇒ Object
- #set_topic_exchange ⇒ Object
- #start_connection ⇒ Object
- #unset_default_routing_key ⇒ Object
Constructor Details
#initialize(config) ⇒ Publisher
Returns a new instance of Publisher.
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/bunny-pub-sub/publisher.rb', line 13 def initialize(config) return unless valid_config? config return unless routing_key_exists? config @config = config @connection = Bunny.new( hostname: @config[:RABBITMQ_HOSTNAME], username: @config[:RABBITMQ_USERNAME], password: @config[:RABBITMQ_PASSWORD] ) end |
Instance Attribute Details
#channel ⇒ Object (readonly)
Returns the value of attribute channel.
10 11 12 |
# File 'lib/bunny-pub-sub/publisher.rb', line 10 def channel @channel end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
11 12 13 |
# File 'lib/bunny-pub-sub/publisher.rb', line 11 def config @config end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
8 9 10 |
# File 'lib/bunny-pub-sub/publisher.rb', line 8 def connection @connection end |
#exchange ⇒ Object (readonly)
Returns the value of attribute exchange.
9 10 11 |
# File 'lib/bunny-pub-sub/publisher.rb', line 9 def exchange @exchange end |
Instance Method Details
#close_channel ⇒ Object
78 79 80 |
# File 'lib/bunny-pub-sub/publisher.rb', line 78 def close_channel @channel.close end |
#close_connection ⇒ Object
82 83 84 |
# File 'lib/bunny-pub-sub/publisher.rb', line 82 def close_connection @connection.close end |
#connect_publisher(publisher_confirms = true) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/bunny-pub-sub/publisher.rb', line 39 def connect_publisher(publisher_confirms = true) start_connection @publisher_confirms = publisher_confirms create_channel set_topic_exchange end |
#create_channel ⇒ Object
30 31 32 33 |
# File 'lib/bunny-pub-sub/publisher.rb', line 30 def create_channel @channel = @connection.create_channel @channel.confirm_select if @publisher_confirms end |
#default_routing_key=(routing_key) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/bunny-pub-sub/publisher.rb', line 46 def default_routing_key=(routing_key) if routing_key.nil? || routing_key&.strip&.empty? puts 'routing_key must be defined and must not be empty' return end @config[:ROUTING_KEY] = routing_key end |
#disconnect_publisher ⇒ Object
86 87 88 89 |
# File 'lib/bunny-pub-sub/publisher.rb', line 86 def disconnect_publisher close_channel close_connection end |
#publish_message(msg, routing_key = nil) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/bunny-pub-sub/publisher.rb', line 59 def (msg, routing_key=nil) if (routing_key.nil? || routing_key&.strip&.empty?) && (@config[:ROUTING_KEY].nil? || @config[:ROUTING_KEY]&.strip&.empty?) return 'No default routing key exists in config. You must specify one.' end @exchange.publish(msg.to_json, routing_key: routing_key || @config[:ROUTING_KEY], persistent: true) return puts ' [x] Message sent!' unless @publisher_confirms success = @channel.wait_for_confirms return puts ' [x] Message sent!' if success @channel.nacked_set.each do |n| # Do something with the nacked message ID # TODO: Add a yielding block to this puts " [x] Message #{n} failed." end end |
#set_topic_exchange ⇒ Object
35 36 37 |
# File 'lib/bunny-pub-sub/publisher.rb', line 35 def set_topic_exchange @exchange = @channel.topic(@config[:EXCHANGE_NAME], durable: true) end |
#start_connection ⇒ Object
26 27 28 |
# File 'lib/bunny-pub-sub/publisher.rb', line 26 def start_connection ServicesManager.instance.start_connection(@connection, 0) end |
#unset_default_routing_key ⇒ Object
55 56 57 |
# File 'lib/bunny-pub-sub/publisher.rb', line 55 def unset_default_routing_key @config[:ROUTING_KEY] = nil end |