Example #1
0
        public static List <T> GenerateData <T>()
        {
            var myJsonString = File.ReadAllText("..\\..\\..\\CarsData.json");
            var myJsonObject = JsonConvert.DeserializeObject <T>(myJsonString);

            Console.Out.WriteLine("myJsonObject datata {0}", ObjectDumper.Dump(myJsonObject));

            //List<T> objectData = JsonConvert.DeserializeObject<List<T>>(myJsonString);
            //return objectData;
            return(JsonConvertObject.ToObjectData <T>(myJsonString));
        }
Example #2
0
        public (List <string>, List <T>) CreateConsumer <T>()
        {
            List <T> objReceived = new List <T>();

            List <string> receivedMessages = new List <string>();

            var consumerConfig = new ConsumerConfig
            {
                BootstrapServers     = kafkaSettings.BootstrapServers,
                GroupId              = kafkaSettings.GroupId,
                AutoOffsetReset      = kafkaSettings.AutoOffsetReset,
                EnableAutoCommit     = kafkaSettings.EnableAutoCommit,
                AutoCommitIntervalMs = kafkaSettings.AutoCommitIntervalMs,
                SessionTimeoutMs     = kafkaSettings.SessionTimeoutMs,
            };

            CancellationTokenSource cancToken = new CancellationTokenSource();

            cancToken.CancelAfter((int)consumerConfig.SessionTimeoutMs);

            //Create the Consumer
            using (var consumer = new ConsumerBuilder <Ignore, string>(consumerConfig).Build())
            {
                try
                {
                    //Subscribe to the Kafka topic
                    consumer.Subscribe(new List <string>()
                    {
                        kafkaSettings.Topic
                    });


                    while (!cancToken.IsCancellationRequested)
                    {
                        if (consumer != null)
                        {
                            try
                            {
                                var result = consumer.Consume(cancToken.Token);

                                //using Car object
                                objReceived.Add(JsonConvertObject.ToSingleObjectData <T>(result.Message.Value));

                                //using List
                                consumedMessages.Add(result.Message.Value);


                                Console.Out.WriteLine("consumedMessages Data {0}", ObjectDumper.Dump(consumedMessages));

                                //check for duplicate list
                                receivedMessages = consumedMessages.Distinct().ToList();
                            }
                            catch (OperationCanceledException consumeCanceled)
                            {
                                consumer.Unsubscribe();

                                consumer.Close();

                                Console.Out.WriteLine(" Cancellation Resquested - consumer Unsubscribe - consumer Close ");
                            }
                            catch (ConsumeException consumeError)
                            {
                                Console.Out.WriteLine(" Consume error {0}", consumeError);
                            }
                        }
                    }
                }

                catch (Exception ex)
                {
                    Console.Out.WriteLine("Subscribe error {0}", ex);
                }
            }

            return(consumedMessages, objReceived);
        }