Skip to content

Lightweight messaging wrapper of MassTransit

License

Notifications You must be signed in to change notification settings

crazyants/MetroBus

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MetroBus


alt tag

Lightweight messaging wrapper of MassTransit

Build status NuGet version

NuGet Packages

PM> Install-Package MetroBus 

####Features:

  • Currently only support RabbitMQ transport
  • Provide easily create Producer and Consumer for Pub/Sub
  • Provide easily Request/Response conversation
  • Include incremental auto retry policy
  • Include circuit breaker
  • Include rate limiter

Usage:

Initializing bus instance for Producer:

ISendEndpoint bus = await MetroBusInitializer.Instance.UseRabbitMq(string rabbitMqUri, string rabbitMqUserName, string rabbitMqPassword)
													.InitializeProducer(string queueName);

after bus instance initializing then you can use Send method with your queues channel TCommand type.

bus.Send<TCommand>(new
			{
				SomeProperty = SomeValue
			}
		);

using for Consumer:

static void Main(string[] args)
{
	BusHandle bus = MetroBusInitializer.Instance.UseRabbitMq(string rabbitMqUri, string rabbitMqUserName, string rabbitMqPassword)
							.InitializeConsumer<TCommandConsumer>(string queueName)
							// or .InitializeConsumer(string queueName, () => new TCommandConsumer())
							.Start();
	//if you want to stop
	bus.Stop();

	Console.ReadLine();
}

TCommandConsumer could like below:

public class TCommandConsumer : IConsumer<TCommand>
{
    public async Task Consume(ConsumeContext<TCommand> context)
    {
        var command = context.Message;

		//do something...
        await Console.Out.WriteAsync($"{command.SomeProperty}");
    }
}

Initializing bus instance for Request/Response conversation:

IRequestClient<TRequest, TResponse> client = MetroBusInitializer.Instance.UseRabbitMq(string rabbitMqUri, string rabbitMqUserName, string rabbitMqPassword)
                                                                    .InitializeRequestClient<TRequest, TResponse>(string queueName);

TResponse result = await client.Request(new TRequest
{
    Command = "Say hello!"
});

and consumer for Request/Response conversation could like below:

public class TCommandConsumer : IConsumer<TRequest>
{
    public async Task Consume(ConsumeContext<TRequest> context)
    {
        var command = context.Message;

		//do something...
        await Console.Out.WriteAsync($"{command.SomeProperty}");

		//and
		context.Respond(new TRequest
            {
                Command = "Hello!"
            });
    }
}

PS: Publisher and Consumer services must be used same TCommand interface. This is important for MassTransit integration. Also one other thing is rabbitMqUri parameter must start with "rabbitmq://" prefix.

There are several options you can set via fluent interface:

  • .UseIncrementalRetryPolicy(int retryLimit, int initialIntervalFromMinute, int intervalIncrementFromMinute, params Exception[] retryOnSpecificExceptionType)
  • .UseCircuitBreaker(int tripThreshold, int activeThreshold, int resetInterval)
  • .UseRateLimiter(int rateLimit, int interval)

About

Lightweight messaging wrapper of MassTransit

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%