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); } } } }
static Task PoolTestAsync() { var configManager = CreateConfigurationManager(); var senderFactory = new AMQSenderFactory(new AMQConnectionFactory()); var receiverFactory = new AMQReceiverFactory(new AMQConnectionFactory()); var factory = new AMQSenderReceiverFactory(senderFactory, receiverFactory); using (var amqPool = new AMQPoolAsync(configManager, factory, 5)) { var tasks = new List <Task>(); for (var i = 0; i < 10; i++) { var task = Task.Run(async() => { try { for (var j = 0; j < 10; j++) { using (var producer = await amqPool.GetAsync <IAMQSenderText>("defaultConnection", "MY_AMQ_TEST")) { var message = $"message: {i * j}"; await producer.Instance.SendAsync(message); Console.WriteLine($"{producer.PoolId} - {producer.Instance.Id} - {message}"); } } } catch (Exception ex) { Console.WriteLine(ex); } }); tasks.Add(task); } Console.WriteLine("Waiting for Tasks to complete..."); Task.WaitAll(tasks.ToArray()); Console.WriteLine("Tasks completed."); return(Task.CompletedTask); } }
static void PoolTest() { var configManager = CreateConfigurationManager(); var senderFactory = new AMQSenderFactory(new AMQConnectionFactory()); var receiverFactory = new AMQReceiverFactory(new AMQConnectionFactory()); var factory = new AMQSenderReceiverFactory(senderFactory, receiverFactory); using (var amqPoole = new AMQPool(configManager, factory, 5)) { for (var i = 0; i < 10; i++) { var t = new Thread(PoolThreadTest1); t.Start(new Tuple <AMQPool, int>(amqPoole, i)); } Console.WriteLine("Waiting for Tasks to complete..."); Console.ReadKey(); } }