AWS Code Sample
Catalog

sqs-ruby-example-send-receive-messages.rb

Sends a message to, receives a message from, and deletes a message from an SQS queue.

# Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. # # This file is licensed under the Apache License, Version 2.0 (the "License"). # You may not use this file except in compliance with the License. A copy of the # License is located at # # http://aws.amazon.com/apache2.0/ # # This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS # OF ANY KIND, either express or implied. See the License for the specific # language governing permissions and limitations under the License. # Demonstrates how to: # 1. Send a message to a queue. # 2. Receive the message in the queue. # 3. Display information about the message. # 4. Delete the message from the queue. require 'aws-sdk-sqs' # v2: require 'aws-sdk' sqs = Aws::SQS::Client.new(region: 'us-east-1') # Send a message to a queue. queue_name = "my-queue" begin queue_url = sqs.get_queue_url(queue_name: queue_name).queue_url # Create a message with three custom attributes: Title, Author, and WeeksOn. send_message_result = sqs.send_message({ queue_url: queue_url, message_body: "Information about current NY Times fiction bestseller for week of 2016-12-11.", message_attributes: { "Title" => { string_value: "The Whistler", data_type: "String" }, "Author" => { string_value: "John Grisham", data_type: "String" }, "WeeksOn" => { string_value: "6", data_type: "Number" } } }) rescue Aws::SQS::Errors::NonExistentQueue puts "A queue named '#{queue_name}' does not exist." exit(false) end puts send_message_result.message_id # Receive the message in the queue. receive_message_result = sqs.receive_message({ queue_url: queue_url, message_attribute_names: ["All"], # Receive all custom attributes. max_number_of_messages: 1, # Receive at most one message. wait_time_seconds: 0 # Do not wait to check for the message. }) # Display information about the message. # Display the message's body and each custom attribute value. receive_message_result.messages.each do |message| puts message.body puts "Title: #{message.message_attributes["Title"]["string_value"]}" puts "Author: #{message.message_attributes["Author"]["string_value"]}" puts "WeeksOn: #{message.message_attributes["WeeksOn"]["string_value"]}" # Delete the message from the queue. sqs.delete_message({ queue_url: queue_url, receipt_handle: message.receipt_handle }) end

Sample Details

Service: sqs

Last tested: 2018-03-16

Author: Doug-AWS

Type: full-example

On this page: