Giter VIP home page Giter VIP logo

r.boobus.rabbitmq's Introduction

R.BooBus.RabiitMq Build Status NuGet Package

A smart and simple library for pub sub implementtion to RabbitMq

Package Manager Console

Install-Package R.BooBus.RabiitMq

.NET Core CLI

dotnet add package R.BooBus.RabiitMq

Usage

1 - In the publish process, register the service in net core DI container with the fluent built in api like this:

      //A instace of ServiceCollection 
      services.UseRabbitMq("amqp://guest:guest@localhost", "Gabriel", nameof(PublishVideoEvent));

2 - Create a custom event that inherits from the Event Class of the R.Boobus.Core namespace

 public class MyEvent : Event
 {
 }

3 - We are now ready to publish the event on the bus.

 public class SomeClass 
 {
        private readonly IRabbitMQEventBus _bus;

        public SomeClass(IRabbitMQEventBus bus)
        {
            _bus = azureServiceBus;
        }

        protected async Task runAsync()
        {
             var message = new MyEvent();             
             _bus.Publish(message);
        }
    }

5 - In the subscriber process, register the service in net core DI container with the fluent built in api like this:

      //A instace of ServiceCollection 
       services.UseRabbitMq("amqp://guest:guest@localhost", "Gabriel", nameof(PublishVideoEvent));

6 - Subscribe to start receiving your message or unsubscribe to stop receiving then.

    public class Worker : BackgroundService
    {
        private IRabbitMQEventBus _bus;

        public Worker(IRabbitMQEventBus bus)
        {
            _bus = bus;
        }

        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            
        }

        public override Task StartAsync(CancellationToken cancellationToken)
        {

            _bus.Subscribe<PublishVideoEvent, PublishedVideoEventHandler>();
            return base.StartAsync(cancellationToken);
        }

        public override Task StopAsync(CancellationToken cancellationToken)
        {
            _bus.Unsubscribe<PublishVideoEvent, PublishedVideoEventHandler>();
            return base.StopAsync(cancellationToken);
        }
    }

7 - Create the same event created on publish process that inherits from the Event Class of the R.Boobus.Core namespace

 public class MyEvent : Event
 {
 }

8 - Create the event handler that implement IEventHandler from the R.Boobus.Core namespace

 public class HelloHandler : IEventHandler<MyEvent>
    {
        private ILogger<HelloHandler> _logger;

        public HelloHandler(ILogger<HelloHandler> logger)
        {
            _logger = logger;
        }
        public Task Handle(MyEvent @event)
        {
            _logger.LogInformation("Get Event from  {@event} at {time}", @event.ToString(), DateTimeOffset.Now);

            return Task.FromResult(true);
        }
    }

r.boobus.rabbitmq's People

Contributors

booletec avatar

Stargazers

 avatar

Watchers

Renato Nascimento avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.