Example #1
0
        public async Task Consume(ConsumeContext <IFailingMessage> context)
        {
            var receiveContext = (RabbitMqReceiveContext)context.ReceiveContext;

            await ColoredConsole.WriteLineAsync(
                $"Failed message received: {JsonConvert.SerializeObject(context.Message, Formatting.Indented)}\n" +
                $"IsFaulted: {receiveContext.IsFaulted}\n" +
                $"IsRedelivered: {receiveContext.Redelivered}\n" +
                $"Exchange: {receiveContext.Exchange}",
                ConsoleColor.Red);
        }
Example #2
0
        public async Task Consume(ConsumeContext <ISendEmail> context)
        {
            await ColoredConsole.WriteLineAsync(
                $"EmailSenderConsumer: Sending email to {context.Message.Recipient} at {context.Message.Email}",
                ConsoleColor.Yellow);

            await context.Publish <IEmailSent>(new
            {
                context.Message.Email,
                SentOn = DateTime.UtcNow
            });
        }
Example #3
0
        public static async Task Main()
        {
            var bus = Bus.Factory.CreateUsingRabbitMq(sbc =>
            {
                var host = sbc.Host(new Uri("rabbitmq://localhost"), h =>
                {
                    h.Username("guest");
                    h.Password("guest");
                });

                sbc.ReceiveEndpoint(host, "test_queue", ep =>
                {
                    ep.UseMessageRetry(cfg => cfg.Immediate(5));

                    ep.Handler <YourMessage>(async context =>
                    {
                        await Console.Out.WriteLineAsync($"Handler 1: Received: {context.Message.Text}, address: {context.ReceiveContext.InputAddress}");
                    });

                    ep.Handler <YourMessageReceived>(async context =>
                    {
                        await Console.Out.WriteLineAsync($"Handler 2: Confirmation received: {context.Message.ReceivedText}, " +
                                                         $"address: {context.ReceiveContext.InputAddress}");
                    });

                    ep.Consumer <FailingConsumer>();
                });

                sbc.ReceiveEndpoint(host, "test_queue_error", ep =>
                {
                    ep.BindMessageExchanges = false;
                    ep.Consumer <ErrorQueueConsumer>();
                });

                sbc.ReceiveEndpoint(host, "second_test_queue", ep =>
                {
                    ep.Handler <YourMessage>(async context =>
                    {
                        await Console.Out.WriteLineAsync($"Handler 3: Received: {context.Message.Text}, address: {context.ReceiveContext.InputAddress}");

                        await context.Publish(new YourMessageReceived
                        {
                            ReceivedText = $"Received message: {context.Message.Text}"
                        });
                    });
                });

                sbc.ReceiveEndpoint(host, "email_sender_queue", ep =>
                {
                    ep.Consumer <EmailSenderConsumer>();
                });

                sbc.ReceiveEndpoint(host, "notification_queue", ep =>
                {
                    ep.Handler <YourMessage>(async context =>
                    {
                        await Console.Out.WriteLineAsync($"Handler 4: Received: {context.Message.Text}, address: {context.ReceiveContext.InputAddress}");
                    });

                    ep.Consumer <NotificationConsumer>();
                });
            });

            bus.Start();

            await bus.Publish(new YourMessage { Text = "Publish Hi" });

            var sendEndpoint = await bus.GetSendEndpoint(new Uri("rabbitmq://localhost/test_queue"));

            await sendEndpoint.Send(new YourMessage { Text = "Send Hahahaha" });

            var emailSenderEndpoint = await bus.GetSendEndpoint(new Uri("rabbitmq://localhost/email_sender_queue"));

            await emailSenderEndpoint.Send <ISendEmail>(new { Email = "*****@*****.**", Recipient = "Good Boy" });

            await ColoredConsole.WriteLineAsync("Sending failing message...", ConsoleColor.Cyan);

            await bus.Publish <IFailingMessage>(new { Id = Guid.NewGuid() });

            //Console.WriteLine("Press any key to exit");
            //Console.ReadLine();

            Thread.Sleep(TimeSpan.FromSeconds(1));

            bus.Stop();
        }
Example #4
0
 public async Task Consume(ConsumeContext <Fault <IFailingMessage> > context)
 {
     await ColoredConsole.WriteLineAsync(
         $"Intercepted exception: {JsonConvert.SerializeObject(context.Message, Formatting.Indented)}",
         ConsoleColor.Green);
 }
Example #5
0
 public async Task Consume(ConsumeContext <IEmailSent> context)
 {
     await ColoredConsole.WriteLineAsync(
         $"NotificationConsumer: Message to {context.Message.Email} sent on {context.Message.SentOn:F}",
         ConsoleColor.White);
 }