Beispiel #1
0
        static private Task ConsumerAsync()
        {
            var args = new Dictionary <string, object>
            {
                { "x-dead-letter-exchange", "" },
                { "x-dead-letter-routing-key", "My.DLQ" }
            };

            var consumerFactory    = new RMQConsumerFactory(new RMQConnectionFactory());
            var connectionSettings = GetRMQConnectionSettings();
            var queueSettings      = new RMQQueueSettings("My.Queue", "My.Queue", true, false, false, true, arguments: args);

            using (var consumer = consumerFactory.CreateText(connectionSettings, queueSettings))
            {
                consumer.Mode = ConsumerMode.OnNoMessage_ReturnNull;

                while (true)
                {
                    try
                    {
                        try
                        {
                            var message = consumer.Receive(1000);
                            if (message != null)
                            {
                                //message.Reject(true);
                                //message.Reject();

                                message.Acknowledge();
                            }
                        }
                        catch (TimeoutException)
                        {
                        }
                        catch (MessageException ex)
                        {
                            switch (ex.ExceptionCode)
                            {
                            case MessageExceptionCode.ExclusiveLock:
                                //await Task.Delay(5000);
                                break;

                            case MessageExceptionCode.LostConnection:
                                //await Task.Delay(5000);
                                throw;

                            default:
                                throw;
                            }
                        }
                        catch (SqlException ex)
                        {
                            if (SQLErrors.IsSevereErrorCode(ex.Number))
                            {
                                // issue connecting with SQL server
                                //await Task.Delay(5000);
                            }

                            throw;
                        }
                    }
                    catch (Exception ex)
                    {
                        consumer.ClearCacheBuffer();
                        Console.WriteLine(ex);
                    }
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Uses the RMQ bound consumers.
        /// </summary>
        /// <param name="serviceProvider">The service provider.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <param name="onException">The on exception.</param>
        public static void UseRMQBoundConsumers(this IServiceProvider serviceProvider, CancellationToken cancellationToken, Action <Exception> onException = null)
        {
            var consumerFactory  = new RMQConsumerFactory(new RMQConnectionFactory());
            var subcriberFactory = new RMQSubscriberFactory(new RMQConnectionFactory());
            var logFactory       = serviceProvider.GetService <ILoggerFactory>();

            foreach (var type in _Consumers.Keys)
            {
                Task.Run(async() =>
                {
                    var logger         = logFactory.CreateLogger(type);
                    var options        = _Consumers[type];
                    var consumer       = (IRMQConsumer)null;
                    var isTextConsumer = typeof(IRMQBoundConsumerText).IsAssignableFrom(type);

                    if (options is RMQBuilderSubscriptionOptions)
                    {
                        var subOptions = (options as RMQBuilderSubscriptionOptions);

                        if (isTextConsumer)
                        {
                            consumer = subcriberFactory.CreateText(subOptions.ConnectionSettings, subOptions.ExchangeSettings, subOptions.QueueSettings);
                        }
                        else
                        {
                            consumer = subcriberFactory.CreateBytes(subOptions.ConnectionSettings, subOptions.ExchangeSettings, subOptions.QueueSettings);
                        }
                    }
                    else // standard consumer
                    {
                        if (isTextConsumer)
                        {
                            consumer = consumerFactory.CreateText(options.ConnectionSettings, options.QueueSettings);
                        }
                        else
                        {
                            consumer = consumerFactory.CreateBytes(options.ConnectionSettings, options.QueueSettings);
                        }
                    }

                    using (consumer)
                    {
                        consumer.Mode = ConsumerMode.OnNoMessage_ReturnNull;

                        while (cancellationToken.IsCancellationRequested == false)
                        {
                            try
                            {
                                try
                                {
                                    if (consumer is IRMQConsumerText)
                                    {
                                        var message = (consumer as IRMQConsumerText).Receive(1000);
                                        if (message != null)
                                        {
                                            using (var scope = serviceProvider.CreateScope())
                                            {
                                                var loopConsumer = scope.ServiceProvider.GetService(type) as IRMQBoundConsumerText;
                                                await loopConsumer.OnMessageAsync(message, cancellationToken);
                                                message.Acknowledge();
                                            }
                                        }
                                    }
                                    else // bytes
                                    {
                                        var message = (consumer as IRMQConsumerBytes).Receive(1000);
                                        if (message != null)
                                        {
                                            using (var scope = serviceProvider.CreateScope())
                                            {
                                                var loopConsumer = scope.ServiceProvider.GetService(type) as IRMQBoundConsumerBytes;
                                                await loopConsumer.OnMessageAsync(message);
                                                message.Acknowledge();
                                            }
                                        }
                                    }
                                }
                                catch (TimeoutException)
                                {
                                }
                                catch (MessageException ex)
                                {
                                    switch (ex.ExceptionCode)
                                    {
                                    case MessageExceptionCode.ExclusiveLock:
                                        await Task.Delay(1000);
                                        break;

                                    default:
                                        throw;
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                consumer.ClearCacheBuffer();

                                try
                                {
                                    onException?.Invoke(ex);
                                }
                                catch (Exception ex2)
                                {
                                    var aggregateException = new AggregateException("RMQ Consumer Error (OnException Handler fault).", new[] { ex, ex2 });
                                    logger.LogError(aggregateException, aggregateException.Message);
                                }
                            }
                        }
                    }
                });
            }
        }
Beispiel #3
0
        static private Task ConsumerAsync()
        {
            var configManager      = CreateConfigurationManager();
            var consumerFactory    = new RMQConsumerFactory(new RMQConnectionFactory());
            var connectionSettings = configManager.GetConnectionSettings("connection");
            var queueSettings      = configManager.GetQueueSettings("MY_RMQ_TEST");

            using (var consumer = consumerFactory.CreateText(connectionSettings, queueSettings))
            {
                consumer.Mode = ConsumerMode.OnNoMessage_ReturnNull;

                while (true)
                {
                    try
                    {
                        try
                        {
                            var message = consumer.Receive(1000);
                            if (message != null)
                            {
                                //message.Reject(true);
                                //message.Reject();

                                Console.WriteLine(message.Data);
                                message.Acknowledge();
                            }
                        }
                        catch (TimeoutException)
                        {
                        }
                        catch (MessageException ex)
                        {
                            switch (ex.ExceptionCode)
                            {
                            case MessageExceptionCode.ExclusiveLock:
                                //await Task.Delay(5000);
                                break;

                            case MessageExceptionCode.LostConnection:
                                //await Task.Delay(5000);
                                break;

                            default:
                                throw;
                            }
                        }
                        catch (SqlException ex)
                        {
                            if (SQLErrors.IsSevereErrorCode(ex.Number))
                            {
                                // issue connecting with SQL server
                                //await Task.Delay(5000);
                            }

                            throw;
                        }
                    }
                    catch (Exception ex)
                    {
                        consumer.ClearCacheBuffer();
                        Console.WriteLine(ex);
                    }
                }
            }
        }