class Bifrost::Message

A message is a letter that we send to a topic. It must contain a subject and body The receiver can process both fields on receipt of the message

Attributes

body[R]
id[R]
message_id[R]
meta[R]
resource_id[R]
status[R]

Public Class Methods

new(body, meta = {}) click to toggle source

A message must have a valid subject and body. The service bus is initialised in the Entity class

Calls superclass method Bifrost::Entity::new
# File lib/bifrost/message.rb, line 15
def initialize(body, meta = {})
  @meta = meta
  if body.is_a?(Azure::ServiceBus::BrokeredMessage)
    merge(body)
  else
    @body ||= body
    @status ||= :undelivered
  end
  super()
end

Public Instance Methods

publish(topic) click to toggle source

A message can be posted to a particular topic

# File lib/bifrost/message.rb, line 27
def publish(topic)
  if topic.exists?
    send_message(topic, create_brokered_message)
    true
  else
    false
  end
end
publish!(topic) click to toggle source

A message can be posted to a particular topic, if the message is succssfully delivered we return a unique identifier for the message

# File lib/bifrost/message.rb, line 38
def publish!(topic)
  if topic.exists?
    send_message(topic, create_brokered_message)
    message_id
  else
    raise Bifrost::Exceptions::MessageDeliveryError, "Could not post message to #{topic}"
  end
end
to_s() click to toggle source

A message when serialised to a string just renders the messag identifier

# File lib/bifrost/message.rb, line 48
def to_s
  id
end

Private Instance Methods

create_brokered_message() click to toggle source

Create the brokered message, note that the Bifrost message supports native ruby hashes, but during the transport these messages are converted to JSON strings

# File lib/bifrost/message.rb, line 76
def create_brokered_message
  payload = body.respond_to?(:to_json) ? body.to_json : body
  message = Azure::ServiceBus::BrokeredMessage.new(payload, meta)
  message.message_id = SecureRandom.uuid
  message
end
merge(raw_message) click to toggle source

Merges this message with the required properties of the raw Azure brokered message The sender might send a message other than JSON in which case we just send the raw data along

# File lib/bifrost/message.rb, line 56
def merge(raw_message)
  @status = :delivered
  begin
    @meta = raw_message.properties
    @body = JSON.parse(raw_message.body)
  rescue JSON::ParserError
    @body = raw_message
  end
  @message_id = raw_message.message_id
end
send_message(topic, message) click to toggle source

Create the message and attempt to deliver It

# File lib/bifrost/message.rb, line 68
def send_message(topic, message)
  @bus.interface.send_topic_message(topic.name, message)
  update_message_state_to_delivered(message)
end
update_message_state_to_delivered(message) click to toggle source

Update the status of the message post delivery

# File lib/bifrost/message.rb, line 84
def update_message_state_to_delivered(message)
  @status = :delivered
  @message_id = message.message_id
end