|
| 1 | +// RabbitMQ AMQP 1.0 Go Client: https://github.com/rabbitmq/rabbitmq-amqp-go-client |
| 2 | +// RabbitMQ AMQP 1.0 documentation: https://www.rabbitmq.com/docs/amqp |
| 3 | +// This example is demonstrating how to consume messages with pre-settled mode enabled. |
| 4 | +// pre-settled is valid only for classic and quorum queues. |
| 5 | +// Pre-settled mode means that messages are considered accepted by the broker as soon as they are delivered to the consumer, |
| 6 | +// without requiring explicit acceptance from the consumer side. |
| 7 | +// This is useful for scenarios where at-most-once delivery semantics are acceptable, and it can improve performance by reducing |
| 8 | +// the overhead of message acknowledgments. |
| 9 | +// Example path:https://github.com/rabbitmq/rabbitmq-amqp-go-client/tree/main/docs/examples/pre_settled/pre_settled.go |
| 10 | +package main |
| 11 | + |
| 12 | +import ( |
| 13 | + "context" |
| 14 | + "fmt" |
| 15 | + |
| 16 | + rmq "github.com/rabbitmq/rabbitmq-amqp-go-client/pkg/rabbitmqamqp" |
| 17 | +) |
| 18 | + |
| 19 | +func main() { |
| 20 | + rmq.Info("Pre-Settled example with AMQP Go AMQP 1.0 Client") |
| 21 | + |
| 22 | + env := rmq.NewEnvironment("amqp://guest:guest@localhost:5672/", nil) |
| 23 | + amqpConnection, err := env.NewConnection(context.Background()) |
| 24 | + if err != nil { |
| 25 | + rmq.Error("Error opening connection", err) |
| 26 | + return |
| 27 | + } |
| 28 | + // create queue for the example |
| 29 | + _, err = amqpConnection.Management().DeclareQueue(context.TODO(), &rmq.QuorumQueueSpecification{ |
| 30 | + Name: "pre-settled-queue", |
| 31 | + }) |
| 32 | + if err != nil { |
| 33 | + rmq.Error("Error declaring queue", err) |
| 34 | + return |
| 35 | + } |
| 36 | + rmq.Info("Queue 'pre-settled-queue' declared") |
| 37 | + |
| 38 | + // publish some messages to the queue |
| 39 | + producer, err := amqpConnection.NewPublisher(context.TODO(), &rmq.QueueAddress{ |
| 40 | + Queue: "pre-settled-queue", |
| 41 | + }, nil) |
| 42 | + if err != nil { |
| 43 | + rmq.Error("Error creating producer", err) |
| 44 | + return |
| 45 | + } |
| 46 | + for i := 0; i < 100; i++ { |
| 47 | + msg := rmq.NewMessage([]byte(fmt.Sprintf("Pre-settled message number %d ", i+1))) |
| 48 | + _, err := producer.Publish(context.TODO(), msg) |
| 49 | + if err != nil { |
| 50 | + rmq.Error("Error publishing message", err) |
| 51 | + return |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // create consumer with pre-settled mode enabled |
| 56 | + |
| 57 | + consumer, err := amqpConnection.NewConsumer(context.TODO(), "pre-settled-queue", &rmq.ConsumerOptions{ |
| 58 | + PreSettled: true, |
| 59 | + }) |
| 60 | + if err != nil { |
| 61 | + rmq.Error("Error creating consumer", err) |
| 62 | + return |
| 63 | + } |
| 64 | + rmq.Info("Consumer created with pre-settled mode enabled") |
| 65 | + |
| 66 | + for i := 0; i < 100; i++ { |
| 67 | + dc, err := consumer.Receive(context.TODO()) |
| 68 | + if err != nil { |
| 69 | + rmq.Error("Error consuming message", err) |
| 70 | + return |
| 71 | + } |
| 72 | + // here we don't need to accept the message |
| 73 | + // because pre-settled mode is enabled |
| 74 | + rmq.Info("[Consumer]", "Message received", string(dc.Message().GetData())) |
| 75 | + } |
| 76 | + |
| 77 | + // clean up |
| 78 | + err = consumer.Close(context.TODO()) |
| 79 | + if err != nil { |
| 80 | + rmq.Error("Error closing consumer", err) |
| 81 | + return |
| 82 | + } |
| 83 | + err = producer.Close(context.TODO()) |
| 84 | + if err != nil { |
| 85 | + rmq.Error("Error closing producer", err) |
| 86 | + return |
| 87 | + } |
| 88 | + // delete the queue |
| 89 | + err = amqpConnection.Management().DeleteQueue(context.TODO(), "pre-settled-queue") |
| 90 | + if err != nil { |
| 91 | + rmq.Error("Error deleting queue", err) |
| 92 | + return |
| 93 | + } |
| 94 | + rmq.Info("Queue 'pre-settled-queue' deleted") |
| 95 | + |
| 96 | + err = amqpConnection.Close(context.TODO()) |
| 97 | + if err != nil { |
| 98 | + rmq.Error("Error closing connection", err) |
| 99 | + return |
| 100 | + } |
| 101 | + rmq.Info("Example finished successfully") |
| 102 | + |
| 103 | +} |
0 commit comments