示例#1
0
        protected ServiceBase(MessageChannel messageChannel)
        {
            Console.WriteLine(messageChannel);

            _factory = new ConnectionFactory
            {
                HostName = "localhost"
            };

            _connection = _factory.CreateConnection();
            _channel    = _connection.CreateModel();
            _channel.QueueDeclare(messageChannel.ToString(), false, false, false, null);

            _consumer = new EventingBasicConsumer(_channel);

            _consumer.Received += (model, ea) =>
            {
                Thread.Sleep(500);

                var body = Encoding.UTF8.GetString(ea.Body);

                MessageType messageType;
                Enum.TryParse(body, out messageType);

                Console.WriteLine("\t-> [x]    " + messageType);

                ReceiveMessage(messageType);
            };

            _channel.BasicConsume(messageChannel.ToString(), true, _consumer);
        }
示例#2
0
        public static void Send(MessageType messageType)
        {
            Thread.Sleep(300);
            Console.WriteLine("\t   [x] -> " + messageType);

            MessageChannel messageChannel = MessageRoutingMap.GetChannelForMessageType(messageType);

            using (var connection = Instance._factory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    channel.QueueDeclare(messageChannel.ToString(), false, false, false, null);

                    string message = messageType.ToString();

                    byte[] body = Encoding.UTF8.GetBytes(message);

                    channel.BasicPublish(
                        "",
                        messageChannel.ToString(),
                        null,
                        body);
                }
        }