Skip to content

ConsumerLinq

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

Consuming Linq Statemeents

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

Creating the queue

You'll need the following

  • 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 queueContainer = new QueueContainer<QueueInit>())
{
	using (var queue = queueContainer.CreateMethodConsumer(queueConnection))
    {
    	queue.Configuration.Worker.WorkerCount = 4;
        queue.Start();
        Console.WriteLine("Processing messages - press any key to stop");
        Console.ReadKey((true));
	}
}

Unlike the POCO queue, no delagate for message processing is used. Instead, the Linq statement is exceuted. All types must be resolvable, or errors will be thrown.

You may want to check for the following conditions in your code

  • 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();
}
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.

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