Skip to content

ConsumerLinqAsync

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

Consuming messages

You may consume messages in a couple of different ways. Either via dedicated worker threads ConsumerLinq, 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
var queueConnection = new QueueConnection(queueName, connectionString);
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<QueueInit>())
            {
            	using (
                var queue = queueContainer.CreateConsumerMethodQueueScheduler(queueConnection, factory)
                 )
                {
                	queue.Start();
                    Console.WriteLine("Processing messages - press any key to stop");
                    Console.ReadKey((true));
               }
 		}
	}
}

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 linq statement finishes with no errors, the statement 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.

You will have to pass the IWorkerNotifications instance to your statement in order to check these flags.

i.e.

var result = queue.Send((m, w) => Console.WriteLine(w.TransportSupportsRollback));
if (w.TransportSupportsRollback && w.WorkerStopping.CancelWorkToken.IsCancellationRequested)
{
	w.Log.Log(DotNetWorkQueue.Logging.LogLevel.Debug, () => "Cancel has been requested - aborting");
	w.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