Last active
January 8, 2022 10:32
-
-
Save hkucuk/78e947dc24be43c1899fa2ce56f726f1 to your computer and use it in GitHub Desktop.
a rabbitmq consumer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Text; | |
using RabbitMQ.Client; | |
using RabbitMQ.Client.Events; | |
var factory = new ConnectionFactory(); | |
//AMQP URL | |
factory.Uri = new Uri("amqps://fxunuiqu:AKyj5ShMAw-AmH3InEBx6G_lQov-Cofs@puffin.rmq2.cloudamqp.com/fxunuiqu"); | |
var queueName = "test-queue"; | |
using var connection = factory.CreateConnection() ; | |
var channel = connection.CreateModel(); | |
var consumer = new EventingBasicConsumer(channel); | |
//Kuyruktan mesajlarin alinma sekli | |
//Kuyruga iki istemci bagli ise her bir istemciye 3 mesaj gider. | |
channel.BasicQos | |
( | |
prefetchSize: 0,//0 degeri herhangi bir boyuttaki mesajlar alinabilir anlamina gelir. | |
prefetchCount: 6,// 5 degeri t aninda 5 adet mesaj alinacagi anlamina gelir. | |
global: true // true degeri 5 adet mesaji hesaplarken kuyruga bagli tum istemcileri degerlendirmesini soyler. | |
// false degeri atanmasi durumunda her bir istemciye tek seferde 6 adet measaj gönderilir | |
); | |
channel.BasicConsume | |
( | |
queue: queueName, | |
autoAck: false, //false degeri mesaj alindiktan sonra kuyruktan silinmesin anlamina gelir. | |
//istmeci mesaj ile isi bittiginde kuyruktan mesaji kendisi siler. | |
consumer: consumer | |
); | |
consumer.Received += (o, e) =>{ | |
try | |
{ | |
var message = Encoding.UTF8.GetString(e.Body.ToArray()); | |
Console.WriteLine(message); | |
//Mesaj islendi kuyruktan silinsin | |
channel.BasicAck | |
( | |
deliveryTag: e.DeliveryTag, | |
multiple: false // Sadece bu mesaj icin bilgi gonder. | |
); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.Message); | |
} | |
}; | |
Console.WriteLine("Kuyruk Dinleniyor"); | |
Console.Read(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment