SIGN IN SIGN UP

Welcome to the AWS Code Examples Repository. This repo contains code examples used in the AWS documentation, AWS SDK Developer Guides, and more. For more information, see the Readme.md file below.

0 0 1 Java
2022-10-11 14:21:14 -06:00
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
# Purpose
# This code example demonstrates how to send a message to a queue in Amazon Simple Queue Service (Amazon SQS).
# snippet-start:[ruby.example_code.sqs.SendMessage]
require 'aws-sdk-sqs'
require 'aws-sdk-sts'
2022-10-11 14:21:14 -06:00
# @param sqs_client [Aws::SQS::Client] An initialized Amazon SQS client.
# @param queue_url [String] The URL of the queue.
# @param message_body [String] The contents of the message to be sent.
# @return [Boolean] true if the message was sent; otherwise, false.
# @example
# exit 1 unless message_sent?(
# Aws::SQS::Client.new(region: 'us-west-2'),
# 'https://sqs.us-west-2.amazonaws.com/111111111111/my-queue',
# 'This is my message.'
# )
def message_sent?(sqs_client, queue_url, message_body)
sqs_client.send_message(
queue_url: queue_url,
message_body: message_body
)
true
rescue StandardError => e
puts "Error sending message: #{e.message}"
false
end
# Full example call:
# Replace us-west-2 with the AWS Region you're using for Amazon SQS.
def run_me
region = 'us-west-2'
queue_name = 'my-queue'
message_body = 'This is my message.'
2022-10-11 14:21:14 -06:00
sts_client = Aws::STS::Client.new(region: region)
# For example:
# 'https://sqs.us-west-2.amazonaws.com/111111111111/my-queue'
queue_url = "https://sqs.#{region}.amazonaws.com/#{sts_client.get_caller_identity.account}/#{queue_name}"
2022-10-11 14:21:14 -06:00
sqs_client = Aws::SQS::Client.new(region: region)
puts "Sending a message to the queue named '#{queue_name}'..."
if message_sent?(sqs_client, queue_url, message_body)
puts 'Message sent.'
2022-10-11 14:21:14 -06:00
else
puts 'Message not sent.'
2022-10-11 14:21:14 -06:00
end
end
# Example usage:
2022-10-11 14:21:14 -06:00
run_me if $PROGRAM_NAME == __FILE__
# snippet-end:[ruby.example_code.sqs.SendMessage]