Class: Stomper::Scopes::ReceiptScope

Inherits:
HeaderScope show all
Defined in:
lib/stomper/scopes/receipt_scope.rb

Overview

Automatically generates “receipt” headers, if none are present and applies a supplied callback to every receipt received for frames generated through it. As instances of this class rely on event callbacks attached to the underlying connection, it is entirely possible for those events to be triggered on Thread other than main. It is for this reason that synchronization is used to ensure the integrity of the internal list of receipt IDs that have not yet been processed through the callback.

Constant Summary collapse

FRAME_COMMANDS =

A list of frames that support being receipted.

Returns:

  • (Array<String>)
%w(SEND SUBSCRIBE UNSUBSCRIBE
BEGIN COMMIT ABORT ACK NACK DISCONNECT)

Constants included from Extensions::Common

Extensions::Common::EXTEND_BY_VERSION

Instance Attribute Summary

Attributes inherited from HeaderScope

#connection, #headers

Instance Method Summary collapse

Methods inherited from HeaderScope

#receipt_manager, #subscription_manager

Methods included from Extensions::Common

#abort, #ack, #begin, #commit, #disconnect, extend_by_protocol_version, #nack, #send, #subscribe, #unsubscribe

Constructor Details

#initialize(connection, headers) ⇒ ReceiptScope

Create a new receipt scope. All receiptable frames transmitted through this instance will use the same callback for handling the RECEIPT frame sent by the broker.



20
21
22
23
# File 'lib/stomper/scopes/receipt_scope.rb', line 20

def initialize(connection, headers)
  super
  @receipt_handler = nil
end

Instance Method Details

#apply_to(callback) ⇒ Object

Takes a block as a callback to invoke when a receipt is received.



26
27
28
# File 'lib/stomper/scopes/receipt_scope.rb', line 26

def apply_to(callback)
  @receipt_handler = callback
end

#transmit(frame) ⇒ Object

Transmits a frame. This method will add an auto-generated receipt header to the frame if one has not been set, and then set up a handler for the receipt value, invoking the callback set through #apply_to when the corresponding RECEIPT frame is received from the broker.

Parameters:



35
36
37
38
39
40
41
42
43
# File 'lib/stomper/scopes/receipt_scope.rb', line 35

def transmit(frame)
  if @receipt_handler && FRAME_COMMANDS.include?(frame.command)
    r_id = frame[:receipt]
    r_id = ::Stomper::Support.next_serial if r_id.nil? || r_id.empty?
    receipt_manager.add(r_id, @receipt_handler)
    frame[:receipt] = r_id
  end
  super(frame)
end