-
Notifications
You must be signed in to change notification settings - Fork 23
QuickStart
The RabbitBus library was designed to allow for the centralization of all RabbitMQ configuration at application startup, separating the concerns of routing, serialization, and error handling from the central concerns of publishing and consuming messages.
RabbitBus works with object-based messages. For example, if you have an application from which you would like to publish status update messages, you might model your message using the following class:
[Serializable]
public class StatusUpdate
{
public StatusUpdate(string status)
{
Status = status;
}
public string Status { get; set; }
}
An instance of the Bus
type is then used to either publish or consume the message.
Configuration of the Bus
is handled through a BusBuilder
. The BusBuilder
type provides an API for specifying how serialization, publication, consumption, and other concerns will be handled by the bus.
To configure a producer application to publish messages of type StatusUpdate
to a direct exchange named “status-update-exchange” on localhost, you would use the following configuration:
Bus bus = new BusBuilder()
.Configure(ctx => ctx.Publish<StatusUpdate>()
.WithExchange("status-update-exchange"))
.Build();
bus.Connect();
To publish a StatusUpdate
message, you would then make the following invocation:
bus.Publish(new StatusUpdate("OK"));
To configure a consumer application to subscribe to StatusUpdate
messages on localhost, you would use the following configuration:
Bus bus = new BusBuilder()
.Configure(ctx => ctx.Consume<StatusUpdate>()
.WithExchange("status-update-exchange")
.WithQueue("status-update-queue"))
.Build();
To subscribe to StatusUpdate
messages, you would then make the following invocation:
bus.Subscribe<StatusUpdate>(messageContext => { /* handle message */ });
RabbitBus was designed to manage long-lived AMPQ connections to the RabbitMQ broker. Therefore, RabbitBus should typically be initialized at the start of your application with a single instance of Bus
being used for all messaging needs.