Purpose
- Call api to AWS Simple Queue Service(SQS) via sdk
Sample Code
Prepare
- install aws sdk, run:
1 2 | go get -u github.com/aws/aws-sdk-go/ |
- configure access-key and secret-key.
Create a session
- create a session for reuse in other functions
- profile used is default , to know profile is being configured, run on linux/mac
1 2 | cat ~/.aws/credentials |
- function to create a session
1 2 3 4 5 6 7 8 9 10 11 12 13 | func GetSession() *session.Session { sess, err := session.NewSessionWithOptions(session.Options{ Profile: "default", Config: aws.Config{ Region: aws.String("us-west-1"), }, }) if err != nil { panic(err) } return sess } |
How to create a queue
- use
CreateQueue
function inaws-sdk
to create a queue for testing. - There are some important parameters to note:
queueName
: the name of the queue you want to createDelaySeconds
: how long the message you want to keep before sending,VisibilityTimeout
: the time a message takes before it expires.
- function create 1 queue
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | func CreateQueue(sess *session.Session, queueName string) (*sqs.CreateQueueOutput, error) { sqsClient := sqs.New(sess) result, err := sqsClient.CreateQueue(&sqs.CreateQueueInput{ QueueName: &queueName, Attributes: map[string]*string{ "DelaySeconds": aws.String("0"), "VisibilityTimeout": aws.String("60"), }, }) if err != nil { return nil, err } return result, nil } |
Get the URL of a queue
- All apis require the url of the queue, so use
GetQueueURL
to query the url of the queue-name - function get url
1 2 3 4 5 6 7 8 9 10 11 | func GetQueueURL(sess *session.Session, queue string) (*sqs.GetQueueUrlOutput, error) { sqsClient := sqs.New(sess) result, err := sqsClient.GetQueueUrl(&sqs.GetQueueUrlInput{ QueueName: &queue, }) if err != nil { return nil, err } return result, nil } |
Send message to queue
- we will use
SendMessage
function fromaws-sdk
to send message to queue - Some important parameters to pay attention to while sending messages:
QueueUrl
: the url of the queue that wants to send the message.- MessageBody: body sent to the queue, can be json-string or string
- function send message
1 2 3 4 5 6 7 8 9 10 11 12 | func SendMessage(sess *session.Session, queueUrl string, messageBody string) (*sqs.SendMessageOutput, error) { sqsClient := sqs.New(sess) sendOut, err := sqsClient.SendMessage(&sqs.SendMessageInput{ QueueUrl: &queueUrl, MessageBody: aws.String(messageBody), }) if err != nil { return nil, err } return sendOut, nil } |
Receive messages from queue
- we will use
ReceiveMessage
function fromaws-sdk
to get message from queue - Some important parameters need to be noticed while receiving messages:
QueueUrl
: the url of the queue that wants to receive the message.MaxNumberOfMessages
: total number of messages that can be received.
- function receive message:
1 2 3 4 5 6 7 8 9 10 11 12 | func GetMessages(sess *session.Session, queueUrl string, maxMessages int) (*sqs.ReceiveMessageOutput, error) { sqsClient := sqs.New(sess) msgResult, err := sqsClient.ReceiveMessage(&sqs.ReceiveMessageInput{ QueueUrl: &queueUrl, MaxNumberOfMessages: aws.Int64(int64(maxMessages)), }) if err != nil { return nil, err } return msgResult, nil } |
Delete messages in the queue
- When receiving a message from the queue, the message is not automatically removed from the queue.
- Other consumers can receive the message after the
VisibilityTimeout
has expired. - To ensure that there are no duplicate messages, it needs to be deleted.
- we will use
DeleteMessage
function fromaws-sdk
to delete message from queue - need to provide
ReceiptHandle
in method component - function to delete messages:
1 2 3 4 5 6 7 8 9 | func DeleteMessage(sess *session.Session, queueUrl string, messageHandle *string) error { sqsClient := sqs.New(sess) _, err := sqsClient.DeleteMessage(&sqs.DeleteMessageInput{ QueueUrl: &queueUrl, ReceiptHandle: messageHandle, }) return err } |
Delete all messages in the queue
- we will use
PurgeQueue
function fromaws-sdk
to delete all messages in queue - Some important paramters need to be noticed while deleting all messages:
- function to delete all messages:
1 2 3 4 5 6 7 8 | func PurgeQueue(sess *session.Session, queueUrl string) error { sqsClient := sqs.New(sess) _, err := sqsClient.PurgeQueue(&sqs.PurgeQueueInput{ QueueUrl: aws.String(queueUrl), }) return err } |