@@ -23,12 +23,6 @@ type ConsumerOption struct {
2323 // Middlewares is a list of middleware functions to be applied to the inbound message handler.
2424 Middlewares []interfaces.InboundMessageHandlerMiddlewareFunc
2525
26- // ActionsPatternSubscribed specifies the list of action patterns that the consumer is subscribed to.
27- ActionsPatternSubscribed []string
28-
29- // TopicName specifies the name of the topic to consume messages from.
30- TopicName string
31-
3226 // MaxRetryFailedMessage specifies the maximum number of times a failed message should be retried.
3327 MaxRetryFailedMessage int64
3428
@@ -68,22 +62,6 @@ func WithMiddlewares(middlewares ...interfaces.InboundMessageHandlerMiddlewareFu
6862 }
6963}
7064
71- // WithActionsPatternSubscribed sets the actions that the consumer will subscribe to.
72- // It takes a variadic parameter `actions` which represents the actions to be subscribed.
73- // The actions are stored in the `ActionsPatternSubscribed` field of the `ConsumerOption` struct.
74- func WithActionsPatternSubscribed (actions ... string ) ConsumerOptionFunc {
75- return func (opt * ConsumerOption ) {
76- opt .ActionsPatternSubscribed = actions
77- }
78- }
79-
80- // WithTopicName sets the topic name for the consumer option.
81- func WithTopicName (name string ) ConsumerOptionFunc {
82- return func (opt * ConsumerOption ) {
83- opt .TopicName = name
84- }
85- }
86-
8765// WithMaxRetryFailedMessage sets the maximum number of retries for failed messages.
8866// It takes an integer parameter 'n' and returns an ConsumerOptionFunc.
8967// The ConsumerOptionFunc updates the 'MaxRetryFailedMessage' field of the ConsumerOption struct.
@@ -122,10 +100,89 @@ type RabbitMQConsumerConfig struct {
122100 ConsumerChannel * amqp.Channel
123101 // ReQueueChannel is the channel used for re-queuing messages in RabbitMQ.
124102 ReQueueChannel * amqp.Channel
103+
104+ // QueueDeclareConfig specifies the configuration for declaring a RabbitMQ queue.
105+ QueueDeclareConfig * RabbitMQQueueDeclareConfig
106+
107+ // QueueBindConfig specifies the configuration for binding a queue to an exchange in RabbitMQ.
108+ QueueBindConfig * RabbitMQQueueBindConfig
109+ }
110+
111+ // RabbitMQQueueDeclareConfig represents the configuration options for declaring a RabbitMQ queue.
112+ // * Durable and Non-Auto-Deleted queues will survive server restarts and remain
113+ // when there are no remaining consumers or bindings. Persistent publishings will
114+ // be restored in this queue on server restart. These queues are only able to be
115+ // bound to durable exchanges.
116+ // * Non-Durable and Auto-Deleted queues will not be redeclared on server restart
117+ // and will be deleted by the server after a short time when the last consumer is
118+ // canceled or the last consumer's channel is closed. Queues with this lifetime
119+ // can also be deleted normally with QueueDelete. These durable queues can only
120+ // be bound to non-durable exchanges.
121+ // * Non-Durable and Non-Auto-Deleted queues will remain declared as long as the
122+ // server is running regardless of how many consumers. This lifetime is useful
123+ // for temporary topologies that may have long delays between consumer activity.
124+ // These queues can only be bound to non-durable exchanges.
125+ // * Durable and Auto-Deleted queues will be restored on server restart, but without
126+ // active consumers will not survive and be removed. This Lifetime is unlikely
127+ // to be useful.
128+ // * Exclusive queues are only accessible by the connection that declares them and
129+ // will be deleted when the connection closes. Channels on other connections
130+ // will receive an error when attempting to declare, bind, consume, purge or
131+ // delete a queue with the same name.
132+ // * When noWait is true, the queue will assume to be declared on the server. A
133+ // channel exception will arrive if the conditions are met for existing queues
134+ // or attempting to modify an existing queue from a different connection.
135+ // When the error return value is not nil, you can assume the queue could not be
136+ // declared with these parameters, and the channel will be closed.
137+ type RabbitMQQueueDeclareConfig struct {
138+ Durable bool // Whether the queue should survive a broker restart.
139+ AutoDelete bool // Whether the queue should be deleted when there are no more consumers.
140+ Exclusive bool // Whether the queue should be exclusive to the connection that declares it.
141+ NoWait bool // Whether to wait for a response from the server after declaring the queue.
142+ Args amqp.Table // Additional arguments to be passed when declaring the queue.
143+ }
144+
145+ // RabbitMQQueueBindConfig represents the configuration options for binding a queue to an exchange in RabbitMQ.
146+ type RabbitMQQueueBindConfig struct {
147+ RoutingKeys []string // The routing key to use for the binding.
148+ ExchangeName string // The name of the exchange to bind to.
149+ NoWait bool // Whether to wait for a response from the server.
150+ Args amqp.Table // Additional arguments for the binding.
125151}
126152
127153const (
128154 ConsumerPlatformRabbitMQ = options .PlatformRabbitMQ
129155 ConsumerPlatformGooglePubSub = options .PlatformGooglePubSub
130156 ConsumerPlatformSQS = options .PlatformSQS
131157)
158+
159+ // RabbitMQConfigWithDefaultTopicFanOutPattern returns a RabbitMQConsumerConfig with default configuration for topic fanout pattern.
160+ // It takes the queueName, exchangeName, and routingKeys as parameters and returns a pointer to RabbitMQConsumerConfig.
161+ // The default configuration includes a durable queue that is not auto-deleted, exclusive, and no-wait.
162+ // The queue is bound to the exchange with the provided routing keys.
163+ // the exchange is must be a fanout exchange. The exchange must be declared before using this configuration.
164+ // Read more about fanout exchange: https://www.rabbitmq.com/tutorials/amqp-concepts.html#exchange-fanout
165+ //
166+ // the routing keys can be in pattern format.
167+ // e.g. "a.*.b.#" will match "a.b", "a.c.b", "a.c.d.b", etc.
168+ // For more information on pattern matching, see https://www.rabbitmq.com/tutorials/tutorial-five-go.html
169+ func RabbitMQConfigWithDefaultTopicFanOutPattern (consumerChannel , publisherChannel * amqp.Channel ,
170+ exchangeName string , routingKeys []string ) * RabbitMQConsumerConfig {
171+ return & RabbitMQConsumerConfig {
172+ ConsumerChannel : consumerChannel ,
173+ ReQueueChannel : publisherChannel ,
174+ QueueDeclareConfig : & RabbitMQQueueDeclareConfig {
175+ Durable : true ,
176+ AutoDelete : false ,
177+ Exclusive : false ,
178+ NoWait : false ,
179+ Args : nil ,
180+ },
181+ QueueBindConfig : & RabbitMQQueueBindConfig {
182+ RoutingKeys : routingKeys ,
183+ ExchangeName : exchangeName ,
184+ NoWait : false ,
185+ Args : nil ,
186+ },
187+ }
188+ }
0 commit comments