Code Monkey home page Code Monkey logo

benner-sistemas / messaging Goto Github PK

View Code? Open in Web Editor NEW
7.0 9.0 4.0 384 KB

A lightweight .NET standard messaging library for easily deal with any message broker, like RabbitMQ, ActiveMQ, AmazonSQS, AzureQueue, ...

Home Page: https://dev.benner.com.br/

License: MIT License

C# 100.00%
messaging messaging-library messaging-api message-queue message-broker message-broker-client queues rabbitmq rabbit-mq rabbitmq-producer rabbitmq-consumer activemq activemq-client activemq-server amazon-sqs azure-mq aws-sqs fifo-queue publisher-subscriber

messaging's Introduction

Benner.Messaging

Benner.Messaging is a .NET Standard lightweight messaging library to deal with any message broker with ease. Benner.Messaging supports RabbitMQ, ActiveMQ, Amazon SQS and Azure Queue. It is free and open-source under MIT License.

Build Status

Branch Status
master Build Status

Nuget Package

Package Name .NET Framework .NET Standard
Benner.Messaging 4.6.1 2.0

One Messaging API to rule them all!

We provide an extremely simple and intuitive API for sending and receiving messages to either RabbitMQ, ActiveMQ, Amazon SQS or Azure Queue:

Messaging.Enqueue("queue-name", "hello world!");
var message = Messaging.Dequeue("queue-name");

Yep, you got that right! You code just once, on an extremly simple way, and run over any message broker. No vendor lock-in, no code refactoring. All the magic relies on configuration (file or injected code).

Behavior matters

It's important to notice that Messaging API was born to work on a specific way:

  • First in, first out
  • One producer sends
  • Only one consumer receives
  • If consumer fails at receiving or processing, the message returns to queue
  • The message will not be lost

Enqueue and Dequeue operations were designed to ensure that a sent message by the sender successfully arrives on the receiver. That means that we pursuit Publisher Confirms and Consumer Acknoledgement approach across any supported broker.

Get Started

Sending message

Create a new project:

dotnet new console -n producer
cd producer

Add Benner.Messaging and open the project on vscode:

dotnet add package benner.messaging
code .

Add using and just send a message to some queue:

using Benner.Messaging;
Messaging.Enqueue("queue-name", "hello world!");

That's it! dotnet run it and you will get:

Messaging config not found

Brokers configuration

Well, you need a messaging.config file, like that (don't worry, we will get deeper on configuration ahead):

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="MessagingConfigSection" type="Benner.Messaging.MessagingFileConfigSection, Benner.Messaging" />
  </configSections>
  <MessagingConfigSection>
    <queues>
      <queue name="teste" broker="RabbitMQ" />
    </queues>
    <brokerList default="RabbitMQ">
      <broker name="RabbitMQ" type="Benner.Messaging.RabbitMQConfig, Benner.Messaging">
        <add key="HostName" value="Servidor" />
        <add key="UserName" value="Usuario" />
        <add key="Password" value="Senha" />
      </broker>
    </brokerList>
  </MessagingConfigSection>
</configuration>

You can also inject config through code:

var config = new MessagingConfigBuilder()
    .WithRabbitMQBroker("RabbitMQ", "servername", setAsDefault: true)
    .Create();

Messaging.Enqueue("queue-name", "hello world!", config);

That's it! dotnet run it and you will get:

Unable to connect to RabbitMQ server

Provisioning a broker

Well... you need, in this case, a RabbitMQ runnig according to your configuration.

Thankfully humanity evolved and we have Docker containers to help us out. So, let's just run it!

docker run -d -v rabbitmq_data:/var/lib/rabbitmq -p 15672:15672 -p 15671:15671 -p 5672:5672 -p 5671:5671 rabbitmq:3.7-management

Now we are good to go! dotnet run it. No errors? Great! Now what?

Check broker management console

On your browser, access http://servername:15672/#/queues (user guest, password guest) to manage Rabbit and you'll be able to see your queue with your messages inside it.

Receive message

Create a new project:

dotnet new console -n consumer
cd consumer

Add Benner.Messaging and open the project on vscode:

dotnet add package benner.messaging
code .

Add using and just receive a message from the queue, not forgeting configuration:

using Benner.Messaging;

var config = new MessagingConfigBuilder()
    .WithRabbitMQBroker("RabbitMQ", "servername", setAsDefault: true)
    .Create();

var message = Messaging.Dequeue("queue-name", config);
Console.Write(message);

That's it! dotnet run it and you receive:

hello world! 

Now let's do it insanelly

Change consumer code to:

using Benner.Messaging;

var config = new MessagingConfigBuilder()
    .WithRabbitMQBroker("RabbitMQ", "servername", setAsDefault: true)
    .Create();

using (var client = new Messaging(config))
{
    client.StartListening("queue-name", (e) =>
    {
        // Print the message
        Console.WriteLine(e.AsString);
        return true;
    });
    // Stand-by the application so it can keep listening
    Console.ReadKey();
}

Ok, dotnet run it 3 times so you will have 3 consumers simultaneously

Change producer code to:

using Benner.Messaging;

var config = new MessagingConfigBuilder()
    .WithRabbitMQBroker("RabbitMQ", "servername", setAsDefault: true)
    .Create();

// Create new instance of messaging
using (var client = new Messaging(config))
{
    // Sending 1000 messages
    for (int i = 1; i <= 1000; i++)
        client.EnqueueMessage("queue-name", "hello world #" + i);
}

Done, dotnet run it and see what happens.

What's next

Check out get started samples here.

Or, read more details here.

messaging's People

Contributors

leoformaggi avatar nkruger01 avatar sanguedemonstro avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

messaging's Issues

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.