Example #1
0
        private static async Task SendMessageToResultTopicAsync(LeaveApplicationReceived leaveRequest, bool isApproved,
                                                                int partitionId
                                                                )
        {
            using var schemaRegistry = new CachedSchemaRegistryClient(_schemaRegistryConfig);
            using var producer       = new ProducerBuilder <string, LeaveApplicationProcessed>(_producerConfig)
                                       .SetKeySerializer(new AvroSerializer <string>(schemaRegistry))
                                       .SetValueSerializer(new AvroSerializer <LeaveApplicationProcessed>(schemaRegistry))
                                       .Build();
            {
                var leaveApplicationResult = new LeaveApplicationProcessed
                {
                    EmpDepartment        = leaveRequest.EmpDepartment,
                    EmpEmail             = leaveRequest.EmpEmail,
                    LeaveDurationInHours = leaveRequest.LeaveDurationInHours,
                    LeaveStartDateTicks  = leaveRequest.LeaveStartDateTicks,
                    ProcessedBy          = $"Manager #{partitionId}",
                    Result = isApproved
                        ? "Approved: Your leave application has been approved."
                        : "Declined: Your leave application has been declined."
                };

                var result = await producer.ProduceAsync(ApplicationConstants.LeaveApplicationResultsTopicName,
                                                         new Message <string, LeaveApplicationProcessed>
                {
                    Key   = $"{leaveRequest.EmpEmail}-{DateTime.UtcNow.Ticks}",
                    Value = leaveApplicationResult
                });

                Console.WriteLine(
                    $"\nMsg: Leave request processed and queued at offset {result.Offset.Value} in the Topic {result.Topic}");
            }
        }
Example #2
0
        private static async Task AddMessagesAsync()
        {
            using var schemaRegistry = new CachedSchemaRegistryClient(_schemaRegistryConfig);
            using var producer       = new ProducerBuilder <string, LeaveApplicationReceived>(_producerConfig)
                                       .SetKeySerializer(new AvroSerializer <string>(schemaRegistry))
                                       .SetValueSerializer(new AvroSerializer <LeaveApplicationReceived>(schemaRegistry))
                                       .Build();
            while (true)
            {
                var empEmail = ReadLine.Read("Enter your employee Email (e.g. [email protected]): ",
                                             "*****@*****.**").ToLowerInvariant();
                var empDepartment        = ReadLine.Read("Enter your department code (HR, IT, OPS): ").ToUpperInvariant();
                var leaveDurationInHours =
                    int.Parse(ReadLine.Read("Enter number of hours of leave requested (e.g. 8): ", "8"));
                var leaveStartDate = DateTime.ParseExact(ReadLine.Read("Enter vacation start date (dd-mm-yy): ",
                                                                       $"{DateTime.Today:dd-MM-yy}"), "dd-mm-yy", CultureInfo.InvariantCulture);

                var leaveApplication = new LeaveApplicationReceived
                {
                    EmpDepartment        = empDepartment,
                    EmpEmail             = empEmail,
                    LeaveDurationInHours = leaveDurationInHours,
                    LeaveStartDateTicks  = leaveStartDate.Ticks
                };
                var partition = new TopicPartition(
                    ApplicationConstants.LeaveApplicationsTopicName,
                    new Partition((int)Enum.Parse <Departments>(empDepartment)));
                var result = await producer.ProduceAsync(partition,
                                                         new Message <string, LeaveApplicationReceived>
                {
                    Key   = $"{empEmail}-{DateTime.UtcNow.Ticks}",
                    Value = leaveApplication
                });

                Console.WriteLine(
                    $"\nMsg: Your leave request is queued at offset {result.Offset.Value} in the Topic {result.Topic}:{result.Partition.Value}\n\n");
            }

            // ReSharper disable once FunctionNeverReturns
        }