-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
114 lines (101 loc) · 2.79 KB
/
Copy pathmain.go
File metadata and controls
114 lines (101 loc) · 2.79 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/streamlinelabs/streamline-go-sdk/streamline"
)
func main() {
// Create client with default configuration
config := streamline.DefaultConfig()
brokers := os.Getenv("STREAMLINE_BOOTSTRAP_SERVERS")
if brokers == "" {
brokers = "localhost:9092"
}
config.Brokers = []string{brokers}
client, err := streamline.NewClient(config)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
defer client.Close()
ctx := context.Background()
// Create a topic
fmt.Println("Creating topic...")
err = client.Admin.CreateTopic(ctx, streamline.TopicConfig{
Name: "example-topic",
NumPartitions: 3,
ReplicationFactor: 1,
})
if err != nil {
log.Printf("Warning: failed to create topic (may already exist): %v", err)
}
// Produce messages
fmt.Println("Producing messages...")
for i := 0; i < 10; i++ {
result, err := client.Producer.Send(ctx, "example-topic",
[]byte(fmt.Sprintf("key-%d", i)),
[]byte(fmt.Sprintf("Hello, Streamline! Message %d", i)),
)
if err != nil {
log.Printf("Failed to send message: %v", err)
continue
}
fmt.Printf("Produced message to partition %d at offset %d\n",
result.Partition, result.Offset)
}
// Produce with headers
result, err := client.Producer.SendMessage(ctx, &streamline.Message{
Topic: "example-topic",
Key: []byte("with-headers"),
Value: []byte("Message with headers"),
Headers: map[string][]byte{
"trace-id": []byte("abc123"),
"content-type": []byte("application/json"),
},
})
if err != nil {
log.Printf("Failed to send message with headers: %v", err)
} else {
fmt.Printf("Produced message with headers to partition %d at offset %d\n",
result.Partition, result.Offset)
}
// Create consumer
fmt.Println("Starting consumer...")
consumer, err := client.NewConsumer(ctx, "example-group", []string{"example-topic"})
if err != nil {
log.Fatalf("Failed to create consumer: %v", err)
}
defer consumer.Close()
// Set up signal handling
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
// Start consuming
messages, errors := consumer.Start(ctx)
fmt.Println("Consuming messages (press Ctrl+C to stop)...")
timeout := time.After(10 * time.Second)
for {
select {
case msg, ok := <-messages:
if !ok {
return
}
fmt.Printf("Received: topic=%s partition=%d offset=%d key=%s value=%s\n",
msg.Topic, msg.Partition, msg.Offset, string(msg.Key), string(msg.Value))
if len(msg.Headers) > 0 {
fmt.Printf(" Headers: %v\n", msg.Headers)
}
case err := <-errors:
log.Printf("Consumer error: %v", err)
case <-sigChan:
fmt.Println("\nShutting down...")
return
case <-timeout:
fmt.Println("\nTimeout reached")
return
}
}
}