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