From 70a7350572caececfb6b3d37fad84a5b3e4863da Mon Sep 17 00:00:00 2001 From: Christoph Haas Date: Tue, 5 Jul 2022 23:41:41 +0200 Subject: [PATCH] queue, exchange and binding declaration is now simplified and much clearer to use --- README.md | 40 ++++++++++++++++++++++++++++++++-------- declare.go | 2 +- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 738957b..be993de 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Supported by [Boot.dev](https://boot.dev) [Streadway's AMQP](https://github.com/rabbitmq/amqp091-go) library is currently the most robust and well-supported Go client I'm aware of. It's a fantastic option and I recommend starting there and seeing if it fulfills your needs. Their project has made an effort to stay within the scope of the AMQP protocol, as such, no reconnection logic and few ease-of-use abstractions are provided. -### Goal +### Goal The goal with `go-rabbitmq` is to still provide most all of the nitty-gritty functionality of AMQP, but to make it easier to work with via a higher-level API. Particularly: @@ -48,7 +48,6 @@ err = consumer.StartConsuming( return rabbitmq.Ack }, "my_queue", - []string{"routing_key1", "routing_key2"} ) if err != nil { log.Fatal(err) @@ -74,13 +73,7 @@ err = consumer.StartConsuming( return rabbitmq.Ack }, "my_queue", - []string{"routing_key", "routing_key_2"}, rabbitmq.WithConsumeOptionsConcurrency(10), - rabbitmq.WithConsumeOptionsQueueDurable, - rabbitmq.WithConsumeOptionsQuorum, - rabbitmq.WithConsumeOptionsBindingExchangeName("events"), - rabbitmq.WithConsumeOptionsBindingExchangeKind("topic"), - rabbitmq.WithConsumeOptionsBindingExchangeDurable, rabbitmq.WithConsumeOptionsConsumerName(consumerName), ) if err != nil { @@ -137,6 +130,37 @@ go func() { }() ``` +## 🚀 Quick Start Queue, Exchange and Binding Declaration + +### Consumer + +```go +consumer, err := rabbitmq.NewConsumer("amqp://user:pass@localhost", rabbitmq.Config{}) +if err != nil { + log.Fatal(err) +} +defer consumer.Close() +err = consumer.StartConsuming( + func(d rabbitmq.Delivery) rabbitmq.Action { + log.Printf("consumed: %v", string(d.Body)) + // rabbitmq.Ack, rabbitmq.NackDiscard, rabbitmq.NackRequeue + return rabbitmq.Ack + }, + "my_queue", + rabbitmq.WithConsumeDeclareOptions( + rabbitmq.WithDeclareQueueDurable, + rabbitmq.WithDeclareQueueQuorum, + rabbitmq.WithDeclareExchangeName("events"), + rabbitmq.WithDeclareExchangeKind("topic"), + rabbitmq.WithDeclareExchangeDurable, + rabbitmq.WithDeclareBindingsForRoutingKeys([]string{"routing_key", "routing_key_2"}), + ), + ) +if err != nil { + log.Fatal(err) +} +``` + ## Other usage examples See the [examples](examples) directory for more ideas. diff --git a/declare.go b/declare.go index 35f2621..84d7fe4 100644 --- a/declare.go +++ b/declare.go @@ -354,7 +354,7 @@ func WithDeclareExchangeNoDeclare(options *DeclareOptions) { // WithDeclareBindingNoWait sets the bindings to nowait, which means if the queue can not be bound // the channel will not be closed with an error. // This function must be called after bindings have been defined, otherwise it has no effect. -func WithDeclareBindingNoWait(options *ConsumeOptions) { +func WithDeclareBindingNoWait(options *DeclareOptions) { for i := range options.Bindings { options.Bindings[i].NoWait = true }