예제 #1
0
        public static void Main(string[] args)
        {
            ConcurrentDictionary <string, CalculationRequest> waitingRequest = new ConcurrentDictionary <string, CalculationRequest>();
            ConnectionFactory factory = new ConnectionFactory();

            factory.HostName    = "localhost";
            factory.VirtualHost = "/";
            factory.Port        = 5672;
            factory.UserName    = "******";
            factory.Password    = "******";

            conn    = factory.CreateConnection();
            channel = conn.CreateModel();

            var consumer = new EventingBasicConsumer(channel);

            consumer.Received += (sender, e) =>
            {
                string requestId = Encoding.UTF8.GetString((byte[])e.BasicProperties.Headers[Constants.RequestIdHeaderKey]);

                CalculationRequest request;
                if (waitingRequest.TryGetValue(requestId, out request))
                {
                    string messageData           = Encoding.UTF8.GetString(e.Body);
                    CalculationResponse response = JsonConvert.DeserializeObject <CalculationResponse>(messageData);
                    Console.WriteLine("Calculation request received " + response.ToString());
                }


                channel.BasicAck(e.DeliveryTag, false);
            };

            channel.BasicConsume("responses", false, consumer);

            Console.WriteLine("Press any key to start");
            Console.ReadKey();

            sendRequest(waitingRequest, channel, new CalculationRequest(2, 4, RequestType.Add));
            sendRequest(waitingRequest, channel, new CalculationRequest(2, 4, RequestType.Substract));

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
            channel.Close();
            conn.Close();
        }
예제 #2
0
        static void Main(string[] args)
        {
            ConcurrentDictionary <string, CalculationRequest> waitingRequests = new ConcurrentDictionary <string, CalculationRequest>();

            ConnectionFactory factory = new ConnectionFactory();

            // "guest"/"guest" by default, limited to localhost connections
            factory.HostName    = "localhost";
            factory.VirtualHost = "/";
            factory.Port        = 5673;
            factory.UserName    = "******";
            factory.Password    = "******";

            IConnection conn    = factory.CreateConnection();
            IModel      channel = conn.CreateModel();

            string responseQueueName = "res." + Guid.NewGuid().ToString();

            channel.QueueDeclare(responseQueueName);


            var consumer = new EventingBasicConsumer(channel);

            consumer.Received += (sender, e) =>
            {
                string             requestId = Encoding.UTF8.GetString((byte[])e.BasicProperties.Headers[Constants.RequestIdHeaderKey]);
                CalculationRequest request;

                if (waitingRequests.TryRemove(requestId, out request))
                {
                    string messageData           = System.Text.Encoding.UTF8.GetString(e.Body);
                    CalculationResponse response = JsonConvert.DeserializeObject <CalculationResponse>(messageData);

                    Console.WriteLine("Calculation result: " + request.ToString() + "=" + response.ToString());
                }
            };

            channel.BasicConsume(responseQueueName, true, consumer);

            Console.WriteLine("Press a key to send requests");
            Console.ReadKey();

            sendRequest(waitingRequests, channel, new CalculationRequest(2, 4, OperationType.Add), responseQueueName);
            sendRequest(waitingRequests, channel, new CalculationRequest(14, 6, OperationType.Subtract), responseQueueName);
            sendRequest(waitingRequests, channel, new CalculationRequest(50, 2, OperationType.Add), responseQueueName);
            sendRequest(waitingRequests, channel, new CalculationRequest(30, 6, OperationType.Subtract), responseQueueName);

            Console.ReadKey();

            channel.Close();
            conn.Close();
        }