Class: Hutch::Publisher

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/hutch/publisher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger, logger, logger=, setup_logger

Constructor Details

#initialize(connection, channel, exchange, config = Hutch::Config) ⇒ Publisher

Returns a new instance of Publisher.



10
11
12
13
14
15
# File 'lib/hutch/publisher.rb', line 10

def initialize(connection, channel, exchange, config = Hutch::Config)
  @connection = connection
  @channel    = channel
  @exchange   = exchange
  @config     = config
end

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



8
9
10
# File 'lib/hutch/publisher.rb', line 8

def channel
  @channel
end

#configObject (readonly)

Returns the value of attribute config.



8
9
10
# File 'lib/hutch/publisher.rb', line 8

def config
  @config
end

#connectionObject (readonly)

Returns the value of attribute connection.



8
9
10
# File 'lib/hutch/publisher.rb', line 8

def connection
  @connection
end

#exchangeObject (readonly)

Returns the value of attribute exchange.



8
9
10
# File 'lib/hutch/publisher.rb', line 8

def exchange
  @exchange
end

Instance Method Details

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



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/hutch/publisher.rb', line 17

def publish(routing_key, message, properties = {}, options = {})
  ensure_connection!(routing_key, message)

  serializer = options[:serializer] || config[:serializer]

  non_overridable_properties = {
    routing_key:  routing_key,
    timestamp:    connection.current_timestamp,
    content_type: serializer.content_type,
  }
  properties[:message_id]   ||= generate_id

  payload = serializer.encode(message)

  log_publication(serializer, payload, routing_key)

  response = exchange.publish(payload, {persistent: true}.
    merge(properties).
    merge(global_properties).
    merge(non_overridable_properties))

  channel.wait_for_confirms if config[:force_publisher_confirms]
  response
end