Questo contenuto non è disponibile nella lingua selezionata.

Chapter 4. Examples


This chapter demonstrates the use of AMQ Ruby through example programs. To run them, make sure you have completed the steps in the Chapter 2, Installation chapter for your environment and you have a running and configured broker.

4.1. Sending Messages

This client program connects to a server using CONNECTION_URL, creates a sender for target ADDRESS, sends a message containing MESSAGE_BODY, closes the connection, and exits.

Example: Sending Messages

require 'qpid_proton'

class SendHandler < Qpid::Proton::MessagingHandler
  def initialize(conn_url, address, message_body)
    super()

    @conn_url = conn_url
    @address = address
    @message_body = message_body
  end

  def on_container_start(container)
    conn = container.connect(@conn_url)
    conn.open_sender(@address)
  end

  def on_sender_open(sender)
    puts "SEND: Opened sender for target address '#{sender.target.address}'\n"
  end

  def on_sendable(sender)
    message = Qpid::Proton::Message.new(@message_body)
    sender.send(message)

    puts "SEND: Sent message '#{message.body}'\n"

    sender.close
    sender.connection.close
  end
end

if ARGV.size == 3
  conn_url, address, message_body = ARGV
else
  abort "Usage: send.rb CONNECTION-URL ADDRESS MESSAGE-BODY\n"
end

handler = SendHandler.new(conn_url, address, message_body)
container = Qpid::Proton::Container.new(handler)
container.run
Copy to Clipboard Toggle word wrap

Running the Example

To run the example program, copy it to a local file and invoke it using the ruby command.

$ ruby send.rb amqp://localhost queue1 hello
Copy to Clipboard Toggle word wrap

4.2. Receiving Messages

This client program connects to a server using CONNECTION_URL, creates a receiver for source ADDRESS, and receives messages until it is terminated or it reaches COUNT messages.

Example: Receiving Messages

require 'qpid_proton'

class ReceiveHandler < Qpid::Proton::MessagingHandler
  def initialize(conn_url, address, desired)
    super()

    @conn_url = conn_url
    @address = address

    @desired = desired
    @received = 0
  end

  def on_container_start(container)
    conn = container.connect(@conn_url)
    conn.open_receiver(@address)
  end

  def on_receiver_open(receiver)
    puts "RECEIVE: Opened receiver for source address '#{receiver.source.address}'\n"
  end

  def on_message(delivery, message)
    puts "RECEIVE: Received message '#{message.body}'\n"

    @received += 1

    if @received == @desired
      delivery.receiver.close
      delivery.receiver.connection.close
    end
  end
end

if ARGV.length > 2
  conn_url, address = ARGV[0..1]
else
  abort "Usage: receive.rb CONNECTION-URL ADDRESS [MESSAGE-COUNT]\n"
end

begin
  desired = Integer(ARGV[2])
rescue ArgumentError
  desired = 0
end

handler = ReceiveHandler.new(conn_url, address, desired)
container = Qpid::Proton::Container.new(handler)
container.run
Copy to Clipboard Toggle word wrap

Running the Example

To run the example program, copy it to a local file and invoke it using the ruby command.

$ ruby receive.rb amqp://localhost queue1
Copy to Clipboard Toggle word wrap
Torna in cima
Red Hat logoGithubredditYoutubeTwitter

Formazione

Prova, acquista e vendi

Community

Informazioni sulla documentazione di Red Hat

Aiutiamo gli utenti Red Hat a innovarsi e raggiungere i propri obiettivi con i nostri prodotti e servizi grazie a contenuti di cui possono fidarsi. Esplora i nostri ultimi aggiornamenti.

Rendiamo l’open source più inclusivo

Red Hat si impegna a sostituire il linguaggio problematico nel codice, nella documentazione e nelle proprietà web. Per maggiori dettagli, visita il Blog di Red Hat.

Informazioni su Red Hat

Forniamo soluzioni consolidate che rendono più semplice per le aziende lavorare su piattaforme e ambienti diversi, dal datacenter centrale all'edge della rete.

Theme

© 2025 Red Hat