-
Notifications
You must be signed in to change notification settings - Fork 5
/
example.go
73 lines (56 loc) · 1.11 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package freezer
import (
"context"
"encoding/json"
"fmt"
"log"
"time"
"github.com/uw-labs/straw"
)
func main() {
produce()
ss, err := straw.Open("file:///")
if err != nil {
log.Fatal(err)
}
cons := NewMessageSource(ss, MessageSourceConfig{Path: "/tmp/"})
// consume messages for 2 seconds
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
handler := func(m []byte) error {
fmt.Printf("message is: %s\n", m)
return nil
}
if err := cons.ConsumeMessages(ctx, handler); err != nil {
log.Fatal(err)
}
}
type MyMessage struct {
CustomerID string
Message string
}
func (m MyMessage) Marshal() ([]byte, error) {
return json.Marshal(m)
}
func produce() {
ss, err := straw.Open("file:///")
if err != nil {
log.Fatal(err)
}
sink, err := NewMessageAutoFlushSink(
ss,
MessageSinkAutoFlushConfig{Path: "/tmp/"},
)
if err != nil {
log.Fatal(err)
}
m, err := MyMessage{
CustomerID: "customer-01",
Message: fmt.Sprintf("hello. it is currently %v", time.Now()),
}.Marshal()
if err != nil {
panic(err)
}
sink.PutMessage(m)
sink.Close()
}