示例#1
0
        private static void Produce()
        {
            Console.WriteLine("Produce -----");

            var producer = new ProducerBuilder <string, string>(
                new AdminClientConfig()
            {
                BootstrapServers = "localhost:29092"
            })
                           .Build();

            producer.Produce("partitioned-topic", new Message <string, string> {
                Key = "fred", Value = "flintstone"
            });
            producer.Produce("partitioned-topic", new Message <string, string> {
                Key = "wilma", Value = "flintstone"
            });

            var h = new Headers();

            h.Add("age", new byte[] { 0 });
            producer.Produce("partitioned-topic", new Message <string, string> {
                Key = "bambam", Value = "flintstone", Headers = h
            });
        }
        public void Transactions_Abort(string bootstrapServers)
        {
            LogToFile("start Transactions_Abort");

            var defaultTimeout = TimeSpan.FromSeconds(30);

            using (var topic = new TemporaryTopic(bootstrapServers, 1))
                using (var producer = new ProducerBuilder <string, string>(new ProducerConfig {
                    BootstrapServers = bootstrapServers, TransactionalId = Guid.NewGuid().ToString()
                }).Build())
                {
                    producer.InitTransactions(defaultTimeout);
                    producer.BeginTransaction();
                    producer.Produce(topic.Name, new Message <string, string> {
                        Key = "test key 0", Value = "test val 0"
                    });
                    Thread.Sleep(1000); // ensure the abort ctrl message makes it into the log.
                    producer.AbortTransaction(defaultTimeout);
                    producer.BeginTransaction();
                    producer.Produce(topic.Name, new Message <string, string> {
                        Key = "test key 1", Value = "test val 1"
                    });
                    producer.CommitTransaction(defaultTimeout);

                    using (var consumer = new ConsumerBuilder <string, string>(new ConsumerConfig {
                        IsolationLevel = IsolationLevel.ReadCommitted, BootstrapServers = bootstrapServers, GroupId = "unimportant", EnableAutoCommit = false, Debug = "all"
                    }).Build())
                    {
                        consumer.Assign(new TopicPartitionOffset(topic.Name, 0, 0));

                        var cr1 = consumer.Consume();
                        var cr2 = consumer.Consume(TimeSpan.FromMilliseconds(100)); // force the consumer to read over the final control message internally.
                        Assert.Equal(2, cr1.Offset);                                // there should be skipped offsets due to the aborted txn and commit marker in the log.
                        Assert.Null(cr2);                                           // control message should not be exposed to application.
                    }

                    using (var consumer = new ConsumerBuilder <string, string>(new ConsumerConfig {
                        IsolationLevel = IsolationLevel.ReadUncommitted, BootstrapServers = bootstrapServers, GroupId = "unimportant", EnableAutoCommit = false, Debug = "all"
                    }).Build())
                    {
                        consumer.Assign(new TopicPartitionOffset(topic.Name, 0, 0));

                        var cr1 = consumer.Consume();
                        var cr2 = consumer.Consume();
                        var cr3 = consumer.Consume(TimeSpan.FromMilliseconds(100)); // force the consumer to read over the final control message internally.
                        Assert.Equal(0, cr1.Offset);                                // the aborted message should not be skipped.
                        Assert.Equal(2, cr2.Offset);                                // there should be a skipped offset due to a commit marker in the log.
                        Assert.Null(cr3);                                           // control message should not be exposed to application.
                    }
                }

            Assert.Equal(0, Library.HandleCount);
            LogToFile("end   Transactions_Abort");
        }
