WIP
This commit is contained in:
@ -4,62 +4,43 @@ using System.Threading.Tasks;
|
||||
using Confluent.Kafka;
|
||||
using Serializers;
|
||||
using Model;
|
||||
using Publisher;
|
||||
|
||||
class Producer
|
||||
{
|
||||
public static string TOPIC_NAME = "test-topic";
|
||||
public static async Task Main (string[] args)
|
||||
{
|
||||
var config = new ProducerConfig { BootstrapServers = "localhost:29092" };
|
||||
string bootstrapServers = "localhost:29092";
|
||||
var DataPublisher = new DataPublisher(TOPIC_NAME, bootstrapServers);
|
||||
|
||||
string introText = "\nType a string of text then press Enter. Type '+' anywhere in the text to quit:\n";
|
||||
var input = "";
|
||||
|
||||
using (var producer = new ProducerBuilder<string, DataModel>(config)
|
||||
.SetValueSerializer(new JsonSerializer<DataModel>())
|
||||
.Build())
|
||||
Console.WriteLine(introText);
|
||||
do
|
||||
{
|
||||
|
||||
Console.WriteLine(introText);
|
||||
do
|
||||
{
|
||||
input = Console.ReadLine();
|
||||
try
|
||||
{
|
||||
if (input != String.Empty)
|
||||
{
|
||||
SendMessage(producer, input);
|
||||
}
|
||||
|
||||
}
|
||||
catch (OverflowException e)
|
||||
{
|
||||
Console.WriteLine("An error has occurred", e.Message, input);
|
||||
Console.WriteLine(introText);
|
||||
}
|
||||
} while (input != "+");
|
||||
}
|
||||
}
|
||||
|
||||
private static async void SendMessage(IProducer<String, DataModel> producer, string str)
|
||||
{
|
||||
try
|
||||
{
|
||||
var msg = new Message<string, DataModel>
|
||||
input = Console.ReadLine();
|
||||
try
|
||||
{
|
||||
Key = System.Guid.NewGuid().ToString(),
|
||||
Value = new DataModel(System.Guid.NewGuid().ToString())
|
||||
if (input != String.Empty)
|
||||
{
|
||||
message = str
|
||||
}
|
||||
};
|
||||
var data = new DataModel()
|
||||
{
|
||||
message = input
|
||||
};
|
||||
|
||||
var dr = await producer.ProduceAsync(TOPIC_NAME, msg);
|
||||
Console.WriteLine($"Delivered message with id '{dr.Value.id}'");
|
||||
}
|
||||
catch (ProduceException<string, DataModel> e)
|
||||
{
|
||||
Console.WriteLine($"Delivery failed: {e.Error.Reason}");
|
||||
}
|
||||
await DataPublisher.Publish(data);
|
||||
}
|
||||
|
||||
}
|
||||
catch (OverflowException e)
|
||||
{
|
||||
Console.WriteLine("An error has occurred", e.Message, input);
|
||||
Console.WriteLine(introText);
|
||||
}
|
||||
}
|
||||
while (input != "+");
|
||||
|
||||
}
|
||||
}
|
45
producer/Publisher/DataPublisher.cs
Normal file
45
producer/Publisher/DataPublisher.cs
Normal file
@ -0,0 +1,45 @@
|
||||
using System.Text.Json;
|
||||
using Confluent.Kafka;
|
||||
using Model;
|
||||
using Serializers;
|
||||
|
||||
namespace Publisher
|
||||
{
|
||||
public class DataPublisher
|
||||
{
|
||||
private string _topic;
|
||||
private string _bootstrapServers;
|
||||
private IProducer<string, DataModel> _producer;
|
||||
|
||||
public DataPublisher(string topic, string bootstrapServers)
|
||||
{
|
||||
this._topic = topic;
|
||||
this._bootstrapServers = bootstrapServers;
|
||||
|
||||
var config = new ProducerConfig { BootstrapServers = this._bootstrapServers };
|
||||
this._producer = new ProducerBuilder<string, DataModel>(config)
|
||||
.SetValueSerializer(new JsonSerializer<DataModel>())
|
||||
.Build();
|
||||
|
||||
}
|
||||
|
||||
public async Task Publish(DataModel data)
|
||||
{
|
||||
try
|
||||
{
|
||||
var msg = new Message<string, DataModel>
|
||||
{
|
||||
Key = System.Guid.NewGuid().ToString(),
|
||||
Value = data
|
||||
};
|
||||
|
||||
var dr = await this._producer.ProduceAsync(this._topic, msg);
|
||||
Console.WriteLine($"Delivered message with id '{dr.Value.id}'");
|
||||
}
|
||||
catch (ProduceException<string, DataModel> e)
|
||||
{
|
||||
Console.WriteLine($"Delivery failed: {e.Error.Reason}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user