Menu
AWS SDK for Go
Developer Guide

Setting Attributes on an Amazon SQS Queue

This Go example shows you how to set attributes on an existing Amazon SQS queue.

You can download complete versions of these example files from the aws-doc-sdk-examples repository on GitHub.

Scenario

This example updates an existing Amazon SQS queue to use long polling.

Long polling reduces the number of empty responses by allowing Amazon SQS to wait a specified time for a message to become available in the queue before sending a response. Also, long polling eliminates false empty responses by querying all of the servers instead of a sampling of servers. To enable long polling, you must specify a non-zero wait time for received messages. You can do this by setting the ReceiveMessageWaitTimeSeconds parameter of a queue or by setting the WaitTimeSeconds parameter on a message when it is received.

The code uses these methods of the Amazon SQS client class:

If you are unfamiliar with using Amazon SQS long polling, you should read Long Polling in the Amazon SQS Developer Guide before proceeding.

Prerequisites

  • You have set up and configured the AWS SDK for Go.

  • You are familiar with using Amazon SQS long polling. To learn more, see Long Polling in the Amazon SQS Developer Guide.

Set Attributes on Queue

Create a new Go file named sqs_longpolling_existing_queue.go.

You must import the relevant Go and AWS SDK for Go packages by adding the following lines.

Copy
package main import ( "flag" "fmt" "os" "strconv" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/sqs" )

Get the queue name and timeout passed in by the user.

Copy
func main() { var name string var timeout int flag.StringVar(&name, "n", "", "Queue name") flag.IntVar(&timeout, "t", 20, "(Optional) Timeout in seconds for long polling") flag.Parse() if len(name) == 0 { flag.PrintDefaults() exitErrorf("Queue name required") }

Initialize a session that the SDK will use to load credentials from the shared credentials file, ~/.aws/credentials.

Copy
sess, err := session.NewSession(&aws.Config{ Region: aws.String("us-west-2")}, ) // Create a SQS service client. svc := sqs.New(sess)

Get the queue. You need to convert the queue name into a URL. You can use the GetQueueUrl API call to retrieve the URL. This is needed for setting attributes on the queue. Print any errors.

Copy
resultURL, err := svc.GetQueueUrl(&sqs.GetQueueUrlInput{ QueueName: aws.String(name), }) if err != nil { if aerr, ok := err.(awserr.Error); ok && aerr.Code() == sqs.ErrCodeQueueDoesNotExist { exitErrorf("Unable to find queue %q.", name) } exitErrorf("Unable to get queue %q, %v.", name, err) }

Update the queue to enable long polling.

Copy
_, err = svc.SetQueueAttributes(&sqs.SetQueueAttributesInput{ QueueUrl: resultURL.QueueUrl, Attributes: aws.StringMap(map[string]string{ "ReceiveMessageWaitTimeSeconds": strconv.Itoa(timeout), }), }) if err != nil { exitErrorf("Unable to update queue %q, %v.", name, err) } fmt.Printf("Succesffuly updated queue %q.\n", name) }

The example uses this utility function.

Copy
func exitErrorf(msg string, args ...interface{}) { fmt.Fprintf(os.Stderr, msg+"\n", args...) os.Exit(1) }