Browse Source

declare exchange before binding

pull/8/head
Miguel Bautista 5 years ago
parent
commit
04b8c6a9e8
3 changed files with 41 additions and 7 deletions
  1. +1
    -0
      .gitignore
  2. +39
    -6
      consume.go
  3. +1
    -1
      examples/consumer/main.go

+ 1
- 0
.gitignore View File

@ -0,0 +1 @@
.idea/

+ 39
- 6
consume.go View File

@ -71,7 +71,7 @@ func getDefaultConsumeOptions() ConsumeOptions {
QueueExclusive: false,
QueueNoWait: false,
QueueArgs: nil,
BindingExchange: "",
BindingExchange: nil,
BindingNoWait: false,
BindingArgs: nil,
Concurrency: 1,
@ -93,7 +93,7 @@ type ConsumeOptions struct {
QueueExclusive bool
QueueNoWait bool
QueueArgs Table
BindingExchange string
BindingExchange *BindingExchangeOptions
BindingNoWait bool
BindingArgs Table
Concurrency int
@ -107,6 +107,18 @@ type ConsumeOptions struct {
ConsumerArgs Table
}
// BindingExchangeOptions are used when binding to an exchange.
// it will verify the exchange is created before binding to it.
type BindingExchangeOptions struct {
Name string
Type string
Durable bool
AutoDelete bool
Internal bool
NoWait bool
ExchangeArgs Table
}
// WithConsumeOptionsQueueDurable sets the queue to durable, which means it won't
// be destroyed when the server restarts. It must only be bound to durable exchanges
func WithConsumeOptionsQueueDurable(options *ConsumeOptions) {
@ -146,9 +158,17 @@ func WithConsumeOptionsQuorum(options *ConsumeOptions) {
}
// WithConsumeOptionsBindingExchange returns a function that sets the exchange the queue will be bound to
func WithConsumeOptionsBindingExchange(exchange string) func(*ConsumeOptions) {
func WithConsumeOptionsBindingExchange(name, kind string, durable, autoDelete, internal, noWait bool, args Table) func(*ConsumeOptions) {
return func(options *ConsumeOptions) {
options.BindingExchange = exchange
options.BindingExchange = &BindingExchangeOptions{
Name: name,
Type: kind,
Durable: durable,
AutoDelete: autoDelete,
Internal: internal,
NoWait: noWait,
ExchangeArgs: args,
}
}
}
@ -300,12 +320,25 @@ func (consumer Consumer) startGoroutines(
return err
}
if consumeOptions.BindingExchange != "" {
if consumeOptions.BindingExchange != nil {
exchange := consumeOptions.BindingExchange
err = consumer.chManager.channel.ExchangeDeclare(
exchange.Name,
exchange.Type,
exchange.Durable,
exchange.AutoDelete,
exchange.Internal,
exchange.NoWait,
tableToAMQPTable(exchange.ExchangeArgs),
)
if err != nil {
return err
}
for _, routingKey := range routingKeys {
err = consumer.chManager.channel.QueueBind(
queue,
routingKey,
consumeOptions.BindingExchange,
exchange.Name,
consumeOptions.BindingNoWait,
tableToAMQPTable(consumeOptions.BindingArgs),
)


+ 1
- 1
examples/consumer/main.go View File

@ -25,7 +25,7 @@ func main() {
rabbitmq.WithConsumeOptionsConcurrency(10),
rabbitmq.WithConsumeOptionsQueueDurable,
rabbitmq.WithConsumeOptionsQuorum,
rabbitmq.WithConsumeOptionsBindingExchange("events"),
rabbitmq.WithConsumeOptionsBindingExchange("events", "topic", true, false, false, true, nil),
)
if err != nil {
log.Fatal(err)


Loading…
Cancel
Save