protected override async Task ExecuteAsync(CancellationToken stoppingToken) { var consumerHelper = new ConsumerWrapper("libraryreservations", _config); _logger.LogInformation("The Service is running and waiting for reservations"); while (!stoppingToken.IsCancellationRequested) { var order = consumerHelper.ReadMessage <ReservationMessage>(); _logger.LogInformation($"Got a reservation for {order.For} for the items {order.Items}"); var numberOfItems = order.Items.Split(',').Count(); await Task.Delay(1000 *numberOfItems); if (numberOfItems % 2 == 0) { await _httpService.MarkReservationAccepted(order); _logger.LogInformation("\tApproved that order."); } else { await _httpService.MarkReservationRejected(order); _logger.LogWarning("\tRejected that one!"); } } }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { Console.WriteLine("OrderProcessing Service Started"); while (!stoppingToken.IsCancellationRequested) { var consumerHelper = new ConsumerWrapper(consumerConfig, "orderrequests"); string orderRequest = consumerHelper.ReadMessage(); if (string.IsNullOrWhiteSpace(orderRequest)) { Console.WriteLine("No message available!"); return; } //Deserilaize OrderRequest order = JsonConvert.DeserializeObject <OrderRequest>(orderRequest); if (order == null) { Console.WriteLine("Order details not available!"); return; } //TODO:: Process Order Console.WriteLine($"Info: OrderHandler => Processing the order for {order.productname}"); order.status = OrderStatus.COMPLETED; //Write to ReadyToShip Queue var producerWrapper = new ProducerWrapper(producerConfig, "readytoship"); await producerWrapper.WriteMessage(JsonConvert.SerializeObject(order)); } }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { foreach (var topic in _topics) { var t = new Thread(async() => { var consumerConfig = new ConsumerConfig(); _configuration.Bind("Kafka:ConsumerConfig", consumerConfig); consumerConfig.GroupId = "Elearning"; consumerConfig.AutoOffsetReset = AutoOffsetReset.Earliest; using (var consumer = new ConsumerWrapper <Null, string>(consumerConfig, topic)) { while (true) { try { var message = consumer.ReadMessage(); if (message == null) { continue; } var exam = JsonConvert.DeserializeObject <Examination>(message); var oldExam = (_examRepo.GetEntityByIdAsync(exam.ExamId).Result); if (oldExam == null) { continue; } Console.WriteLine(oldExam.ExamId); if (oldExam.Status != 1) { var c = await _examRepo.Update(exam); } } catch (Exception ex) { Console.WriteLine(ex); } } } }); t.Start(); await Task.Delay(3000, stoppingToken); _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); } } }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { Console.WriteLine("OrderProcessing Service Started"); while (!stoppingToken.IsCancellationRequested) { var consumerHelper = new ConsumerWrapper(consumerConfig, "orderrequests"); string orderRequest = consumerHelper.ReadMessage(); //Deserialize OrderRequest order = JsonConvert.DeserializeObject <OrderRequest>(orderRequest); //TODO:: Process Order Console.WriteLine($"Info: OrderHandler => Processing the order for {order.ProductName}"); order.Status = OrderStatus.Completed; //Write to ReadyToShip Queue var producerWrapper = new ProducerWrapper(producerConfig, "readytoship"); await producerWrapper.WriteMessage(JsonConvert.SerializeObject(order)); } }