From 782433508f1686382356712c528b84f70388fba6 Mon Sep 17 00:00:00 2001 From: Pablo Morelli Date: Tue, 7 May 2024 16:58:13 +0200 Subject: [PATCH] add support for adding handler on existing consumer (#35) --- consumer.go | 6 ++++++ tests/consumer_publish_test.go | 5 +++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/consumer.go b/consumer.go index 501dcf8..19b5b94 100644 --- a/consumer.go +++ b/consumer.go @@ -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, diff --git a/tests/consumer_publish_test.go b/tests/consumer_publish_test.go index 35c41ec..868733a 100644 --- a/tests/consumer_publish_test.go +++ b/tests/consumer_publish_test.go @@ -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)