forked from kubemq-io/kubemq-sources
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
99 lines (91 loc) · 2.16 KB
/
main.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"context"
"io/ioutil"
"log"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/kubemq-io/kubemq-go"
"github.com/nats-io/nuid"
)
func main() {
dat, err := ioutil.ReadFile("./credentials/aws/awsKey.txt")
if err != nil {
log.Fatal(err)
}
awsKey := string(dat)
dat, err = ioutil.ReadFile("./credentials/aws/awsSecretKey.txt")
if err != nil {
log.Fatal(err)
}
awsSecretKey := string(dat)
dat, err = ioutil.ReadFile("./credentials/aws/region.txt")
if err != nil {
log.Fatal(err)
}
region := string(dat)
dat, err = ioutil.ReadFile("./credentials/aws/sqs/queue.txt")
if err != nil {
log.Fatal(err)
}
queue := string(dat)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
client, err := kubemq.NewClient(context.Background(),
kubemq.WithAddress("localhost", 50000),
kubemq.WithClientId(nuid.Next()),
kubemq.WithCheckConnection(true),
kubemq.WithTransportType(kubemq.TransportTypeGRPC))
if err != nil {
log.Fatal(err)
}
go func() {
errCh := make(chan error)
eventsCh, err := client.SubscribeToEvents(ctx, "event.aws.sqs", "", errCh)
if err != nil {
log.Fatal(err)
}
for {
select {
case err := <-errCh:
log.Fatal(err)
return
case event, more := <-eventsCh:
if !more {
log.Println("client done")
return
}
log.Printf("client:%s Channel: %s, Data: %s", event.ClientId, event.Channel, string(event.Body))
case <-ctx.Done():
return
}
}
}()
sess, err := session.NewSession(&aws.Config{
Region: aws.String(region),
Credentials: credentials.NewStaticCredentials(awsKey, awsSecretKey, ""),
})
if err != nil {
log.Fatal(err)
}
svc := sqs.New(sess)
m := &sqs.SendMessageInput{}
m.SetQueueUrl(queue)
m.SetMessageBody("my test body!")
_, err = svc.SendMessage(m)
if err != nil {
log.Fatal(err)
}
time.Sleep(10 * time.Second)
m2 := &sqs.SendMessageInput{}
m2.SetQueueUrl(queue)
m2.SetMessageBody("my second body!")
_, err = svc.SendMessage(m2)
if err != nil {
log.Fatal(err)
}
time.Sleep(10 * time.Second)
}