Ejemplo n.º 1
0
        public void OnOrderGetReceived(IMessage message)
        {
            var timeout       = TimeSpan.FromSeconds(HTTP_TIMEOUT_SECONDS);
            var textMessage   = (ITextMessage)message;
            var replyQueue    = textMessage.NMSReplyTo;
            var correlationId = textMessage.NMSCorrelationID;

            var orderId = textMessage.Text;

            Console.WriteLine($"{_timestamp.Generate()} GET OrderId={orderId}");

            var order = new RandomOrderFactory().Create();

            order.OrderId = Convert.ToInt32(orderId);

            using (var producer = _mqSession.Session.CreateProducer(replyQueue))
            {
                var serialized   = JsonConvert.SerializeObject(order);
                var replyMessage = _mqSession.Session.CreateTextMessage(serialized);
                replyMessage.NMSCorrelationID = correlationId;
                replyMessage.NMSTimeToLive    = timeout;

                producer.Send(replyMessage);
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            // generate a random customerId
            var r = new Random();

            _customerId          = r.Next(10000, 100000);
            Console.Title        = $"Caller: (CustomerId={_customerId}";
            Console.WindowHeight = 6;
            Console.WindowWidth  = 100;

            var stopwatch    = new Stopwatch();
            var orderFactory = new RandomOrderFactory();
            var httpClient   = new HttpClient()
            {
                BaseAddress = new Uri(_baseUri)
            };

            while (true)
            {
                var order = orderFactory.Create();
                order.Customer = _customerId;

                var serialised    = JsonConvert.SerializeObject(order, Formatting.Indented);
                var stringContent = new StringContent(serialised, Encoding.UTF8, "application/json");

                stopwatch.Restart();

                var post = httpClient.PostAsync("/api/orders", stringContent);
                post.Wait();

                stopwatch.Stop();


                if (!post.Result.IsSuccessStatusCode)
                {
                    throw new HttpException($"{post.Result.StatusCode} - {post.Result.ReasonPhrase}");
                }

                var responseBody = post.Result.Content.ReadAsStringAsync();
                responseBody.Wait();
                var responseOrder = JsonConvert.DeserializeObject <Order>(responseBody.Result);

                Console.WriteLine(
                    $"Sent OrderId {order.OrderId} for Customer {order.Customer}, " +
                    $"Received OrderId {responseOrder.OrderId} for Customer {responseOrder.Customer} " +
                    $"(roundtrip: {stopwatch.ElapsedMilliseconds} ms)");

                Thread.Sleep(1000);
            }
        }