示例#3
0
        public static void useProduce()
        {
            Console.WriteLine("useProduce");
            var conf = new ProducerConfig {
                BootstrapServers = "localhost:9092"
            };

            Action <DeliveryReport <Null, string> > handler = r =>
                                                              Console.WriteLine(!r.Error.IsError
                                        ? $"Delivered message to {r.TopicPartitionOffset}"
                                        : $"Delivery Error: {r.Error.Reason}");

            using (var p = new ProducerBuilder <Null, string>(conf).Build())
            {
                for (int i = 0; i < 100; ++i)
                {
                    Console.WriteLine(i);
                    p.Produce("testTopicName", new Message <Null, string> {
                        Value = i.ToString()
                    }, handler);
                }

                // wait for up to 10 seconds for any inflight messages to be delivered.
                p.Flush(TimeSpan.FromSeconds(10));
            }
        }
        static void Main(string[] args)
        {
            var config = new ProducerConfig {
                BootstrapServers = "localhost:9092"
            };

            Action <DeliveryReport <Null, string> > handler = r => Console.WriteLine(
                !r.Error.IsError
                    ? $"Message delivered :) to {r.TopicPartitionOffsetError}"
                    : $"Message delivery failed :( awwww REASON? -- {r.Error.Reason}");

            using (var producer = new ProducerBuilder <Null, string>(config).Build())
            {
                for (var i = 0; i < 100; i++)
                {
                    producer.Produce("sample-topic", new Message <Null, string>()
                    {
                        Value = $"Publisher message - {i}"
                    }, handler);
                    Thread.Sleep(1000);
                }

                producer.Flush(TimeSpan.FromSeconds(10));
            }
        }
    public void SendMessage()
    {
        try
        {
            var bootstrap = Environment.GetEnvironmentVariable("BROKER_BOOTSTRAP");
            var config    = new ProducerConfig
            {
                BootstrapServers = bootstrap,
                ClientId         = $"{Dns.GetHostName()}_NeoVirtFS",
            };

            using (var producer = new ProducerBuilder <string, string>(config).Build())
            {
                // We'll have to add schema control (and supposedly have to use newtonsoftjson since it can't handle these for schemas)

                string jsonString = JsonSerializer.Serialize(this, options: new JsonSerializerOptions
                {
                    WriteIndented          = true,
                    NumberHandling         = JsonNumberHandling.AllowReadingFromString,
                    DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault
                });

                producer.Produce("neofiles", new Message <string, string> {
                    Key = MessageType, Value = jsonString
                });
                Console.WriteLine($"Send message {jsonString}");
                producer.Flush();
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"SendMessage Error: {ex.Message}");
        }
    }
示例#6
0
        static void Main(string[] args)
        {
            var config = new ProducerConfig {
                BootstrapServers = "localhost:9092"
            };

            // If serializers are not specified, default serializers from
            // `Confluent.Kafka.Serializers` will be automatically used where
            // available. Note: by default strings are encoded as UTF8.

            void DeliveryHandler(DeliveryReport <Null, int> r)
            {
                Console.WriteLine(!r.Error.IsError
                    ? $"Delivered message {r.Message.Value} to {r.TopicPartitionOffset}"
                    : $"Delivery Error: {r.Error.Reason}");
            }

            using (var p = new ProducerBuilder <Null, int>(config).Build())
            {
                var count  = 0;
                var random = new Random();

                while (true)
                {
                    p.Produce("test-topic", new Message <Null, int> {
                        Value = count++
                    }, DeliveryHandler);
                    Thread.Sleep(random.Next(_randomRange.Item1, _randomRange.Item2));
                }
            }
        }
示例#7
0
        static void Produce(string topic, ClientConfig config)
        {
            using (var producer = new ProducerBuilder <string, string>(config).Build())
            {
                int numProduced = 0;
                int numMessages = 100;
                for (int i = 0; i < numMessages; ++i)
                {
                    var key = "alice";
                    var val = JObject.FromObject(new { count = i }).ToString(Formatting.None);

                    Console.WriteLine($"Producing record: {key} {val}");

                    producer.Produce(topic, new Message <string, string> {
                        Key = key, Value = val
                    },
                                     (deliveryReport) =>
                    {
                        if (deliveryReport.Error.Code != ErrorCode.NoError)
                        {
                            Console.WriteLine($"Failed to deliver message: {deliveryReport.Error.Reason}");
                        }
                        else
                        {
                            Console.WriteLine($"Produced message to: {deliveryReport.TopicPartitionOffset}");
                            numProduced += 1;
                        }
                    });
                }

                producer.Flush(TimeSpan.FromSeconds(10));

                Console.WriteLine($"{numProduced} messages were produced to topic {topic}");
            }
        }
