Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

how to make rabbitmq start and stop base on c# services

        public async Task StartConsuming(IMessageBusConsumer consumer, MessageBusConsumerName fullConsumerName, CancellationToken cancellationToken)
        {
            var queueName = GetQueueName(consumer.MessageBusConsumerEnum);

            using (var model = _rabbitConnection.CreateModel())
            {
                // Configure the Quality of service for the model. Below is how what each setting means.
                // BasicQos(0="Don't send me a new message until I’ve finished",  _fetchSize = "Send me N messages at a time", false ="Apply to this Model only")
                model.BasicQos(0, consumer.FetchCount.Value, false);
                var queueingConsumer = new QueueingBasicConsumer(model);

                model.BasicConsume(queueName, false, fullConsumerName, queueingConsumer);


                var queueEmpty = new BasicDeliverEventArgs(); //This is what gets returned if nothing in the queue is found.

                while (!cancellationToken.IsCancellationRequested)
                {
                    var deliverEventArgs = queueingConsumer.Queue.DequeueNoWait(queueEmpty);
                    if (deliverEventArgs == queueEmpty)
                    {
                        // This 100ms wait allows the processor to go do other work.
                        // No sense in going back to an empty queue immediately. 
                        // CancellationToken intentionally not used!
                        // ReSharper disable once MethodSupportsCancellation
                        await Task.Delay(100);  
                        continue;
                    }
                    //DO YOUR WORK HERE!
                  }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: Test for even Number 
Csharp :: indexing an array 
Csharp :: c# convert securestring to string 
Csharp :: wpf listbox binding change style of selected item 
Csharp :: input.getbutton unity 
Csharp :: c# resize multidimensional array 
Csharp :: c# datagridview cell align center 
Csharp :: check if list contains any empty element in c# 
Csharp :: mysql restore backup from multiple files 
Csharp :: convert date to days c# 
Csharp :: how to get gravity from Rigidbody2D in c# 
Csharp :: color rgb to float c# 
Csharp :: dotnet core encryption and decryption 
Csharp :: c# convert xml to list string 
Csharp :: c# split string 
Csharp :: set background from C# wpf 
Csharp :: c# getting response content from post 
Csharp :: convert xml to json 
Csharp :: C# long 
Csharp :: Proxy in Config 
Csharp :: send mail c# 
Csharp :: Entity framwork update parent entity added new sub entity 
Csharp :: what does focusbale do in listview WPF 
Csharp :: c# bool? to bool 
Csharp :: How to cache database tables to prevent many database queries in Asp.net C# mvc 
Csharp :: infinit range loop c# 
Csharp :: asp.net session empty cehck 
Csharp :: "using" c# 
Csharp :: c# simplified if statement 
Csharp :: F# tuple get item 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =