Giter Club home page Giter Club logo

carrot's Introduction

Carrot

Carrot is a .NET lightweight library that provides a couple of facilities over RabbitMQ.

install from nugetdownloads

What is it?

Prompted by the need for an easy-to-use RabbitMQ access component not requiring lots of boilerplate code in order to accomplish basic operations with RabbitMQ broker. Inspired by MassTransit.

Getting started

Just mark your POCO message contracts with MessageBinding attribute:

[MessageBinding("urn:message:foo")]
public class Foo
{
    public Int32 Bar { get; set; }
}

Define your message consumer:

class FooConsumer : Consumer<Foo>
{
    public override Task ConsumeAsync(ConsumedMessage<Foo> message)
    {
        return Task.Factory.StartNew(() =>
                                     {
                                         Console.WriteLine("received '{0}'",
                                                           message.Headers.MessageId);
                                     });
    }
}

Create an instance of Broker providing the RabbitMQ host as input.

var broker = Broker.New(_ =>
{
    _.Endpoint(new Uri("amqp://guest:guest@localhost:5672/", UriKind.Absolute));
    _.ResolveMessageTypeBy(new MessageBindingResolver(typeof(Foo).Assembly));
});

Declare your AMQP entities as the following:

var exchange = broker.DeclareDirectExchange("source_exchange");
var queue = broker.DeclareQueue("my_test_queue");

Bind entities, subscribe your queue and call IBroker.Connect:

broker.DeclareExchangeBinding(exchange, queue, "routing_key");
broker.SubscribeByAtLeastOnce(queue, _ => _.Consumes(new FooConsumer()));
var connection = broker.Connect();

You're up 'n running! Do not forget to call IConnection.Dispose() when your application exits.

Please note that exchanges, queues and messages are not durable by default. You can create durable entities by calling the proper builder methods.

var durableExchange = broker.DeclareDurableDurectExchange.Direct("source_exchange");
var durableQueue = broker.DeclareDurableQueue.Direct("my_test_queue");

You can publish messages as the following:

connection.PublishAsync(new OutboundMessage<Foo>(new Foo { Bar = 2 }),
                        exchange);

Please note that messages are not durable by default. If you need durable messaging, make use of DurableOutboundMessage<T>:

connection.PublishAsync(new DurableOutboundMessage<Foo>(new Foo { Bar = 2 }),
                        exchange);

Building from Source

Clone the source down to your machine.

git clone https://github.com/naighes/Carrot.git

Run build.bat

Resources

carrot's People

Contributors

naighes avatar paololaurenti avatar matteobaglini avatar lsfera avatar pichierrif avatar emadb avatar

Watchers

James Cloos avatar  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.