Skip to content

ConsumerAsync

Brian Lehnen edited this page Dec 8, 2020 · 8 revisions

Consuming messages

You may consume messages in a couple of different ways. Either via dedicated worker threads Consumer, or via a pool of shared threads between multiple queues.

Creating the queue

You'll need the following

  • A task scheduler instance
  • The transport your connecting to, determined by the transport init module you specify when creating the container.
  • The connection string to the transport
  • The name of the queue
  • A delegate that will be called for each message to process
using (var schedulerContainer = new SchedulerContainer())
{
	using (var scheduler = schedulerContainer.CreateTaskScheduler())
        {
        	var factory = schedulerContainer.CreateTaskFactory(scheduler);
            factory.scheduler.Configuration.MaximumThreads = 8;
            factory.scheduler.Configuration.MinimumThreads = 0;
            factory.scheduler.Start();
            using (var queueContainer = new QueueContainer<SqlServerMessageQueueInit>())
            {
            	var queueConnection = new QueueConnection(queueName, connectionString);
            	using (
                var queue = queueContainer.CreateConsumerQueueScheduler(queueConnection, factory)
                 )
                {
                	queue.Start<SimpleMessage>(Handle);
                    Console.WriteLine("Processing messages - press any key to stop");
                    Console.ReadKey((true));
               }
 		}
	}
}

private void Handle(IReceivedMessage<SimpleMessage> m, IWorkerNotification n)
{
	//processing logic goes here
}

You may use a default scheduler and factory by not passing one to the creation logic. However, this means you will be unable to change their configuration. It also means you won't be able to re-use the scheduler with multiple queues.

When your delgate method finishes with no errors, the message will be acked and considered finished. However, you may wish to check for a condition in your logic

  • The queue shutting down. A cancel token is provided for this.
    • If the transport supports rollback, you may throw an operation canceled exception to requeue the message

For example, here is how you can check to see if canelazation is requested, and also force a requeue. Note that we are verifying that the transport supports rollbacks first.

if (notifications.TransportSupportsRollback && notifications.WorkerStopping.CancelWorkToken.IsCancellationRequested)
{
	notifications.Log.Log(DotNetWorkQueue.Logging.LogLevel.Debug, () => "Cancel has been requested - aborting");
	notifications.WorkerStopping.CancelWorkToken.ThrowIfCancellationRequested();
}

You could register a delegate with the Cancel token instead, so that you don't have to constantly check the token throughout your code.

Stopping the queue

To stop the queue, call dispose on it.

queue.Dispose();

Calling dispose on the queue container will dispose all queues created by that container as well. If you created a task scheduler, you should dispose of it after all queues have been disposed.

scheduler.Dispose();

Dispose is blocking operation. Depending on your configuration settings and how quickly your message consuming code responds to cancels, it may take a while to return.

Clone this wiki locally