public static void Run() { var loggerFactory = new ActionLinkLoggerFactory(x => new ColoredConsoleLinkLogger(x)); using (var link = new Link("amqp://localhost", cfg => cfg .AutoStart(false) .LoggerFactory(loggerFactory) )) { //TestTopology(link); #pragma warning disable 4014 TestPullConsumer(link); #pragma warning restore 4014 TestPublish(link); ColorConsole.WriteLine("Running..."); Console.ReadLine(); } }
private static async Task TestPullConsumer(Link link) { await Task.Delay(0) .ConfigureAwait(false); Console.WriteLine("Creating consumer"); using (var consumer = link.CreateConsumer( async cfg => { var exchange = await cfg.ExchangeDeclarePassive("link.consume"); var queue = await cfg.QueueDeclare("link.consume"); await cfg.Bind(queue, exchange); return queue; }, config: cfg => cfg.AutoAck(false) .PrefetchCount(1000) .TypeNameMap(map => map.Set<string>("string").Set<MyClass>("woot")) )) { //Console.ReadLine(); ILinkMessage<object> msg; ILinkMessage<object> oldMsg = null; while (true) { try { msg = await consumer.GetMessageAsync(); if (msg == oldMsg) { ColorConsole.WriteLine("DUPE MESSAGE".Red()); } oldMsg = msg; ColorConsole.WriteLine("Message recieved(".Green(), msg.GetType().GenericTypeArguments[0].Name, "):\n".Green(), JsonConvert.SerializeObject(msg, Formatting.Indented)); try { await msg.AckAsync() .ConfigureAwait(false); } catch (Exception ex) { ColorConsole.WriteLine("Consume ACK exception:".Red(), ex.ToString()); } } catch (ObjectDisposedException) { break; } catch (Exception ex) { ColorConsole.WriteLine("Consume exception:".Red(), ex.ToString()); } } } }
private static void TestTopology(Link link) { ColorConsole.WriteLine("Creating topology configurators"); link.CreatePersistentTopologyConfigurator(PersConfigure, configurationError: PersOnException); ColorConsole.WriteLine("Starting..."); link.Initialize(); ColorConsole.WriteLine("Configuring topology"); try { link.ConfigureTopologyAsync(OnceConfigure, TimeSpan.FromSeconds(10)) .WaitAndUnwrapException(); ColorConsole.WriteLine("Topology configured"); } catch (Exception ex) { ColorConsole.WriteLine($"Topology config exception: {ex}"); } }
private static void TestPublish(Link link) { ColorConsole.WriteLine("Starting..."); link.Initialize(); using ( var producer = link.CreateProducer( async cfg => await cfg.ExchangeDeclare("link.consume", LinkExchangeType.Fanout), config: cfg => cfg.TypeNameMap(map => map.Set<string>("string").Set<MyClass>("woot"))) ) { ColorConsole.WriteLine("Publish"); var sw = Stopwatch.StartNew(); var tasks = Enumerable .Range(0, 100) .Select(i => $"Item {i + 1}") .Select(body => producer.PublishAsync( body, new LinkMessageProperties {DeliveryMode = LinkMessageDeliveryMode.Persistent}, new LinkPublishProperties {Mandatory = false} )) .ToArray(); ColorConsole.WriteLine("Waiting for publish end..."); Task.WaitAll(tasks); ColorConsole.WriteLine("Publish done"); sw.Stop(); Console.WriteLine("Done in {0:0.###}s", sw.Elapsed.TotalSeconds); } }