您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“c# rabbitmq 怎么實(shí)現(xiàn)簡單收發(fā)消息”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“c# rabbitmq 怎么實(shí)現(xiàn)簡單收發(fā)消息”吧!
發(fā)布消息:(生產(chǎn)者)
/// <summary> /// 發(fā)送消息 /// </summary> /// <param name="queue">隊(duì)列名</param> /// <param name="message">消息內(nèi)容</param> private static void PublishInfo(string queue, string message) { try { var factory = new ConnectionFactory(); factory.HostName = "localhost";//RabbitMQ服務(wù)在本地運(yùn)行 factory.UserName = "guest";//用戶名 factory.Password = "guest";//密碼 using (var connection = factory.CreateConnection()) { using (var channel = connection.CreateModel()) { bool durable = true;//將消息標(biāo)記為持久性 channel.QueueDeclare(queue, durable, false, false, null); IBasicProperties properties = channel.CreateBasicProperties(); properties.DeliveryMode = 2; var body = Encoding.UTF8.GetBytes(message); channel.BasicPublish("", queue, properties, body); //開始傳遞 } } } catch (System.Exception ex) { Console.WriteLine(ex.ToString()); } Console.ReadLine(); }
使用:
PublishInfo("test", "222222222222");
點(diǎn)擊test進(jìn)入隊(duì)列內(nèi)部:
消費(fèi)消息:
方法一:舊版可用
private static void ConsumeInfo(string queue) { try { var factory = new ConnectionFactory(); factory.HostName = "localhost";//RabbitMQ服務(wù)在本地運(yùn)行 factory.UserName = "guest";//用戶名 factory.Password = "guest";//密碼 using (var connection = factory.CreateConnection()) { using (var channel = connection.CreateModel()) { //bool durable = true;//將消息標(biāo)記為持久性 //channel.QueueDeclare("sljcgx:finish-queue", durable, false, false, null);//聲明隊(duì)列,隊(duì)列不存在則創(chuàng)建該隊(duì)列 //設(shè)置prefetchCount : 1來告知RabbitMQ,在未收到消費(fèi)端的消息確認(rèn)時,不再分發(fā)消息,也就確保了當(dāng)消費(fèi)端處于忙碌狀態(tài)時,不再分配任務(wù)。 channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false); var consumer = new QueueingBasicConsumer(channel); //false為手動確認(rèn),獲取消息后需要添加 channel.BasicAck(ea.DeliveryTag, false)確認(rèn) //true則為自動確認(rèn),不需要這行代碼 channel.BasicConsume(queue, false, consumer); while (true) { try { var ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();//阻塞函數(shù),獲取隊(duì)列中的消息 var bodyReceive = ea.Body; string message = Encoding.UTF8.GetString(bodyReceive); Console.WriteLine(message); channel.BasicAck(ea.DeliveryTag, false);// 消息確認(rèn)信號,確認(rèn)后該消息將從隊(duì)列里移除 } catch (System.Exception ex) { Console.WriteLine(ex.Message); } } } } } catch (System.Exception ex) { Console.WriteLine(ex.ToString()); } Console.ReadLine(); }
方法二:(通用)
private static void ConsumeInfo2(string queue) { try { var factory = new ConnectionFactory(); factory.HostName = "localhost";//RabbitMQ服務(wù)在本地運(yùn)行 factory.UserName = "guest";//用戶名 factory.Password = "guest";//密碼 var connection = factory.CreateConnection(); var channel = connection.CreateModel(); channel.QueueDeclare(queue, true, false, false, null);//聲明隊(duì)列,隊(duì)列不存在則創(chuàng)建該隊(duì)列 channel.BasicQos(0, 1, false);//公平分發(fā)、同一時間只處理一個消息。 var consumer = new EventingBasicConsumer(channel); //消費(fèi)者(指定消息通道)該事件在接收到消息時觸發(fā) consumer.Received += (sender, e) => { byte[] body = e.Body.ToArray(); //消息字節(jié)數(shù)組 string message = Encoding.UTF8.GetString(body); //消息內(nèi)容 Console.WriteLine(message); channel.BasicAck(e.DeliveryTag, false); //手工確認(rèn) }; channel.BasicConsume(queue, false, consumer); //消費(fèi)消息(在當(dāng)前通道中監(jiān)聽 queue 隊(duì)列,并進(jìn)行消費(fèi)) Console.ReadLine(); connection.Close(); channel.Close(); } catch (System.Exception ex) { Console.WriteLine(ex.ToString()); } }
到此,相信大家對“c# rabbitmq 怎么實(shí)現(xiàn)簡單收發(fā)消息”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。