Class: AMQP::Client::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/amqp/client/message.rb

Overview

A message delivered from the broker

Defined Under Namespace

Classes: DeliveryInfo

Instance Attribute Summary collapse

Message coding collapse

Instance Method Summary collapse

Instance Attribute Details

#bodyString

The message body

Returns:

  • (String)


54
55
56
# File 'lib/amqp/client/message.rb', line 54

def body
  @body
end

#channelConnection::Channel (readonly)

The channel the message was deliviered to

Returns:



25
26
27
# File 'lib/amqp/client/message.rb', line 25

def channel
  @channel
end

#consumer_tagString? (readonly)

The tag of the consumer the message was deliviered to

Returns:

  • (String)
  • (nil)

    If the message was polled and not deliviered to a consumer



30
31
32
# File 'lib/amqp/client/message.rb', line 30

def consumer_tag
  @consumer_tag
end

#delivery_tagInteger (readonly)

The delivery tag of the message, used for acknowledge or reject the message

Returns:

  • (Integer)


34
35
36
# File 'lib/amqp/client/message.rb', line 34

def delivery_tag
  @delivery_tag
end

#exchangeString (readonly)

Name of the exchange the message was published to

Returns:

  • (String)


38
39
40
# File 'lib/amqp/client/message.rb', line 38

def exchange
  @exchange
end

#exchange_nameString (readonly)

Deprecated.

Returns:

  • (String)

See Also:



92
93
94
# File 'lib/amqp/client/message.rb', line 92

def exchange_name
  @exchange
end

#propertiesProperties

Message properties

Returns:



50
51
52
# File 'lib/amqp/client/message.rb', line 50

def properties
  @properties
end

#redeliveredBoolean (readonly)

True if the message have been delivered before

Returns:

  • (Boolean)


46
47
48
# File 'lib/amqp/client/message.rb', line 46

def redelivered
  @redelivered
end

#routing_keyString (readonly)

The routing key the message was published with

Returns:

  • (String)


42
43
44
# File 'lib/amqp/client/message.rb', line 42

def routing_key
  @routing_key
end

Instance Method Details

#acknil

Acknowledge the message

Returns:

  • (nil)


69
70
71
72
73
74
75
# File 'lib/amqp/client/message.rb', line 69

def ack
  return if @ack_or_reject_sent

  @channel.basic_ack(@delivery_tag)
  @ack_or_reject_sent = true
  nil
end

#decodeString

Decode the message body based on content_encoding

Returns:

  • (String)

    The decoded message body

Raises:



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/amqp/client/message.rb', line 122

def decode
  registry = @channel.connection.codec_registry
  strict = @channel.connection.strict_coding
  ce = @properties&.content_encoding
  coder = registry&.find_coder(ce)

  return coder.decode(@body, @properties) if coder

  is_unsupported = ce && ce != ""
  raise Error::UnsupportedContentEncoding, ce if is_unsupported && strict

  @body
end

#delivery_infoObject



56
57
58
59
60
61
62
63
64
65
# File 'lib/amqp/client/message.rb', line 56

def delivery_info
  @delivery_info ||= DeliveryInfo.new(
    consumer_tag:,
    delivery_tag:,
    redelivered:,
    exchange:,
    routing_key:,
    channel:
  )
end

#parseObject

Parse the message body based on content_type and content_encoding

Returns:

  • (Object)

    The parsed message body

Raises:



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/amqp/client/message.rb', line 102

def parse
  return @parsed unless @parsed.nil?

  registry = @channel.connection.codec_registry
  strict = @channel.connection.strict_coding
  decoded = decode
  ct = @properties&.content_type
  parser = registry&.find_parser(ct)

  return @parsed = parser.parse(decoded, @properties) if parser

  is_unsupported = ct && ct != "" && ct != "text/plain"
  raise Error::UnsupportedContentType, ct if is_unsupported && strict

  @parsed = decoded
end

#reject(requeue: false) ⇒ nil

Reject the message

Parameters:

  • requeue (Boolean) (defaults to: false)

    If true the message will be put back into the queue again, ready to be redelivered

Returns:

  • (nil)


80
81
82
83
84
85
86
# File 'lib/amqp/client/message.rb', line 80

def reject(requeue: false)
  return if @ack_or_reject_sent

  @channel.basic_reject(@delivery_tag, requeue:)
  @ack_or_reject_sent = true
  nil
end