Skip to content

Commit aee0ab4

Browse files
committed
rabbit library
1 parent 5bbe663 commit aee0ab4

File tree

7 files changed

+152
-1
lines changed

7 files changed

+152
-1
lines changed

src/Basket/Basket.API/Basket.API.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,9 @@
1414
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.3.1" />
1515
</ItemGroup>
1616

17+
<ItemGroup>
18+
<ProjectReference Include="..\..\Common\EventBusRabbitMQ\EventBusRabbitMQ.csproj" />
19+
</ItemGroup>
20+
1721

1822
</Project>

src/Basket/Basket.API/Startup.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Basket.API.Data.Interfaces;
77
using Basket.API.Repositories;
88
using Basket.API.Repositories.Interfaces;
9+
using EventBusRabbitMQ;
910
using Microsoft.AspNetCore.Builder;
1011
using Microsoft.AspNetCore.Hosting;
1112
using Microsoft.AspNetCore.Mvc;
@@ -14,6 +15,7 @@
1415
using Microsoft.Extensions.Hosting;
1516
using Microsoft.Extensions.Logging;
1617
using Microsoft.OpenApi.Models;
18+
using RabbitMQ.Client;
1719
using StackExchange.Redis;
1820

1921
namespace Basket.API
@@ -30,7 +32,7 @@ public Startup(IConfiguration configuration)
3032
// This method gets called by the runtime. Use this method to add services to the container.
3133
public void ConfigureServices(IServiceCollection services)
3234
{
33-
services.AddControllers();
35+
services.AddControllers();
3436

3537
#region Redis Dependencies
3638

@@ -58,6 +60,32 @@ public void ConfigureServices(IServiceCollection services)
5860
});
5961

6062
#endregion
63+
64+
65+
#region RabbitMQ Dependencies
66+
67+
services.AddSingleton<IRabbitMQConnection>(sp =>
68+
{
69+
var factory = new ConnectionFactory()
70+
{
71+
HostName = Configuration["EventBusHostName"]
72+
};
73+
74+
if (!string.IsNullOrEmpty(Configuration["EventBusUserName"]))
75+
{
76+
factory.UserName = Configuration["EventBusUserName"];
77+
}
78+
79+
if (!string.IsNullOrEmpty(Configuration["EventBusPassword"]))
80+
{
81+
factory.Password = Configuration["EventBusPassword"];
82+
}
83+
84+
return new RabbitMQConnection(factory);
85+
});
86+
87+
#endregion
88+
6189
}
6290

6391
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
3+
namespace EventBusRabbitMQ.Common
4+
{
5+
public class BasketCheckout
6+
{
7+
public string City { get; set; }
8+
public string Street { get; set; }
9+
public string State { get; set; }
10+
public string Country { get; set; }
11+
public string ZipCode { get; set; }
12+
public string CardNumber { get; set; }
13+
public string CardHolderName { get; set; }
14+
public DateTime CardExpiration { get; set; }
15+
public string CardSecurityNumber { get; set; }
16+
public int CardTypeId { get; set; }
17+
public string Buyer { get; set; }
18+
public Guid RequestId { get; set; }
19+
}
20+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace EventBusRabbitMQ.Common
2+
{
3+
public static class EventBusConstants
4+
{
5+
public const string BasketCheckoutQueue = "basketCheckoutQueue";
6+
}
7+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using EventBusRabbitMQ.Common;
2+
using Newtonsoft.Json;
3+
using RabbitMQ.Client;
4+
using RabbitMQ.Client.Events;
5+
using System;
6+
using System.Text;
7+
8+
namespace EventBusRabbitMQ.Consumer
9+
{
10+
public class EventBusRabbitMQConsumer
11+
{
12+
private readonly IRabbitMQConnection _connection;
13+
14+
public EventBusRabbitMQConsumer(IRabbitMQConnection connection)
15+
{
16+
_connection = connection ?? throw new ArgumentNullException(nameof(connection));
17+
}
18+
19+
public void Consume()
20+
{
21+
var channel = _connection.CreateModel();
22+
channel.QueueDeclare(queue: EventBusConstants.BasketCheckoutQueue, durable: false, exclusive: false, autoDelete: false, arguments: null);
23+
24+
var consumer = new EventingBasicConsumer(channel);
25+
26+
//Create event when something receive
27+
consumer.Received += ReceivedEvent;
28+
29+
channel.BasicConsume(queue: EventBusConstants.BasketCheckoutQueue, autoAck: true, consumer: consumer);
30+
}
31+
32+
private void ReceivedEvent(object sender, BasicDeliverEventArgs e)
33+
{
34+
if (e.RoutingKey == EventBusConstants.BasketCheckoutQueue)
35+
{
36+
var message = Encoding.UTF8.GetString(e.Body);
37+
BasketCheckout basketCheckout = JsonConvert.DeserializeObject<BasketCheckout>(message);
38+
39+
// TODO : stuff
40+
}
41+
}
42+
43+
public void Disconnect()
44+
{
45+
_connection.Dispose();
46+
}
47+
}
48+
}

src/Common/EventBusRabbitMQ/EventBusRabbitMQ.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8+
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
89
<PackageReference Include="RabbitMQ.Client" Version="5.1.2" />
910
</ItemGroup>
1011

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using EventBusRabbitMQ.Common;
2+
using Newtonsoft.Json;
3+
using RabbitMQ.Client;
4+
using System;
5+
using System.Text;
6+
7+
namespace EventBusRabbitMQ.Producer
8+
{
9+
public class EventBusRabbitMQProducer
10+
{
11+
private readonly IRabbitMQConnection _connection;
12+
13+
public EventBusRabbitMQProducer(IRabbitMQConnection connection)
14+
{
15+
_connection = connection ?? throw new ArgumentNullException(nameof(connection));
16+
}
17+
18+
public void PublishBasketCheckout(string queueName, BasketCheckout publishModel)
19+
{
20+
using (var channel = _connection.CreateModel())
21+
{
22+
channel.QueueDeclare(queue: queueName, durable: false, exclusive: false, autoDelete: false, arguments: null);
23+
var message = JsonConvert.SerializeObject(publishModel);
24+
var body = Encoding.UTF8.GetBytes(message);
25+
26+
IBasicProperties properties = channel.CreateBasicProperties();
27+
properties.Persistent = true;
28+
properties.DeliveryMode = 2;
29+
30+
channel.ConfirmSelect();
31+
channel.BasicPublish(exchange: "", routingKey: queueName, mandatory: true, basicProperties: properties, body: body);
32+
channel.WaitForConfirmsOrDie();
33+
34+
channel.BasicAcks += (sender, eventArgs) =>
35+
{
36+
Console.WriteLine("Sent RabbitMQ");
37+
//implement ack handle
38+
};
39+
channel.ConfirmSelect();
40+
}
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)