Class: ActiveMessage::Broker

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/active_message/broker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBroker

Returns a new instance of Broker.



7
8
# File 'lib/active_message/broker.rb', line 7

def initialize
end

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



5
6
7
# File 'lib/active_message/broker.rb', line 5

def configuration
  @configuration
end

#connectionObject

Returns the value of attribute connection.



5
6
7
# File 'lib/active_message/broker.rb', line 5

def connection
  @connection
end

#exchangeObject

Returns the value of attribute exchange.



5
6
7
# File 'lib/active_message/broker.rb', line 5

def exchange
  @exchange
end

Instance Method Details

#ack(channel, delivery_tag) ⇒ Object



29
30
31
# File 'lib/active_message/broker.rb', line 29

def ack(channel, delivery_tag)
  channel.ack(delivery_tag, false)
end

#connect(config) ⇒ Object



10
11
12
13
14
15
# File 'lib/active_message/broker.rb', line 10

def connect(config)
  @configuration  = config
  @connection     = Bunny.new(host: ActiveMessage::Configuration.get(:host), auth_mechanism: "EXTERNAL", tls: ActiveMessage::Configuration.get(:tls), tls_cert: ActiveMessage::Configuration.get(:tls_cert), tls_key: ActiveMessage::Configuration.get(:tls_key), tls_ca_certificates: ActiveMessage::Configuration.get(:tls_ca_certificates))
  @connection.start
  @exchange       = @connection.topic(@configuration[:exchange] || "demo", auto_delete: true)
end

#disconnectObject



17
18
19
# File 'lib/active_message/broker.rb', line 17

def disconnect
  self.connection.close
end

#generate_idObject



48
49
50
# File 'lib/active_message/broker.rb', line 48

def generate_id
  SecureRandom.uuid
end

#nack(channel, delivery_tag) ⇒ Object



33
34
35
# File 'lib/active_message/broker.rb', line 33

def nack(channel, delivery_tag)
  channel.nack(delivery_tag, false, false)
end

#publish(routing_key, message, properties = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/active_message/broker.rb', line 37

def publish(routing_key, message, properties = {})
  ActiveMessage::Logger.debug "Publishing Message => '#{message.inspect}' to #{routing_key}"
  non_overridable_properties = {
    routing_key: routing_key,
    timestamp: Time.now.to_i,
    content_type: 'application/json'
  }
  properties[:message_id] ||= generate_id
  exchange.publish(message, { persistent: true }.merge(properties).merge(non_overridable_properties))
end