Пример #1
0
        public static void Main(string[] args)
        {
            try
            {
                Console.SetWindowSize(60, 10);

                BusConfig busConfig = new BusConfig("publisher", "1111", "rabbitmq://192.168.1.9:5672/");
                EndpointConvention.Map <StartNotification>(new Uri(busConfig.Host, QUEUE_START));
                EndpointConvention.Map <EndNotification2>(new Uri(busConfig.Host, QUEUE_END));

                string consumer = args.Length > 0 ? args[0] : null;
                switch (consumer)
                {
                case "start":
                    busConfig.ReceiveEndpoints.Add(new BusReceiveEndpoint(QUEUE_START, endpoint =>
                    {
                        endpoint.Handler <StartNotification>(async context =>
                        {
                            await Console.Out.WriteLineAsync($"Start message Received: {context.Message.ToString()}");
                            var endNotification = new EndNotification2(context.Message);
                            context.Send(endNotification);
                        });
                    }));
                    break;

                case "end":
                    busConfig.ReceiveEndpoints.Add(new BusReceiveEndpoint(QUEUE_END, endpoint =>
                    {
                        endpoint.Handler <EndNotification2>(async context =>
                        {
                            await Console.Out.WriteLineAsync($"End message Received: {context.Message.ToString()}");
                        });
                    }));
                    break;

                default:
                    break;
                }
                using (_busService = new BusService(busConfig))
                {
                    if (consumer != null)
                    {
                        Consumer(consumer);
                    }
                    else
                    {
                        Publisher();
                    }
                }
            }
            catch (Exception ex)
            {
                if (_busService != null)
                {
                    _busService.Dispose();
                }
                Console.WriteLine(ex.Message);
                Console.ReadKey();
            }
        }
Пример #2
0
        public BusService(BusConfig busConfig)
        {
            _busConfig = busConfig;

            BusControl = Bus.Factory.CreateUsingRabbitMq(cfg =>
            {
                var host = cfg.Host(_busConfig.Host, h =>
                {
                    h.Username(_busConfig.Username);
                    h.Password(_busConfig.Password);
                });

                foreach (var endpoint in _busConfig.ReceiveEndpoints)
                {
                    cfg.ReceiveEndpoint(host, endpoint.QueueName, endpoint.Action);
                }
            });

            BusControl.Start();
        }