-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
85 lines (73 loc) · 2.27 KB
/
Copy pathmain.go
File metadata and controls
85 lines (73 loc) · 2.27 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
package main
import (
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/confluentinc/confluent-kafka-go/kafka"
)
// main demonstrates Kafka transactions to achieve exactly-once semantics.
func main() {
// Kafka broker and topic configuration
broker := "localhost:9092"
topic := "example-topic"
// Create a transactional producer
producer, err := kafka.NewProducer(&kafka.ConfigMap{
"bootstrap.servers": broker,
"transactional.id": "transactional-id-example",
"enable.idempotence": true, // Ensures exactly-once delivery
"message.send.max.retries": 3,
"acks": "all",
})
if err != nil {
log.Fatalf("Failed to create producer: %v", err)
}
defer producer.Close()
// Initialize transactions
err = producer.InitTransactions(nil)
if err != nil {
log.Fatalf("Failed to initialize transactions: %v", err)
}
// Handle graceful shutdown on signals
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-sigChan
log.Printf("Caught signal %v: terminating", sig)
producer.Close()
os.Exit(0)
}()
// Begin a transaction
log.Println("Beginning transaction...")
err = producer.BeginTransaction()
if err != nil {
log.Fatalf("Failed to begin transaction: %v", err)
}
// Create a test message
message := &kafka.Message{
TopicPartition: kafka.TopicPartition{Topic: &topic, Partition: kafka.PartitionAny},
Key: []byte("key-1"),
Value: []byte("Hello, Kafka! This message is part of a transactional batch."),
Timestamp: time.Now(),
}
// Produce the message within the transaction
log.Println("Producing transactional message...")
err = producer.Produce(message, nil)
if err != nil {
log.Printf("Error occurred while producing message: %v", err)
log.Println("Aborting transaction...")
err = producer.AbortTransaction(nil)
if err != nil {
log.Fatalf("Failed to abort transaction: %v", err)
}
log.Fatalf("Transaction aborted due to error: %v", err)
}
// Commit the transaction
log.Println("Committing transaction...")
err = producer.CommitTransaction(nil)
if err != nil {
log.Fatalf("Failed to commit transaction: %v", err)
}
log.Println("Transaction committed successfully, ensuring exactly-once message delivery.")
}