示例#8
0
        public Task <bool> CanConnectKafka(CancellationToken cancellationToken = default)
        {
            var config = new ProducerConfig
            {
                BootstrapServers = _kafkaOptions.BootstrapServers,
                ClientId         = $"{_kafkaOptions.ClientId} - {Dns.GetHostName()}",
            };

            using (var producer = new ProducerBuilder <Null, string>(config).Build())
            {
                try
                {
                    var kafkaEvent = KafkaEvent.HealthCheckEvent();
                    _logger.Debug("Sending Message ...");
                    producer.Produce("HealthCheck.Sent", new Message <Null, string> {
                        Value = JsonConvert.SerializeObject(kafkaEvent)
                    }, ProducerHandler);
                    producer.Flush();
                    _logger.Debug("... Message Produced");
                }
                catch (Exception ex)
                {
                    Task.FromResult(false);
                }
            }
            return(Task.FromResult(true));
        }
示例#9
0
        public void ProduceData(ReportRequest report)
        {
            try
            {
                var conf = new ProducerConfig {
                    BootstrapServers = "localhost:9092"
                };

                Action <DeliveryReport <Null, string> > handler = r =>
                                                                  Console.WriteLine(!r.Error.IsError
                        ? $"Delivered message to {r.TopicPartitionOffset}"
                        : $"Delivery Error: {r.Error.Reason}");

                using (var p = new ProducerBuilder <Null, string>(conf).Build())
                {
                    for (int i = 0; i < 100; ++i)
                    {
                        p.Produce("userReport", new Message <Null, string> {
                            Value = report.UUID
                        }, handler);
                    }

                    // wait for up to 10 seconds for any inflight messages to be delivered.
                    p.Flush(TimeSpan.FromSeconds(10));
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
示例#10
0
        private static void Produce(string topic, int numMessages, ClientConfig config, Action <DeliveryReport <string, string> > deliveryHandler, bool isTombstone)
        {
            using (var producer = new ProducerBuilder <string, string>(config).Build())
            {
                for (var i = 0; i < numMessages; ++i)
                {
                    var messageNumber = Interlocked.Increment(ref _messageNumber);
                    var hasHandler    = deliveryHandler is not null;
                    var key           = $"{messageNumber}-Sync-{hasHandler}{(isTombstone ? "-tombstone" : "")}";
                    var value         = isTombstone ? null : GetMessage(i, isProducedAsync: false);
                    var message       = new Message <string, string> {
                        Key = key, Value = value
                    };

                    Console.WriteLine($"Producing record {i}: {message.Key}...");

                    producer.Produce(topic, message, deliveryHandler);

                    if (numMessages % FlushInterval == 0)
                    {
                        producer.Flush(FlushTimeout);
                    }
                }
                Flush(producer);

                Console.WriteLine($"Finished producing {numMessages} messages to topic {topic}");
            }
        }
示例#11
0
        static void Main(string[] args)
        {
            var conf = new ProducerConfig()
            {
                BootstrapServers = "localhost:9092",
            };

            Action <DeliveryReport <Null, string> > handler = r =>
            {
                var conLog = r.Error.IsError ? $"Delivery error: {r.Error.Reason}" : $"Delivered message to {r.TopicPartitionOffset}";
                Console.WriteLine(conLog);
            };

            using (var p = new ProducerBuilder <Null, string>(conf).Build())
            {
                for (var i = 0; i < 100; i++)
                {
                    var msg = new Message <Null, string>()
                    {
                        Value = $"haha,{i}",
                    };
                    p.Produce("my-topic", msg, handler);
                }

                p.Flush(TimeSpan.FromSeconds(10));
            }
        }
        public void Produce <T>(string msgKey, T msgVal, string topic = "retry-transient-error")
        {
            Logger.LogThreadInfo("Produce() method");

            using var producer = new ProducerBuilder <string, string>(config).Build();

            var val = JsonConvert.SerializeObject(msgVal);

            Logger.LogInfo($"Producing record: {val}");

            producer.Produce(
                topic,

                new Message <string, string>
            {
                Key = msgKey, Value = val
            },

                Handler
                );

            producer.Flush(TimeSpan.FromSeconds(10));

            Console.WriteLine($"{msgKey} message produced to topic {topic}");
        }
示例#13
0
        static void Main(string[] args)
        {
            Console.WriteLine("Producer");
            var config = new ProducerConfig
            {
                BootstrapServers = ConfigurationManager.AppSettings["BootstrapServers"].Trim().ToString()
            };
            var topic = ConfigurationManager.AppSettings["topic"].Trim().ToString();

            Console.Write("Total message: ");
            var totalMsg = int.Parse(Console.ReadLine());


            using (var producer = new ProducerBuilder <Null, string>(config).Build())
            {
                #region old
                //try
                //{
                //    var dr = await p.ProduceAsync("first_topic", new Message<string, string> { Key = "tuong", Value = Newtonsoft.Json.JsonConvert.SerializeObject(new { foo = "bar 2" }) });
                //    Console.WriteLine($"Delivered '{dr.Value}' to '{dr.TopicPartitionOffset}'");
                //}
                //catch (ProduceException<Null, string> e)
                //{
                //    Console.WriteLine($"Delivery failed: {e.Error.Reason}");
                //}
                #endregion

                int numProduced = 0;
                //int numMessages = 10;
                for (int i = 0; i < totalMsg; ++i)
                {
                    //var key = "key"+i.ToString();
                    var val = Newtonsoft.Json.JsonConvert.SerializeObject(new { foo = "tuong: " + i.ToString() });

                    Console.WriteLine($"Producing record: {val}");

                    producer.Produce(topic, new Message <Null, string> {
                        Value = val
                    },
                                     (deliveryReport) =>
                    {
                        if (deliveryReport.Error.Code != ErrorCode.NoError)
                        {
                            Console.WriteLine($"Failed to deliver message: {deliveryReport.Error.Reason}");
                        }
                        else
                        {
                            Console.WriteLine($"Produced message to: {deliveryReport.TopicPartitionOffset}");
                            numProduced += 1;
                        }
                    });
                    //Thread.Sleep(1000);
                }
                // wait for up to 10 seconds for any inflight messages to be delivered.
                producer.Flush(TimeSpan.FromSeconds(10));
                Console.WriteLine($"{numProduced} messages were produced to topic {topic}");
            }

            Console.ReadKey();
        }
示例#14
0
        public void Transactions_SendOffsets(string bootstrapServers)
        {
            LogToFile("start Transactions_SendOffsets");

            var defaultTimeout = TimeSpan.FromSeconds(30);

            var groupName = Guid.NewGuid().ToString();

            using (var topic = new TemporaryTopic(bootstrapServers, 1))
                using (var producer = new ProducerBuilder <string, string>(new ProducerConfig {
                    BootstrapServers = bootstrapServers, TransactionalId = Guid.NewGuid().ToString()
                }).Build())
                    using (var consumer = new ConsumerBuilder <string, string>(new ConsumerConfig {
                        IsolationLevel = IsolationLevel.ReadCommitted, BootstrapServers = bootstrapServers, GroupId = groupName, EnableAutoCommit = false, Debug = "all"
                    }).Build())
                    {
                        producer.InitTransactions(defaultTimeout);
                        producer.BeginTransaction();
                        producer.Produce(topic.Name, new Message <string, string> {
                            Key = "test key 0", Value = "test val 0"
                        });
                        producer.SendOffsetsToTransaction(new List <TopicPartitionOffset> {
                            new TopicPartitionOffset(topic.Name, 0, 7324)
                        }, consumer.ConsumerGroupMetadata, TimeSpan.FromSeconds(30));
                        producer.CommitTransaction(defaultTimeout);
                        var committed = consumer.Committed(new List <TopicPartition> {
                            new TopicPartition(topic.Name, 0)
                        }, TimeSpan.FromSeconds(30));
                        Assert.Single(committed);
                        Assert.Equal(7324, committed[0].Offset);
                    }

            Assert.Equal(0, Library.HandleCount);
            LogToFile("end   Transactions_SendOffsets");
        }
        public override bool Send(string val, List <Variable> vars)
        {
            var config = new ProducerConfig {
                BootstrapServers = bootStrapServers
            };

            foreach (Variable v in vars)
            {
                try {
                    Key = Key.Replace(v.token, v.value);
                } catch (Exception) {
                    // NO-OP
                }
                try {
                    Topic = Topic.Replace(v.token, v.value);
                } catch (Exception) {
                    // NO-OP
                }

                using (var p = new ProducerBuilder <string, string>(config).Build()) {
                    try {
                        p.Produce(Topic, new Message <string, string> {
                            Key = Key, Value = val
                        });
                    } catch (Exception e) {
                        logger.Error(e.Message);
                        logger.Error(e);
                        logger.Error($"Unable to deliver to Kafka Server on topic {Topic}");
                        return(false);
                    }
                    logger.Info($"Kafka sent message to {Topic}");
                }
            }
            return(true);
        }
示例#16
0
        public static void Main(string[] args)
        {
            var conf = new ProducerConfig {
                BootstrapServers = "localhost:9092"
            };

            void Handler(DeliveryReport <Null, string> r) =>
            Console.WriteLine(!r.Error.IsError
                    ? $"Delivered message to {r.TopicPartitionOffset}"
                    : $"Delivery Error: {r.Error.Reason}");

            using (var p = new ProducerBuilder <Null, string>(conf).Build())
            {
                Console.WriteLine("Hi, I am producer.");

                while (true)
                {
                    try
                    {
                        var mess = Console.ReadLine();
                        if (!string.IsNullOrWhiteSpace(mess))
                        {
                            p.Produce("timemanagement_booking", new Message <Null, string> {
                                Value = mess
                            }, Handler);
                            p.Flush(TimeSpan.FromSeconds(10));
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine($"Error occured: {e.Message}");
                    }
                }
            }
        }
示例#17
0
        /// <inheritdoc cref="IProducer.ProduceMany"/>
        public void ProduceMany(IList <Event> events)
        {
            using var schemaRegistry  = new CachedSchemaRegistryClient(_schemaRegistryConfig);
            using var producerBuilder = new ProducerBuilder <string, Event>(_producerConfig)
                                        .SetKeySerializer(new AvroSerializer <string>(schemaRegistry))
                                        .SetValueSerializer(new AvroSerializer <Event>(schemaRegistry))
                                        .SetErrorHandler((_, error) => _logger.LogError("Kafka encountered an error: {@Error}", error))
                                        .Build();
            foreach (var @event in events)
            {
                var message = new Message <string, Event>
                {
                    Key   = @event.AggregateName,
                    Value = @event
                };

                producerBuilder.Produce(@event.AggregateName, message, report =>
                {
                    if (report.Error.IsFatal)
                    {
                        throw new ProducerException("Fatal error producing message to Kafka: " +
                                                    $"ErrorCode [{report.Error.Code}] | " +
                                                    $"Reason [{report.Error.Reason}]");
                    }
                });
            }
            // wait for up to 5 seconds for any inflight messages to be delivered.
            producerBuilder.Flush(TimeSpan.FromSeconds(10));
        }
示例#18
0
        static void Main(string[] args)
        {
            var config = new ProducerConfig {
                BootstrapServers = "localhost:9092"
            };

            using var producer1 = new ProducerBuilder <string, Game>(config).SetValueSerializer(new GameSerializer()).Build();

Label:
            for (int i = 0; i < 100; i++)
            {
                producer1.Produce(TopicName, new Message <string, Game>()
                {
                    Key   = "SKIP",
                    Value = new Game()
                    {
                        GameId = i, Title = $"Title{i}"
                    }
                });
                producer1.Flush();
            }
            Console.WriteLine("Type to resend 100 messages");
            Console.ReadLine();
            goto Label;
        }
示例#19
0
        static void Main(string[] args)
        {
            Console.WriteLine("Kafka Producer");
            Console.WriteLine("==============\n");

            var conf = new ProducerConfig {
                BootstrapServers = "localhost:9092"
            };

            Action <DeliveryReport <Null, string> > handler = r =>
                                                              Console.WriteLine(!r.Error.IsError
                    ? $"Delivered message to {r.TopicPartitionOffset}"
                    : $"Delivery Error: {r.Error.Reason}");

            // If serializers are not specified, default serializers from
            // `Confluent.Kafka.Serializers` will be automatically used where
            // available. Note: by default strings are encoded as UTF8.
            using (var p = new ProducerBuilder <Null, string>(conf).Build())
            {
                for (int i = 0; i < 100; ++i)
                {
                    p.Produce("my-topic", new Message <Null, string> {
                        Value = i.ToString()
                    }, handler);
                }

                // wait for up to 10 seconds for any inflight messages to be delivered.
                p.Flush(TimeSpan.FromSeconds(10));
            }
        }
        static void Main(string[] args)
        {
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

            // Console.WriteLine("Hello World!");
            ProducerConfig config = new ProducerConfig()
            {
                BootstrapServers = "172.20.90.54:9092"
            };

            using (var productor = new ProducerBuilder <Null, string>(config).Build())
            {
                Random random    = new Random();
                var    cancelled = false;
                System.Console.CancelKeyPress += (_, e) =>
                {
                    e.Cancel  = true; // prevent the process from terminating.
                    cancelled = true;
                };
                while (!cancelled)
                {
                    var student = new CollegeStudents()
                    {
                        ClassName = "三年级一班", Counselor = ChineseNameGenerater.GetChineseName(), Name = ChineseNameGenerater.GetChineseName(), Address = "浙江省杭州市", Age = random.Next(20, 100)
                    };
                    System.Console.WriteLine($"---->{student.ToString()}");
                    productor.Produce(topic: nameof(CollegeStudents), new Message <Null, string>()
                    {
                        Value = student.ToString()
                    });
                    Thread.Sleep(1000);
                }
            }
        }
示例#21
0
        public void Publish <T>(string topic, T value, Action successful = null, Action fail = null)
        {
            using (var p = new ProducerBuilder <Null, string>(_producerConfig).Build())
            {
                for (var i = 0; i < 100; i++)
                {
                    p.Produce(topic, new Message <Null, string> {
                        Value = i.ToString()
                    }, r =>
                    {
                        if (!r.Error.IsError)
                        {
                            successful();
                        }
                        else
                        {
                            fail();
                        }
                    });
                }

                // wait for up to 10 seconds for any inflight messages to be delivered.
                p.Flush(TimeSpan.FromSeconds(10));
            }
        }
示例#22
0
        public IEnumerable <WeatherForecast> Get()
        {
            var rng    = new Random();
            var config = new ProducerConfig
            {
                BootstrapServers = "localhost:9092"
            };

            using (var producer = new ProducerBuilder <Null, string>(config).Build())
            {
                producer.Produce("chat-message", new Message <Null, string>()
                {
                    Value = DateTime.Now.ToString()
                });
                producer.Flush();
            }



            return(Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            })
                   .ToArray());
        }
示例#23
0
        private static void SendBulkMessage(ProducerConfig conn, int numberOfMessages)
        {
            Console.Clear();
            Console.WriteLine($"Sending {numberOfMessages} messages");

            Action <DeliveryReport <Null, string> > handler = r =>
                                                              Console.WriteLine(!r.Error.IsError
                                                        ? $"Delivered message to {r.TopicPartitionOffset}"
                                                        : $"Delivery Error: {r.Error.Reason}");

            using (var producer = new ProducerBuilder <Null, string>(conn).Build())
            {
                for (int i = 1; i <= numberOfMessages; ++i)
                {
                    var messageJson = CreateMessageJson(i);

                    producer.Produce("test", new Message <Null, string> {
                        Value = messageJson
                    }, handler);
                }

                // wait for up to 10 seconds for any inflight messages to be delivered.
                producer.Flush(TimeSpan.FromSeconds(10));
            }
        }
        public IActionResult Post(TopicMessageModel model)
        {
            var config = new ProducerConfig
            {
                BootstrapServers = _kafkaOptions.BootstrapServers,
                ClientId         = $"{_kafkaOptions.ClientId} - {Dns.GetHostName()}",
            };

            using (var producer = new ProducerBuilder <Null, string>(config).Build())
            {
                try
                {
                    var kafkaEvent = new KafkaEvent(model.Message);
                    _logger.Debug("Sending Message ...");
                    producer.Produce(model.Topic, new Message <Null, string> {
                        Value = JsonConvert.SerializeObject(kafkaEvent)
                    }, ProducerHandler);
                    producer.Flush();
                    _logger.Debug("... Message Produced");
                }
                catch (Exception ex)
                {
                    return(BadRequest(ex.Message));
                }
            }

            return(Accepted());
        }
        static void Produce(string topic, ClientConfig config)
        {
            using (var producer = new ProducerBuilder <string, string>(config).Build())
            {
                int numProduced = 0;
                int numMessages = 10;
                for (int i = 0; i < numMessages; ++i)
                {
                    var key = "alice";
                    var val = "This is an exciting message.  Number" + i + " of " + numMessages + ".  It was created " + DateTime.Now;

                    Console.WriteLine($"Producing record: {key} {val}");

                    producer.Produce(topic, new Message <string, string> {
                        Key = key, Value = val
                    },
                                     (deliveryReport) =>
                    {
                        if (deliveryReport.Error.Code != ErrorCode.NoError)
                        {
                            Console.WriteLine($"Failed to deliver message: {deliveryReport.Error.Reason}");
                        }
                        else
                        {
                            Console.WriteLine($"Produced message to: {deliveryReport.TopicPartitionOffset}");
                            numProduced += 1;
                        }
                    });
                }

                producer.Flush(TimeSpan.FromSeconds(10));

                Console.WriteLine($"{numProduced} messages were produced to topic {topic}");
            }
        }
        public void SimpleProduce(string topic, List <Message <Null, string> > messages)
        {
            var producerConfig = new ProducerConfig {
                BootstrapServers = this.Brokers
            };

            void handler(DeliveryReport <Null, string> r)
            {
                if (!r.Error.IsError)
                {
                    Console.WriteLine($"Delivered message to {r.TopicPartitionOffset}");
                }
                else
                {
                    Console.WriteLine($"Delivery Error: {r.Error.Reason}");
                }
            }

            using (var producer = new ProducerBuilder <Null, string>(producerConfig).Build()) {
                foreach (var message in messages)
                {
                    producer.Produce(topic, message, handler);
                }
                // wait for up to 10 seconds for any inflight messages to be delivered.
                producer.Flush(TimeSpan.FromSeconds(1 + (messages.Count / 10)));
            }
        }
示例#27
0
        public int Produce(ClientConfig config, string topic)
        {
            _logger.Info($"  Produce to the '{topic}' topic: ");
            using (var producer = new ProducerBuilder <string, string>(config).Build())
            {
                int numProduced = 0;
                for (int i = 0; i < _numMessages; ++i)
                {
                    var val = JObject.FromObject(new { count = i }).ToString(Formatting.None);
                    //_logger.Info($"Producing record: {_key} {val}");
                    producer.Produce(topic, new Message <string, string> {
                        Key = _key, Value = val
                    },
                                     (deliveryReport) =>
                    {
                        if (deliveryReport.Error.Code != ErrorCode.NoError)
                        {
                            _logger.Error($"Failed to deliver message: {deliveryReport.Error.Reason}");
                        }
                        else
                        {
                            _logger.Info($"produced message to: {deliveryReport.TopicPartitionOffset}");
                            numProduced += 1;
                        }
                    });
                }
                producer.Flush(TimeSpan.FromSeconds(10));

                _logger.Info($"{numProduced} messages were produced to topic {topic}");
                return(numProduced);
            }
        }
示例#28
0
        public static async Task Main(string[] args)
        {
            var conf = new ProducerConfig {
                BootstrapServers = "localhost:9092"
            };

            Action <DeliveryReport <Null, string> > handler = r =>
                                                              Console.WriteLine(!r.Error.IsError
                    ? $"Delivered message to {r.TopicPartitionOffset}"
                    : $"Delivery Error: {r.Error.Reason}");

            using (var p = new ProducerBuilder <Null, string>(conf).Build())
            {
                for (int i = 0; i < 100; ++i)
                {
                    //Enter the name of the topic, and start producing

                    p.Produce("myTopic", new Message <Null, string> {
                        Value = "What " + i.ToString()
                    }, handler);
                }

                // wait for up to 10 seconds for any inflight messages to be delivered.
                p.Flush(TimeSpan.FromSeconds(10));
            }
        }
示例#29
0
    public static void Main(string[] args)
    {
        var conf = new ProducerConfig
        {
            //BootstrapServers = "localhost:9092",
            BootstrapServers = "172.20.2.8:9092",
        };
        string val = string.Empty;

        while (val != "test")
        {
            Console.WriteLine("Producer -->");
            val = Console.ReadLine();
            //Action<DeliveryReport<Null, string>> handler = r =>
            //    Console.WriteLine(!r.Error.IsError
            //        ? $"Delivered message to {r.TopicPartitionOffset}"
            //        : $"Delivery Error: {r.Error.Reason}");

            using (var p = new ProducerBuilder <Null, string>(conf).Build())
            {
                p.Produce("my-topic", new Message <Null, string> {
                    Value = val
                });
                //p.Produce("my-topic", new Message<Null, string> { Value = val }, handler);


                // wait for up to 10 seconds for any inflight messages to be delivered.
                p.Flush(TimeSpan.FromSeconds(10));
            }
        }
    }
示例#30
0
        private static void Main(string[] args)
        {
            Console.WriteLine("Hello Kafka Numbers Producer!");

            var config = new ProducerConfig
            {
                BootstrapServers = "localhost:9092"
            };

            using var producer = new ProducerBuilder <string, string>(config).Build();

            while (true)
            {
                var input = Console.ReadKey().KeyChar.ToString();

                try
                {
                    producer.Produce(
                        "topic-numbers",
                        new Message <string, string>
                    {
                        Value   = input,
                        Key     = input,
                        Headers = new Headers
                        {
                            new Header("bragajs", Encoding.UTF8.GetBytes("bragajs kafka"))
                        }
                    });
                }
                catch (ProduceException <Null, string> e)
                {
                    Debug.WriteLine($"Delivery failed: {e.Error.Reason}");
                }
            }
        }