public void PublishMessage(Message_Customer message) { var config = KafkaConfigManagement.Instance; var msg = JsonConvert.SerializeObject(message); //using (var producer = new Producer<Null, string>(config.GetConfigProducer(), null, new StringSerializer(Encoding.UTF8))) //{ // producer.ProduceAsync(config.GetTopic, null, msg).GetAwaiter().GetResult(); // //producer.Flush(config.TimeOut); //} using (var producer = new ProducerBuilder <Null, string>(config.GetProducerConfig()).Build()) { producer.ProduceAsync(config.GetTopic, new Message <Null, string> { Value = msg }) .GetAwaiter() .GetResult(); //for (int i = 0; i < 10; i++) //{ // producer.ProduceAsync(new TopicPartition(config.GetTopic, new Partition(i)), new Message<Null, string> // { // Value = msg // }); //} producer.Flush(config.TimeOut); } }
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); } }
public static void Main(string[] args) { var config = new ProducerConfig { BootstrapServers = args[0] }; using (var producer = new ProducerBuilder <string, string>(config).Build()) using (var producer2 = new DependentProducerBuilder <Null, int>(producer.Handle).Build()) { // write (string, string) data to topic "first-topic". producer.ProduceAsync("first-topic", new Message <string, string> { Key = "my-key-value", Value = "my-value" }); // write (null, int) data to topic "second-data" using the same underlying broker connections. producer2.ProduceAsync("second-topic", new Message <Null, int> { Value = 42 }); // producers are not tied to topics. Although it's unusual that you might want to // do so, you can use different producers to write to the same topic. producer2.ProduceAsync("first-topic", new Message <Null, int> { Value = 107 }); // As the Tasks returned by ProduceAsync are not waited on there will still be messages in flight. producer.Flush(TimeSpan.FromSeconds(10)); } }
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 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}"); } }
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)); } }
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 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))); } }
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)); } }
public async Task <IActionResult> ConsultarHotelesAsync([FromBody] ConsultarHoteles model) { try { DateTime dateTimeInicio; DateTime dateTimeFinal; if (!DateTime.TryParseExact(model.FechaInicio, "yyyy'-'MM'-'dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTimeInicio)) { return(BadRequest("Formato de fecha invalido, formato permitido dd/MM/aaaa")); } if (!DateTime.TryParseExact(model.FechaFinal, "yyyy'-'MM'-'dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTimeFinal)) { return(BadRequest("Formato de fecha invalido, formato permitido yyyy-MM-dd")); } var destino = _db.Aeropuertos.FirstOrDefault(c => c.CiudadUbicacin == model.CiudadDestino); if (destino == null) { return(NotFound("No se encontro la ciudad de destino")); } ParametrosDTO parametros = new ParametrosDTO(); parametros.processType = "CATALOG"; parametros.Uuid = model.Uuid; parametros.Tipo_proveedor = "HOTEL"; parametros.Tipo_proceso = "catalogue"; Consulta2 consultaHotel = new Consulta2 { City = destino.CiudadUbicacin, Country = "Colombia", QuantityRooms = "1", RoomType = "Bar", EndDate = model.FechaFinal, StartDate = model.FechaInicio }; parametros.Parametros.hotel.consulta = consultaHotel; string parametrosSerializados = JsonConvert.SerializeObject(parametros); using (var producer = new ProducerBuilder <Null, string>(_config).Build()) { await producer.ProduceAsync("topic-info-reader", new Message <Null, string> { Value = parametrosSerializados }); producer.Flush(TimeSpan.FromSeconds(10)); return(Ok()); } } catch (Exception ex) { Logger.LogError("Excepcion generada en ConsultarVuelos: " + ex.Message); return(StatusCode(500, "Ocurrio un error")); throw; } }
private static async Task ProcessMessage(OrderMessage order) { 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()) { var orderSerialized = JsonConvert.SerializeObject(order); if (order.CountryDestination.Contains("Portugal")) { p.BeginProduce("csw-topic-portugal", new Message <Null, string> { Value = orderSerialized }, handler); } else if (order.CountryDestination.Contains("Espanha")) { p.BeginProduce("csw-topic-espanha", new Message <Null, string> { Value = orderSerialized }, handler); } else { p.BeginProduce("csw-topic", new Message <Null, string> { Value = orderSerialized }, handler); } p.Flush(TimeSpan.FromSeconds(10)); } }
static async Task ProduceSpecific(string bootstrapServers, string schemaRegistryUrl) { using (var schemaRegistry = new CachedSchemaRegistryClient(new SchemaRegistryConfig { SchemaRegistryUrl = schemaRegistryUrl })) using (var producer = new ProducerBuilder <Null, BadgeEvent>(new ProducerConfig { BootstrapServers = bootstrapServers }) .SetValueSerializer(new AvroSerializer <BadgeEvent>(schemaRegistry)) .Build()) { await producer.ProduceAsync("badgeevent", new Message <Null, BadgeEvent> { Value = new BadgeEvent { id = "9", name = "Teacher", userId = "16", displayName = "dragonmantank", reputation = "7636", upVotes = 56, downVotes = 3, processedDate = DateTime.UtcNow.ToString() } }); producer.Flush(TimeSpan.FromSeconds(30)); } }
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()); }
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()); }
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)); }
public async Task <bool> AddAsync(DataLog data) { // TODO pasar a variables de ambiente o setting de la api string kafkaEndpoint = "127.0.0.1:9092"; string kafkaTopic = "registro-log"; var config = new ProducerConfig { BootstrapServers = kafkaEndpoint }; 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>(config).Build()) { await producer.ProduceAsync(kafkaTopic, new Message <Null, string> { Value = data.Mensaje }); producer.Flush(TimeSpan.FromSeconds(10)); } return(true); }
public void Producer_ProduceAsync_Await_Serializing(string bootstrapServers) { LogToFile("start Producer_ProduceAsync_Await_Serializing"); Func <Task> mthd = async() => { using (var producer = new ProducerBuilder <Null, string>(new ProducerConfig { BootstrapServers = bootstrapServers }).Build()) { var dr = await producer.ProduceAsync( singlePartitionTopic, new Message <Null, string> { Value = "test string" }); Assert.Equal(0, producer.Flush(TimeSpan.FromSeconds(10))); Assert.NotEqual(Offset.Unset, dr.Offset); } }; mthd().Wait(); Assert.Equal(0, Library.HandleCount); LogToFile("end Producer_ProduceAsync_Await_Serializing"); }
static async Task ProducerDemoAsync() { IProducer <string, string> producer = new ProducerBuilder <string, string>(new ProducerConfig { BootstrapServers = BootstrapServers, Acks = Acks.Leader, MessageSendMaxRetries = 5, BatchSize = 20, LingerMs = 3000, }).Build(); try { while (true) { DeliveryResult <string, string> deliveryResult = await producer.ProduceAsync(Topic, new Message <string, string> { Key = Guid.NewGuid().ToString(), Value = DateTime.Now.ToString() }); Console.WriteLine($"Producer::{deliveryResult.Key}::{deliveryResult.Value}::{deliveryResult.Partition.Value}::{deliveryResult.Offset.Value}::{Thread.CurrentThread.ManagedThreadId}"); // await Task.Delay(100); } } catch (Exception ex) { Console.WriteLine(ex); } finally { producer.Flush(); producer.Dispose(); } }
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}"); } }
public static void 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()) { // initiate cpp process p.BeginProduce("my-topic", new Message <Null, string> { Value = "start" }, handler); // wait for up to 10 seconds for any inflight messages to be delivered. p.Flush(TimeSpan.FromSeconds(10)); Thread.Sleep(3000); p.BeginProduce("my-topic", new Message <Null, string> { Value = "stop" }, handler); } }
public async Task <bool> Publish(string message) { var config = new ProducerConfig { BootstrapServers = "127.0.0.1:9092" }; Action <DeliveryReport <Null, string> > handler = r => Console.WriteLine(!r.Error.IsError ? $"Message Delivered To {r.TopicPartitionOffset}" : $"Error {r.Error.Reason}"); using (var producer = new ProducerBuilder <Null, string>(config).Build()) { // var strValue = ""; // for(int i =0; i < 100; i++) // { // strValue += $"Sample_{i}_"; // await producer.ProduceAsync("sample-topic", new Message<Null, string>{Value = strValue}); // } var result = await producer.ProduceAsync("testtopic", new Message <Null, string> { Value = message }); Console.WriteLine($"Event {message} sent on Partition: {result.Partition} with Offset: {result.Offset}"); producer.Flush(TimeSpan.FromSeconds(10)); } return(true); }
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}"); } } } }
async static Task ProduceSpecific(string bootstrapServers, string schemaRegistryUrl) { using (var schemaRegistry = new CachedSchemaRegistryClient(new SchemaRegistryConfig { SchemaRegistryUrl = schemaRegistryUrl })) using (var producer = new ProducerBuilder <Null, MessageTypes.LogMessage>(new ProducerConfig { BootstrapServers = bootstrapServers }) .SetValueSerializer(new AvroSerializer <MessageTypes.LogMessage>(schemaRegistry)) .Build()) { await producer.ProduceAsync("log-messages", new Message <Null, MessageTypes.LogMessage> { Value = new MessageTypes.LogMessage { IP = "192.168.0.1", Message = "a test message 2", Severity = MessageTypes.LogLevel.Info, Tags = new Dictionary <string, string> { { "location", "CA" } } } }); producer.Flush(TimeSpan.FromSeconds(30)); } }
/// <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)); }
public void Publish(Message_Customer message) { var config = KafkaConfigManagement.Instance; var msg = JsonConvert.SerializeObject(message); using (var producer = new ProducerBuilder <Null, string>(config.GetProducerConfig()).Build()) { //producer.ProduceAsync(config.GetTopic2, null, msg); //producer.ProduceAsync(config.GetTopic3, null, msg); // publish to trang thai //producer.Flush(config.TimeOut); producer.ProduceAsync(config.GetTopic2, new Message <Null, string> { Value = msg }) .ContinueWith(c => { }); producer.ProduceAsync(config.GetTopic3, new Message <Null, string> { Value = msg }) .ContinueWith(c => { }); producer.Flush(config.TimeOut); } }
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; } }
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)); } }
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)); } }
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)); } } }
static void Main(string[] args) { var config = 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 producer = new ProducerBuilder <Null, string>(config).Build()) { var stringValue = ""; for (int i = 0; i < 5; ++i) { stringValue += "👮🙉� lol" + DateTime.Now.ToLongTimeString(); producer.ProduceAsync("banana-topic", new Message <Null, string> { Value = stringValue }); } producer.Flush(TimeSpan.FromSeconds(10)); } }