forked from remind101/mq-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
42 lines (33 loc) · 1014 Bytes
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package mq_test
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/sqs"
mq "github.com/remind101/mq-go"
)
func Example() {
queueURL := "https://sqs.us-east-2.amazonaws.com/123456789012/MyQueue"
h := mq.HandlerFunc(func(m *mq.Message) error {
fmt.Printf("Received message: %s", aws.StringValue(m.SQSMessage.Body))
// Returning no error signifies the message was processed successfully.
// The Server will queue the message for deletion.
return nil
})
// Configure mq.Server
s := mq.NewServer(queueURL, h)
// Start a loop to receive SQS messages and pass them to the Handler.
s.Start()
defer s.Shutdown(context.Background())
// Start a publisher
p := mq.NewPublisher(queueURL)
p.Start()
defer p.Shutdown(context.Background())
// Publish messages (will be batched).
p.Publish(&sqs.SendMessageBatchRequestEntry{
MessageBody: aws.String("Hello"),
})
p.Publish(&sqs.SendMessageBatchRequestEntry{
MessageBody: aws.String("World!"),
})
}