コード例 #1
0
        public async Task ExecuteRequests(int batchSize = 5)
        {
            if (batchSize > 5)
            {
                batchSize = 5;
            }

            var requests = (await GetRequests()).OrderByDescending(x => x.Key).Take(5);

            foreach (var item in requests)
            {
                try
                {
                    //call mockup
                    var response = await ExecuteRequest(item.Value);

                    //delete request from cache
                    var requestToDelete = requests.Where(x => x.Value.Request == item.Value.Request).FirstOrDefault();
                    await cacheAccessor.DeleteRequestsFromCache(requestToDelete.Value);

                    //save response
                    await cacheAccessor.SetResponseToCache(
                        new ResponseItem
                    {
                        Customer    = response,
                        RequestGuid = item.Value.Request
                    });
                }
                catch (Exception ex)
                {
                    continue;
                }
            }
        }
コード例 #2
0
        public void ReceiveMessageFromQ()
        {
            try
            {
                //TODO: move hostname to localhost
                _factory = new ConnectionFactory()
                {
                    HostName = configuration.GetValue <string>("RabbitMQ:HostName")
                };
                _connection = _factory.CreateConnection();
                _channel    = _connection.CreateModel();

                {
                    _channel.QueueDeclare(queue: "counter",
                                          durable: true,
                                          exclusive: false,
                                          autoDelete: false,
                                          arguments: null);

                    _channel.BasicQos(prefetchSize: 0, prefetchCount: 3, global: false);

                    var consumer = new EventingBasicConsumer(_channel);
                    consumer.Received += async(model, ea) =>
                    {
                        var item = Helpers.BytesToObject <MQItem>(ea.Body);
                        await cacheAccessor.DeleteRequestsFromCache(item);

                        _channel.BasicAck(ea.DeliveryTag, false);
                    };

                    _channel.BasicConsume(queue: "counter", autoAck: false, consumer: consumer);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"{ex.Message} | {ex.StackTrace}");
            }
        }