-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.go
49 lines (42 loc) · 1.14 KB
/
example.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
package example
import (
"fmt"
"strconv"
"time"
"github.com/go-redis/redis"
"github.com/semihbkgr/go-rsmq"
)
func exampleProducerConsumer() {
ns := "example"
qname := "queue"
prodTicker := time.NewTicker(time.Second * 3)
prodClient := redis.NewClient(&redis.Options{Addr: "localhost:6379"})
prodRsmq := rsmq.NewRedisSMQ(prodClient, ns)
err := prodRsmq.CreateQueue(qname, rsmq.UnsetVt, rsmq.UnsetDelay, rsmq.UnsetMaxsize)
if err != nil {
fmt.Println(err.Error())
}
consTicker := time.NewTicker(time.Second * 1)
consClient := redis.NewClient(&redis.Options{Addr: "localhost:6379"})
consRsmq := rsmq.NewRedisSMQ(consClient, ns)
for {
select {
case <-prodTicker.C:
id, err := prodRsmq.SendMessage(qname, strconv.FormatInt(time.Now().UnixNano(), 10), rsmq.UnsetVt)
if err != nil {
fmt.Println(err)
} else {
fmt.Printf("message sent, id: %s\n", id)
}
case <-consTicker.C:
msg, err := consRsmq.PopMessage(qname)
if err != nil {
fmt.Println(err)
} else if msg != nil {
fmt.Printf("message received, id: %s, message: %s\n", msg.ID, msg.Message)
} else {
fmt.Println("queue is empty")
}
}
}
}