예제 #1
0
        private static void StartConsumert(MessagePublishEventArgs eventArgs)
        {
            var consumer = new RabbitMqConsumer()
            {
                KeepConsumer = true
            };

            ServiceConsumerMap.Add(eventArgs.ServiceName, consumer);
            var thread = new Thread(() => consumer.Subscribe(eventArgs.ServiceName, eventArgs.ConsumerVirtualHostName, eventArgs.ConsumerQueueName));

            thread.Start();
        }
예제 #2
0
        public static void Main(string[] args)
        {
            var listOfThread = new List <Thread>();

            //run publiser
            var publisher = new RabbitMqPublisher();

            Console.WriteLine("Input message => service name:message");
            while (true)
            {
                var inputMsg = Console.ReadLine();
                if (inputMsg.Equals("exit", StringComparison.CurrentCultureIgnoreCase))
                {
                    break;
                }
                var inputArgs = inputMsg.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                if (inputArgs.Length != 2 || inputArgs.Any(input => string.IsNullOrWhiteSpace(input)))
                {
                    Console.WriteLine("Input message is not in correct format!");
                    continue;
                }

                var serviceName = inputArgs[0].Trim();
                var message     = inputArgs[1].Trim();
                MessagePublishEventArgs eventArgs = publisher.SentMessage(serviceName, message);
                if (!ServiceConsumerMap.ContainsKey(serviceName))
                {
                    var thread = new Thread(() => StartConsumert(eventArgs));
                    listOfThread.Add(thread);
                    thread.Start();
                }
            }

            foreach (var keyValuePair in ServiceConsumerMap)
            {
                if (keyValuePair.Value != null)
                {
                    keyValuePair.Value.KeepConsumer = false;
                }
            }

            listOfThread.ForEach(t => { if (t.IsAlive)
                                        {
                                            t.Abort();
                                        }
                                 });

            Console.ReadLine();
        }
        public MessagePublishEventArgs SentMessage(string serviceName, string message)
        {
            var eventArgs = new MessagePublishEventArgs()
            {
                ServiceName = serviceName
            };
            var factoryPublisher = GetConnectionFactory(serviceName, true);
            var factoryConsumer  = GetConnectionFactory(serviceName, false);

            using (var pubisherConnection = factoryPublisher.CreateConnection())
                using (var consumerConnection = factoryConsumer.CreateConnection())
                {
                    using (var pubisherModel = pubisherConnection.CreateModel())
                        using (var consumerModel = consumerConnection.CreateModel())
                        {
                            //set source exchange
                            var exchangePublisher = GetExchangeName(pubisherModel, serviceName, true);
                            var queuePublisher    = GetQueueName(pubisherModel, serviceName, true);
                            pubisherModel.QueueBind(
                                queue: queuePublisher,
                                exchange: exchangePublisher,
                                routingKey: "");

                            //set destination exchange
                            var exchangeConsumer = GetExchangeName(consumerModel, serviceName, false);
                            var queueConsumer    = GetQueueName(consumerModel, serviceName, false);
                            consumerModel.QueueBind(
                                queue: queueConsumer,
                                exchange: exchangeConsumer,
                                routingKey: "");

                            pubisherModel.ExchangeBind(exchangeConsumer, exchangePublisher, "");

                            var body = Encoding.UTF8.GetBytes(message);
                            pubisherModel.BasicPublish(exchange: exchangePublisher,
                                                       routingKey: "",
                                                       basicProperties: null,
                                                       body: body);

                            eventArgs.ConsumerExchangeName    = exchangeConsumer;
                            eventArgs.ConsumerQueueName       = queueConsumer;
                            eventArgs.ConsumerVirtualHostName = GetVirtualHostFromService(serviceName, false);
                        }
                }

            return(eventArgs);
        }