C#操作RabbitMQ的完整實(shí)例
一、下載RabbitMQ
http://www.rabbitmq.com/install-windows.html
二、下載OTP
http://www.erlang.org/downloads
三、安裝OTP、RabbitMQ
四、配置RabbitMQ
找到bat的目錄
執(zhí)行相關(guān)命令
1.添加用戶密碼 rabbitmqctl add_user wenli wenli
2.設(shè)置wenli為管理員rabbitmqctl set_user_tags wenli administrator
3.啟動(dòng)RabbitMQ的web管理rabbitmq-plugins enable rabbitmq_management
4.創(chuàng)建virtual host
5.設(shè)置用戶權(quán)限
點(diǎn)擊用戶名進(jìn)行設(shè)置
將virtual hosts 權(quán)限賦給用戶wenli
6.創(chuàng)建Exchanges
五.創(chuàng)建C# console
1.下載RabbitMQ驅(qū)動(dòng) https://github.com/yswenli/Wenli.Data.RabbitMQ/releases/tag/Release1.0.0
2.添加引用
3.添加配置
4.測(cè)試代碼:
using System; using System.Text; using System.Threading; using System.Threading.Tasks; namespace Wenli.Data.RabbitMQ.Console { using Console = System.Console; class Program { static void Main(string[] args) { Console.Title = "Wenli.Data.RabbitMQ.Console"; Console.WriteLine("正連接到mq"); try { Test(); } catch (Exception ex) { Console.WriteLine("err:" + ex.Message + ex.Source + ex.StackTrace); } Console.Read(); } static void Test() { var topic = "testtopic"; var cnn = RabbitMQBuilder.Get(MQConfig.Default).GetConnection(); var operation = cnn.GetOperation(topic); Console.WriteLine("正連接到訂閱【" + topic + "】"); operation.Subscribe(); Console.WriteLine("正在入隊(duì)"); Task.Factory.StartNew(() => { while (true) { operation.Enqueue(Encoding.UTF8.GetBytes(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " hello!")); Thread.Sleep(1); } }); Console.WriteLine("正在出隊(duì)"); Task.Factory.StartNew(() => { while (true) { var result = operation.Dnqueue(); if (result == null) { Thread.Sleep(1); } else { Console.WriteLine(Encoding.UTF8.GetString(result)); } } }); Console.ReadLine(); Console.WriteLine("正在取消訂閱"); operation.UnSubscribe(); Console.WriteLine("測(cè)試完成"); } } }
5.運(yùn)行結(jié)果:
至此C# 成功操作Rabbitmq完成。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C#?RabbitMQ的使用詳解
- C#通過(guò)rabbitmq實(shí)現(xiàn)定時(shí)任務(wù)(延時(shí)隊(duì)列)
- C#用RabbitMQ實(shí)現(xiàn)消息訂閱與發(fā)布
- C#利用RabbitMQ實(shí)現(xiàn)點(diǎn)對(duì)點(diǎn)消息傳輸
- C#使用RabbitMq隊(duì)列(Sample,Work,Fanout,Direct等模式的簡(jiǎn)單使用)
- c# rabbitmq 簡(jiǎn)單收發(fā)消息的示例代碼
- C#調(diào)用RabbitMQ實(shí)現(xiàn)消息隊(duì)列的示例代碼
- C#實(shí)現(xiàn)rabbitmq 延遲隊(duì)列功能實(shí)例代碼
- C#使用RabbitMQ發(fā)送和接收消息工具類的實(shí)現(xiàn)
相關(guān)文章
C#窗口轉(zhuǎn)向方式(由一個(gè)窗口,跳轉(zhuǎn)到另一個(gè)窗口)
這篇文章主要介紹了C#窗口轉(zhuǎn)向方式(由一個(gè)窗口,跳轉(zhuǎn)到另一個(gè)窗口)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07C# 批量生成隨機(jī)密碼必須包含數(shù)字和字母并用加密算法加密
這篇文章主要介紹了C# 批量生成隨機(jī)密碼必須包含數(shù)字和字母并用加密算法加密,需要的朋友參考下2017-01-01C#Js時(shí)間格式化問(wèn)題簡(jiǎn)單實(shí)例
這篇文章介紹了C#Js時(shí)間格式化問(wèn)題簡(jiǎn)單實(shí)例,有需要的朋友可以參考一下2013-10-10詳解C# WinForm如何實(shí)現(xiàn)自動(dòng)更新程序
在C/S這種模式中,自動(dòng)更新程序就顯得尤為重要,它不像B/S模式,直接發(fā)布到服務(wù)器上,瀏覽器點(diǎn)個(gè)刷新就可以了。本文就為大家準(zhǔn)備了WinForm實(shí)現(xiàn)自動(dòng)更新程序的示例代碼,需要的可以參考一下2022-10-10