-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Keycloak integrationa and added RabbitMQ
- Loading branch information
1 parent
f47749b
commit 34c3078
Showing
8 changed files
with
294 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using RabbitMQ.Client.Events; | ||
using RabbitMQ.Client; | ||
using System.Text.Json; | ||
using System.Text; | ||
|
||
namespace MusicService.Services.Event | ||
{ | ||
public class EventService : IEventService, IDisposable | ||
{ | ||
private readonly IConnection connection; | ||
public EventService(IConfiguration configuration) | ||
{ | ||
var factory = new ConnectionFactory | ||
{ | ||
HostName = configuration.GetConnectionString("rabbitmq") | ||
}; | ||
|
||
connection = factory.CreateConnection(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
connection.Close(); | ||
} | ||
|
||
public void Publish<T>(string exchange, string topic, T data) | ||
{ | ||
using var channel = connection.CreateModel(); | ||
|
||
channel.ExchangeDeclare(exchange, ExchangeType.Direct, durable: true, autoDelete: false); | ||
|
||
var json = JsonSerializer.Serialize(data); | ||
var body = Encoding.UTF8.GetBytes(json); | ||
|
||
channel.BasicPublish(exchange: exchange, routingKey: topic, body: body); | ||
} | ||
|
||
public void subscribe<T>(string exchange, string queue, string topic, Action<T> handler) | ||
{ | ||
var channel = connection.CreateModel(); | ||
|
||
channel.ExchangeDeclare(exchange: exchange, type: ExchangeType.Direct, durable: true, autoDelete: false); | ||
channel.QueueDeclare(queue, exclusive: false, autoDelete: false); | ||
channel.QueueBind(exchange: exchange, routingKey: topic, queue: queue); | ||
|
||
channel.BasicQos(0, 1, false); | ||
|
||
|
||
var consumer = new EventingBasicConsumer(channel); | ||
|
||
consumer.Received += (model, eventArgs) => | ||
{ | ||
var body = eventArgs.Body.ToArray(); | ||
var message = Encoding.UTF8.GetString(body); | ||
|
||
var data = JsonSerializer.Deserialize<T>(message); | ||
|
||
handler.Invoke(data); | ||
Check warning on line 58 in MusicService/Services/Event/EventService.cs GitHub Actions / build
|
||
}; | ||
|
||
channel.BasicConsume(queue: queue, autoAck: true, consumer: consumer); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace MusicService.Services.Event | ||
{ | ||
public interface IEventService | ||
{ | ||
public void Publish<T>(string exchange, string topic, T data); | ||
public void subscribe<T>(string exchange, string queue, string topic, Action<T> handler); | ||
} | ||
} |
Oops, something went wrong.