Class: RepeatedHeadersExample

### Home Classes Methods

### In Files

- examples/putget11_rh1.rb

### Parent

Object

### Methods

- ::new
- #run

### Files

- CHANGELOG.rdoc
- LICENSE

### Class/Module Index

Quicksearch

- Stomp
- Stomp::Client
- Stomp::Connection
- Stomp::Error
- Stomp::Error::BrokerException
- Stomp::Error::ConnectReadTimeout
- Stomp::Error::DuplicateSubscription
- Stomp::Error::InvalidFormat
- Stomp::Error::InvalidHeartBeatHeaderError
- Stomp::Error::InvalidMessageLength
- Stomp::Error::InvalidServerCommand
- Stomp::Error::LoggerConnectionError
- Stomp::Error::MalformedFailoverOptionsError
- Stomp::Error::MaxReconnectAttempts
- Stomp::Error::MessageIDRequiredError
- Stomp::Error::NilMessageError
- Stomp::Error::NoCurrentConnection
- Stomp::Error::PacketParsingTimeout
- Stomp::Error::ProducerFlowControlException
- Stomp::Error::ProtocolError11p
- Stomp::Error::ProtocolErrorConnect
- Stomp::Error::ProtocolErrorEmptyHeaderKey
- Stomp::Error::ProtocolErrorEmptyHeaderValue
- Stomp::Error::ProtocolException
- Stomp::Error::ReadReceiptTimeoutException
- Stomp::Error::SSLClientParamsError
- Stomp::Error::SSLNoCertFileError
- Stomp::Error::SSLNoKeyFileError
- Stomp::Error::SSLNoTruststoreFileError
- Stomp::Error::SSLUnreadableCertFileError
- Stomp::Error::SSLUnreadableKeyFileError
- Stomp::Error::SSLUnreadableTruststoreFileError
- Stomp::Error::SocketOpenTimeout
- Stomp::Error::StartTimeoutException
- Stomp::Error::StompException
- Stomp::Error::StompServerError
- Stomp::Error::SubscriptionRequiredError
- Stomp::Error::UTF8ValidationError
- Stomp::Error::UnsupportedProtocolError
- Stomp::HeaderCodec
- Stomp::Message
- Stomp::NullLogger
- Stomp::SSLParams
- Client11Example1
- Client11PutGet1
- Connection11Example1
- Connection11Example2
- ExampleConsumer
- ExamplePublisher
- ExampleRubyCiphers
- ExampleSSL1
- ExampleSSL1C
- ExampleSSL2
- ExampleSSL2C
- ExampleSSL3
- ExampleSSL3C
- ExampleSSL4
- ExampleSSL4C
- ExampleSSLCtxOptions
- ExampleSSLNewParm
- ExampleTopicConsumer
- ExampleTopicPublisher
- Hash
- HeartBeatExample1
- LoggerExample
- Object
- Publish11Example1
- Receive11Example1
- Receive11Example2
- RepeatedHeadersExample
- SSLLoggerExample
- Slogger
- Stomp11Common
- TestAnonymous
- TestBase
- TestClient
- TestConnection
- TestConnection1P
- TestMessage
- TestSSL
- TestURLLogins
- Tlogger

No matching classes.

# RepeatedHeadersExample

## Stomp 1.1 Send/Receive Example - Repeated Headers

Purpose: to demonstrate sending and receiving using Stomp 1.1, and an unusual aspect of the specification. What is demonstrated here is the use of 'repeated headers'. Note that brokers MAY support repeated headers as demonstrated, but are not required to provide this support. This example should run against the Apollo broker. It will not currently run against RabbitMQ. YMMV depending on your broker.

See: stomp.github.com/stomp-specification-1.1.html#Repeated_Header_Entries

### Public Class Methods

new() click to toggle source

Initialize.

    # File examples/putget11_rh1.rb, line 27
    def initialize
    end

### Public Instance Methods

run() click to toggle source

Run example.

    # File examples/putget11_rh1.rb, line 30
    def run
      conn = get_connection() # Use helper method to obtain a Stomp#connection
      raise "Unexpected protocol level" if conn.protocol != Stomp::SPL_11
      #
      # The gem supports repeated headers by allowing the 'value' part of a header
      # to be an Array instance.
      #
      # On 'publish', all values in the Array are placed on the wire and sent to the
      # broker in order.
      #
      # On 'receive', if repeated headers are detected, an Array instance is created
      # to hold the repeated values.  This is presented the the calling client to
      # be processed per client requirements.
      #
      qname = "/queue/nodea.nodeb.nodec"
      data = "message payload: #{Time.now.to_f}"
      key2_repeats = ["key2val3", "key2val2", "key2val1" ]
      headers = {"key1" => "value1",  # A normal header
        "key2" => key2_repeats,       # A repeated header
        "key3" => "value3",           # Another normal header
      }
      #
      # Ship it.
      #
      conn.publish qname, data , headers
      puts "Sent data: #{data}"
      #
      # Receive phase.
      #
      uuid = conn.uuid()
      conn.subscribe qname, {"id" => uuid}
      received = conn.receive
      conn.unsubscribe qname, {"id" => uuid}
      #
      # Check that we received what we sent.
      #
      raise "Unexpected payload" unless data == received.body
      raise "Missing key" unless received.headers["key2"]
      raise "Repeats not present" unless received.headers.has_value?(key2_repeats)
      raise "Unexpected repeat values" unless key2_repeats == received.headers["key2"]
      #
      # Demonstrate how to process repeated headers received by display of those
      # received headers for a visual check.
      #
      received.headers.each_pair do |k,v|
        if v.is_a?(Array)
          v.each do |e|
            puts "#{k}:#{e}"
          end
        else
          puts "#{k}:#{v}"
        end
      end
      #
      # And finally, disconnect.
      #
      conn.disconnect
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.