Skip to content

Commit

Permalink
Adding consumer layer
Browse files Browse the repository at this point in the history
  • Loading branch information
farhadzm committed Apr 30, 2021
1 parent 177953d commit 8498f91
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 0 deletions.
25 changes: 25 additions & 0 deletions RabbitMq/RabbitMq.Consumer/HostedServices/ConsumerHostedService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Microsoft.Extensions.Hosting;
using RabbitMq.Consumer.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace RabbitMq.Consumer.HostedServices
{
public class ConsumerHostedService : BackgroundService
{
private readonly IConsumerService _consumerService;

public ConsumerHostedService(IConsumerService consumerService)
{
_consumerService = consumerService;
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await _consumerService.ReadMessgaes();
}
}
}
26 changes: 26 additions & 0 deletions RabbitMq/RabbitMq.Consumer/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace RabbitMq.Consumer
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
28 changes: 28 additions & 0 deletions RabbitMq/RabbitMq.Consumer/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:3920",
"sslPort": 44319
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"RabbitMq.Consumer": {
"commandName": "Project",
"dotnetRunMessages": "true",
"launchBrowser": true,
"applicationUrl": "https://localhost:5005;",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
11 changes: 11 additions & 0 deletions RabbitMq/RabbitMq.Consumer/RabbitMq.Consumer.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\RabbitMq.Common\RabbitMq.Common.csproj" />
</ItemGroup>

</Project>
9 changes: 9 additions & 0 deletions RabbitMq/RabbitMq.Consumer/RabbitMq.Consumer.csproj.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup>
<ActiveDebugProfile>RabbitMq.Consumer</ActiveDebugProfile>
</PropertyGroup>
</Project>
50 changes: 50 additions & 0 deletions RabbitMq/RabbitMq.Consumer/Services/ConsumerService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using RabbitMq.Common.Services;
using RabbitMQ.Client.Events;
using System;
using RabbitMQ.Client;
using System.Threading.Tasks;

namespace RabbitMq.Consumer.Services
{
public interface IConsumerService
{
Task ReadMessgaes();
}

public class ConsumerService : IConsumerService, IDisposable
{
private readonly IModel _model;
private readonly IConnection _connection;
public ConsumerService(IRabbitMqService rabbitMqService)
{
_connection = rabbitMqService.CreateChannel();
_model = _connection.CreateModel();
_model.QueueDeclare(_queueName, durable: true, exclusive: false, autoDelete: false);
_model.ExchangeDeclare("UserExchange", ExchangeType.Fanout, durable: true, autoDelete: false);
_model.QueueBind(_queueName, "UserExchange", string.Empty);
}
const string _queueName = "User";
public async Task ReadMessgaes()
{
var consumer = new AsyncEventingBasicConsumer(_model);
consumer.Received += async (ch, ea) =>
{
var body = ea.Body.ToArray();
var text = System.Text.Encoding.UTF8.GetString(body);
Console.WriteLine(text);
await Task.CompletedTask;
_model.BasicAck(ea.DeliveryTag, false);
};
_model.BasicConsume(_queueName, false, consumer);
await Task.CompletedTask;
}

public void Dispose()
{
if (_model.IsOpen)
_model.Close();
if (_connection.IsOpen)
_connection.Close();
}
}
}
35 changes: 35 additions & 0 deletions RabbitMq/RabbitMq.Consumer/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using RabbitMq.Common.Extensions;
using RabbitMq.Consumer.HostedServices;
using RabbitMq.Consumer.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace RabbitMq.Consumer
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; set; }
public void ConfigureServices(IServiceCollection services)
{
services.AddCommonService(Configuration);
services.AddSingleton<IConsumerService, ConsumerService>();
services.AddHostedService<ConsumerHostedService>();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
}
}
}

0 comments on commit 8498f91

Please sign in to comment.