Ejemplo n.º 1
0
        static private Task ConsumerAsync()
        {
            var configManager      = CreateConfigurationManager();
            var receiverFactory    = new AMQReceiverFactory(new AMQConnectionFactory());
            var connectionSettings = configManager.GetConnectionSettings("defaultConnection");
            var queueSettings      = configManager.GetDestinationSettings("MY_AMQ_TEST");

            using (var receiver = receiverFactory.CreateText(connectionSettings, queueSettings))
            {
                while (true)
                {
                    try
                    {
                        try
                        {
                            var message = receiver.Receive(1000);
                            if (message != null)
                            {
                                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)
                    {
                        receiver.ClearCacheBuffer();
                        Console.WriteLine(ex);
                    }
                }
            }
        }