Skip to content

Commit

Permalink
add support for adding handler on existing consumer (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmorelli92 authored May 7, 2024
1 parent cfb230c commit 7824335
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
6 changes: 6 additions & 0 deletions consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ func (c *Connection) NewConsumer(
}
}

// AddHandlerToConsumer adds a handler for the given routing key.
// It is another way to add handlers when the consumer is already created and cannot use the options.
func AddHandlerToConsumer[T any](consumer *Consumer, routingKey string, handler EventHandler[T]) {
consumer.options.handlers[routingKey] = newWrappedHandler(handler)
}

// Consume will start consuming events for the indicated queue.
// The first time this function is called it will return error if
// handlers or default handler are not specified and also if queues, exchanges,
Expand Down
5 changes: 3 additions & 2 deletions tests/consumer_publish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ func TestConsumerPublisher(t *testing.T) {
consumer := connection.NewConsumer(
queueName,
bunnify.WithQuorumQueue(),
bunnify.WithBindingToExchange(exchangeName),
bunnify.WithHandler(routingKey, eventHandler))
bunnify.WithBindingToExchange(exchangeName))

bunnify.AddHandlerToConsumer(&consumer, routingKey, eventHandler)

if err := consumer.Consume(); err != nil {
t.Fatal(err)
Expand Down

0 comments on commit 7824335

Please sign in to comment.