public async Task <IActionResult> FireSpecificMessage([FromBody] PublishSingleRequest request) { var res = await _publisher.PublishSpecificAvroMessage(request); return(res == "Ok" ? Ok(new StandardResponse("Successfully published message")) : StatusCode(500, new StandardResponse(res))); }
public async Task <IActionResult> FireMessage([FromBody] PublishSingleRequest request) { var res = await _publisher.PublishToKafka(request.Message, request.Topic, request.Brokers); return(res == "Ok" ? Ok(new StandardResponse("Successfully published message")) : StatusCode(500, new StandardResponse(res))); }
public async Task <string> PublishGenericAvroMessage(PublishSingleRequest request) { var schema = (RecordSchema)RecordSchema.Parse( @"{ ""namespace"": ""Kafka.Workshop.Avro"", ""type"": ""record"", ""name"": ""JustAMessage"", ""fields"": [ {""name"": ""title"", ""type"": ""string""}, {""name"": ""message"", ""type"": ""string""} ] }" ); var cts = new CancellationTokenSource(); using (var schemaRegistry = new CachedSchemaRegistryClient(new SchemaRegistryConfig { Url = _schemaRegistryUrl })) using (var producer = new ProducerBuilder <string, GenericRecord>(new ProducerConfig { BootstrapServers = _defaultBrokerString, SocketTimeoutMs = 5000, MessageTimeoutMs = 3000, RequestTimeoutMs = 3000, }) .SetKeySerializer(new AvroSerializer <string>(schemaRegistry)) .SetValueSerializer(new AvroSerializer <GenericRecord>(schemaRegistry)) .Build()) { var record = new GenericRecord(schema); record.Add("title", request.Message.Title); record.Add("message", request.Message.Message); var res = await producer .ProduceAsync(request.Topic, new Message <string, GenericRecord> { Key = "producer", Value = record }) .ContinueWith(task => task.IsFaulted ? $"Error producing message {task.Exception.Message}" : $"Message sent to: {task.Result.TopicPartitionOffset}", cts.Token); return(res); } }
public async Task <string> PublishSpecificAvroMessage(PublishSingleRequest request) { var cts = new CancellationToken(); using (var schemaRegistry = new CachedSchemaRegistryClient(new SchemaRegistryConfig { Url = _schemaRegistryUrl, RequestTimeoutMs = 5000, MaxCachedSchemas = 10 })) using (var producer = new ProducerBuilder <string, MessageModel>(new ProducerConfig { BootstrapServers = _defaultBrokerString, SocketTimeoutMs = 5000, MessageTimeoutMs = 3000, RequestTimeoutMs = 3000, }) .SetKeySerializer(new AvroSerializer <string>(schemaRegistry)) .SetValueSerializer(new AvroSerializer <MessageModel>(schemaRegistry)) .SetErrorHandler((_, e) => Console.WriteLine($"Error in Kafka: {e.Reason}")) .Build()) { var msg = new MessageModel { Title = request.Message.Title, Message = request.Message.Message, CorrelationId = Guid.NewGuid().ToString(), TransactionTs = DateTime.Now.ToString() }; Console.WriteLine($"Pushing message to kafka. {JsonConvert.SerializeObject(msg)}"); var res = await producer.ProduceAsync(request.Topic, new Message <string, MessageModel> { Key = "Producer", Value = msg }) .ContinueWith(task => task.IsFaulted ? $"error producing message: {task.Exception.Message}" : $"produced to: {task.Result.TopicPartitionOffset}", cts); return(res); } }
public async Task <bool> Publish(PublishSingleRequest request) { var response = await this.PublishToKafka(request.Message); return(response); }
public async Task <IActionResult> FireMessage([FromBody] PublishSingleRequest request) { return(await _publisher.Publish(request) ? Ok("Successfully published message") : StatusCode(500, "Failed to deliver message to Kafka servers. Ensure the servers are available")); }