Skip to content

Subscribe

derekgreer edited this page Jun 22, 2012 · 2 revisions

Message subscriptions in RabbitBus are setup using the IBus.Subscribe() method. For example, to subscribe to StatusUpdate messages, you would make the following invocation:

bus.Subscribe<StatusUpdate>(messageContext => { /* handle message */ });

The StatusUpdate type is a simple class which might be defined as follows:

[Serializable]
public class StatusUpdate
{
	public StatusUpdate(string status)
	{
		Status = status;
	}
	public string Status { get; set; }
}

By default, RabbitBus attempts to consume non-registered messages from an auto-deleted, non-persistent, non-durable queue matching the name of the message being published which is bound to an auto-deleted, non-persistent, non-durable direct exchange by the same name. The following attempts to subscribe to messages of type ```StatusUpdate```` using the default routing configuration:

var bus = new Bus();
bus.Subscribe<StatusUpdate>(messageContext => { /* handle message */ });
bus.Close();

To specify how RabbitBus subscribes to messages, an instance of IBus can be configured using the BusBuilder type. The following example subscribes to StatusUpdate messages to a direct exchange named "status-update-exchange" with a queue named "status-update-queue" on localhost:

Bus bus = new BusBuilder()
  .Configure(ctx => ctx.Consume<StatusUpdate>()
                         .WithExchange("status-update-exchange")
                         .WithQueue("status-update-queue"))
  .Build();
Clone this wiki locally