Compare commits
2 Commits
947bf35cf6
...
d9866c2fa9
Author | SHA1 | Date | |
---|---|---|---|
d9866c2fa9 | |||
1ce55a583a |
@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "producer", "producer\produc
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "consumer", "consumer\consumer.csproj", "{1F8DAF6D-67B2-43C4-ACFD-38B8541722C3}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "consumer", "consumer\consumer.csproj", "{1F8DAF6D-67B2-43C4-ACFD-38B8541722C3}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "shared", "shared\shared.csproj", "{6958D7C2-23D8-4383-9F03-6395DE86FD63}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@ -24,5 +26,9 @@ Global
|
|||||||
{1F8DAF6D-67B2-43C4-ACFD-38B8541722C3}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{1F8DAF6D-67B2-43C4-ACFD-38B8541722C3}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{1F8DAF6D-67B2-43C4-ACFD-38B8541722C3}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{1F8DAF6D-67B2-43C4-ACFD-38B8541722C3}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{1F8DAF6D-67B2-43C4-ACFD-38B8541722C3}.Release|Any CPU.Build.0 = Release|Any CPU
|
{1F8DAF6D-67B2-43C4-ACFD-38B8541722C3}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{6958D7C2-23D8-4383-9F03-6395DE86FD63}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{6958D7C2-23D8-4383-9F03-6395DE86FD63}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{6958D7C2-23D8-4383-9F03-6395DE86FD63}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{6958D7C2-23D8-4383-9F03-6395DE86FD63}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
EndGlobal
|
EndGlobal
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
using System.Threading;
|
using System.Threading;
|
||||||
using Confluent.Kafka;
|
using Confluent.Kafka;
|
||||||
using Model;
|
using Model;
|
||||||
|
using Repository;
|
||||||
using Serializers;
|
using Serializers;
|
||||||
|
|
||||||
class Consumer
|
class Consumer
|
||||||
@ -14,6 +15,8 @@ class Consumer
|
|||||||
AutoOffsetReset = AutoOffsetReset.Latest
|
AutoOffsetReset = AutoOffsetReset.Latest
|
||||||
};
|
};
|
||||||
|
|
||||||
|
DataRepository dataRepository = new DataRepository("mongodb://mongo:mongo@localhost:27017", "mongo");
|
||||||
|
|
||||||
using (var consumer = new ConsumerBuilder<Ignore, DataModel>(conf)
|
using (var consumer = new ConsumerBuilder<Ignore, DataModel>(conf)
|
||||||
.SetValueDeserializer(new JsonSerializer<DataModel>())
|
.SetValueDeserializer(new JsonSerializer<DataModel>())
|
||||||
.Build())
|
.Build())
|
||||||
@ -33,6 +36,7 @@ class Consumer
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
var cr = consumer.Consume(cts.Token);
|
var cr = consumer.Consume(cts.Token);
|
||||||
|
dataRepository.Save(cr.Message.Value);
|
||||||
|
|
||||||
Console.WriteLine($"Consumed message with id '{cr.Message.Value.id}' at: '{cr.TopicPartitionOffset}'.");
|
Console.WriteLine($"Consumed message with id '{cr.Message.Value.id}' at: '{cr.TopicPartitionOffset}'.");
|
||||||
}
|
}
|
||||||
|
37
consumer/Repository/DataRepository.cs
Normal file
37
consumer/Repository/DataRepository.cs
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
using Model;
|
||||||
|
using MongoDB.Driver;
|
||||||
|
|
||||||
|
namespace Repository
|
||||||
|
{
|
||||||
|
public class DataRepository
|
||||||
|
{
|
||||||
|
private static string COLLECTION_NAME = "data";
|
||||||
|
private string _connectionString = "mongodb://mongo:mongo@localhost:27017";
|
||||||
|
private string _databaseName = "mongo";
|
||||||
|
private IMongoCollection<DataModel> _dataCollection;
|
||||||
|
private MongoClient _mongoClient;
|
||||||
|
|
||||||
|
public DataRepository(string connectionString, string databaseName)
|
||||||
|
{
|
||||||
|
this._connectionString = connectionString;
|
||||||
|
this._databaseName = databaseName;
|
||||||
|
this._mongoClient = new MongoClient(this._connectionString);
|
||||||
|
|
||||||
|
var db = this._mongoClient.GetDatabase(this._databaseName);
|
||||||
|
this._dataCollection = db.GetCollection<DataModel>(COLLECTION_NAME);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DataRepository()
|
||||||
|
{
|
||||||
|
this._mongoClient = new MongoClient(this._connectionString);
|
||||||
|
|
||||||
|
var db = this._mongoClient.GetDatabase(this._databaseName);
|
||||||
|
this._dataCollection = db.GetCollection<DataModel>(COLLECTION_NAME);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async void Save(DataModel data)
|
||||||
|
{
|
||||||
|
await this._dataCollection.InsertOneAsync(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -12,4 +12,8 @@
|
|||||||
<PackageReference Include="MongoDB.Driver" Version="2.26.0" />
|
<PackageReference Include="MongoDB.Driver" Version="2.26.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\shared\shared.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
@ -1,18 +0,0 @@
|
|||||||
namespace Model
|
|
||||||
{
|
|
||||||
public class DataModel
|
|
||||||
{
|
|
||||||
public DataModel(string id)
|
|
||||||
{
|
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public string id { get; set; }
|
|
||||||
|
|
||||||
public string? name { get; set; }
|
|
||||||
|
|
||||||
public string? description { get; set; }
|
|
||||||
|
|
||||||
public string? notes { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,22 +0,0 @@
|
|||||||
using Confluent.Kafka;
|
|
||||||
using System.Text.Json;
|
|
||||||
|
|
||||||
namespace Serializers
|
|
||||||
{
|
|
||||||
public class JsonSerializer<T> : ISerializer<T>, IDeserializer<T>
|
|
||||||
{
|
|
||||||
public byte[] Serialize(T data, SerializationContext context)
|
|
||||||
{
|
|
||||||
return JsonSerializer.SerializeToUtf8Bytes(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
public T Deserialize(ReadOnlySpan<byte> data, bool isNull, SerializationContext context)
|
|
||||||
{
|
|
||||||
if (!isNull)
|
|
||||||
{
|
|
||||||
return JsonSerializer.Deserialize<T>(data);
|
|
||||||
}
|
|
||||||
throw new Exception ("Data cannot be null");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -11,4 +11,8 @@
|
|||||||
<PackageReference Include="Confluent.Kafka" Version="2.4.0" />
|
<PackageReference Include="Confluent.Kafka" Version="2.4.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\shared\shared.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
13
shared/shared.csproj
Normal file
13
shared/shared.csproj
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Confluent.Kafka" Version="2.4.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
Loading…
Reference in New Issue
Block a user