Skip to content

GetMessage

derekgreer edited this page Jun 14, 2012 · 2 revisions

In addition to the support of RabbitMQ's subscription-based push API, RabbitBus also supports RabbitMQ's pull API which allows consumers to retrieve messages on-demand. Just as with the subscription-based API, configuration for the pull-based API is facilitated through the Consume() method provided by the BusBuilder type.

To configure a consumer application to pull 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();

In contrast to the RabbitBus subscription API, the pull API is facilitated through the use of a disposable IConsumerContext type which maintains an open channel on the RabbitMQ connection until disposed. To retrieve a single StatusUpdate message, you would make the following invocation:

using (IConsumerContext<StatusUpdate> consumerContext = bus.CreateConsumerContext<StatusUpdate>())
{
	IMessageContext<StatusUpdate> messageContext = consumerContext.GetMessage();
	/* 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; }
}
Clone this wiki locally