@@ -49,6 +49,7 @@ public class RabbitMQMessageSubscriberService : IMessageBrokerSubscriberService
49
49
private readonly string _portNumber ;
50
50
private IModel ? _channel ;
51
51
private bool _disposedValue ;
52
+ private readonly ushort _prefetchCount = 1 ;
52
53
53
54
public event ConnectionErrorHandler ? OnConnectionError ;
54
55
@@ -72,19 +73,23 @@ public RabbitMQMessageSubscriberService(IOptions<MessageBrokerServiceConfigurati
72
73
_deadLetterExchange = configuration . SubscriberSettings [ ConfigurationKeys . DeadLetterExchange ] ;
73
74
_deliveryLimit = int . Parse ( configuration . SubscriberSettings [ ConfigurationKeys . DeliveryLimit ] , NumberFormatInfo . InvariantInfo ) ;
74
75
_requeueDelay = int . Parse ( configuration . SubscriberSettings [ ConfigurationKeys . RequeueDelay ] , NumberFormatInfo . InvariantInfo ) ;
76
+ if ( configuration . SubscriberSettings . TryGetValue ( ConfigurationKeys . PrefetchCount , out var value ) )
77
+ {
78
+ _prefetchCount = ushort . Parse ( value ?? "1" , NumberFormatInfo . InvariantInfo ) ;
79
+ }
75
80
76
- if ( configuration . SubscriberSettings . ContainsKey ( ConfigurationKeys . UseSSL ) )
81
+ if ( configuration . SubscriberSettings . TryGetValue ( ConfigurationKeys . UseSSL , out var sslValue ) )
77
82
{
78
- _useSSL = configuration . SubscriberSettings [ ConfigurationKeys . UseSSL ] ;
83
+ _useSSL = sslValue ;
79
84
}
80
85
else
81
86
{
82
87
_useSSL = string . Empty ;
83
88
}
84
89
85
- if ( configuration . SubscriberSettings . ContainsKey ( ConfigurationKeys . Port ) )
90
+ if ( configuration . SubscriberSettings . TryGetValue ( ConfigurationKeys . Port , out var portValue ) )
86
91
{
87
- _portNumber = configuration . SubscriberSettings [ ConfigurationKeys . Port ] ;
92
+ _portNumber = portValue ;
88
93
}
89
94
else
90
95
{
@@ -112,7 +117,7 @@ private void CreateChannel()
112
117
_channel = _rabbitMqConnectionFactory . CreateChannel ( ChannelType . Subscriber , _endpoint , _username , _password , _virtualHost , _useSSL , _portNumber ) ?? throw new ServiceException ( "Failed to create a new channel to RabbitMQ" ) ;
113
118
_channel . ExchangeDeclare ( _exchange , ExchangeType . Topic , durable : true , autoDelete : false ) ;
114
119
_channel . ExchangeDeclare ( _deadLetterExchange , ExchangeType . Topic , durable : true , autoDelete : false ) ;
115
- _channel . BasicQos ( prefetchSize : 0 , prefetchCount : 1 , global : false ) ;
120
+ _channel . BasicQos ( prefetchSize : 0 , prefetchCount : _prefetchCount , global : false ) ;
116
121
_channel . ModelShutdown += Channel_ModelShutdown ;
117
122
_logger . ConnectedToRabbitMQ ( Name , _endpoint , _virtualHost ) ;
118
123
} ) ;
@@ -234,15 +239,15 @@ private QueueDeclareOk DeclareQueues(string[] topics, string queue, ushort prefe
234
239
235
240
var queueDeclareResult = _channel ! . QueueDeclare ( queue : queue , durable : true , exclusive : false , autoDelete : false , arguments : arguments ) ;
236
241
237
- var deadLetterExists = QueueExists ( deadLetterQueue ) ;
238
- if ( deadLetterExists . exists == false )
242
+ var ( exists , accessable ) = QueueExists ( deadLetterQueue ) ;
243
+ if ( exists == false )
239
244
{
240
245
_channel . QueueDeclare ( queue : deadLetterQueue , durable : true , exclusive : false , autoDelete : false ) ;
241
246
}
242
247
243
248
try
244
249
{
245
- BindToRoutingKeys ( topics , queueDeclareResult . QueueName , deadLetterExists . accessable ? deadLetterQueue : "" ) ;
250
+ BindToRoutingKeys ( topics , queueDeclareResult . QueueName , accessable ? deadLetterQueue : "" ) ;
246
251
_channel . BasicQos ( 0 , prefetchCount , false ) ;
247
252
}
248
253
catch ( OperationInterruptedException operationInterruptedException )
0 commit comments