Class: AceMQ::AMQP::PublishContext

Inherits:
Object
  • Object
show all
Defined in:
lib/acemq/amqp/interceptors.rb

Overview

A message on its way out, as an interceptor sees it.

Everything on it can be changed, and changing it changes what is sent: the exchange and the routing key redirect the message, the envelope carries the headers, and the payload is the value that has not been encoded yet — so an interceptor can rewrite it while it is still a Ruby object rather than trying to patch bytes.

The envelope is replaced rather than mutated, because an Envelope is frozen on purpose: what a log line said about a message should not change after it was written. #set_header does the replacing, which is what most interceptors want.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exchange:, routing_key:, envelope:, payload:, reply_to: nil, mandatory: false) ⇒ PublishContext

Returns a new instance of PublishContext.



49
50
51
52
53
54
55
56
57
# File 'lib/acemq/amqp/interceptors.rb', line 49

def initialize(exchange:, routing_key:, envelope:, payload:, reply_to: nil,
               mandatory: false)
  @exchange = exchange
  @routing_key = routing_key
  @envelope = envelope
  @payload = payload
  @reply_to = reply_to
  @mandatory = mandatory
end

Instance Attribute Details

#envelope ⇒ Object

Returns the value of attribute envelope.



34
35
36
# File 'lib/acemq/amqp/interceptors.rb', line 34

def envelope
  @envelope
end

#exchange ⇒ Object

Returns the value of attribute exchange.



34
35
36
# File 'lib/acemq/amqp/interceptors.rb', line 34

def exchange
  @exchange
end

#mandatory ⇒ Object

Whether the broker must hand this message back rather than drop it when it reaches no queue.

Readable and writable, so an interceptor that redirects a message can also decide whether reaching nothing is allowed to be a silence. False unless the caller asked, which is what Go and Python default to as well.



47
48
49
# File 'lib/acemq/amqp/interceptors.rb', line 47

def mandatory
  @mandatory
end

#payload ⇒ Object

Returns the value of attribute payload.



34
35
36
# File 'lib/acemq/amqp/interceptors.rb', line 34

def payload
  @payload
end

#reply_to ⇒ Object

AMQP's own reply-to property, which is not a header and so is not in the envelope. Nil for the messages that expect no answer, which is most of them; AceMQ::AMQP::Patterns::Requester is what usually sets it.



39
40
41
# File 'lib/acemq/amqp/interceptors.rb', line 39

def reply_to
  @reply_to
end

#routing_key ⇒ Object

Returns the value of attribute routing_key.



34
35
36
# File 'lib/acemq/amqp/interceptors.rb', line 34

def routing_key
  @routing_key
end

Instance Method Details

#set_header(name, value) ⇒ Object

Adds an application header to the message about to be sent.

Reserved x-acemq- names are refused, here as everywhere: an interceptor that quietly overwrote the attempt count would break the retry engine from outside it, with nothing in the code to show why.

Raises:

  • (ArgumentError) —

    when the name is reserved



66
67
68
# File 'lib/acemq/amqp/interceptors.rb', line 66

def set_header(name, value)
  @envelope = @envelope.with(headers: @envelope.headers.merge(name.to_s => value))
end

#to_s ⇒ Object



70
# File 'lib/acemq/amqp/interceptors.rb', line 70

def to_s = "publish #{@envelope.id} to #{@exchange.inspect}/#{@routing_key}"