Sending and Receiving Messages in Amazon SQS
After you create a queue in Amazon SQS, you can send a message to it and then consume it. To learn more, see Tutorial: Sending a Message to an Amazon SQS Queue and Tutorial: Receiving and Deleting a Message from an Amazon SQS Queue.
In this example, you use the AWS SDK for Ruby with Amazon SQS to:
-
Send a message to a queue by using Aws::SQS::Client#send_message.
Note
If your queue is a FIFO queue, you must include a
message_group_id
parameter in addition to the
id
and message_body
parameters.
-
Receive the message in the queue by using Aws::SQS::Client#receive_message.
-
Display information about the message.
-
Delete the message from the queue by using Aws::SQS::Client#delete_message.
Prerequisites
Before running the example code, you need to install and configure the AWS SDK for Ruby, as described in:
You also need to create the queue my-queue, which you can do in the Amazon SQS console.
Example
# Copyright 2010-2019 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