一個C#操做RabbitMQ的完整例子

 

1、下載RabbitMQ

http://www.rabbitmq.com/install-windows.htmlhtml

 

2、下載OTP

http://www.erlang.org/downloadsgit

 

3、安裝OTP、RabbitMQ

4、配置RabbitMQ

找到bat的目錄github

執行相關命令web

 

 1.添加用戶密碼 rabbitmqctl add_user wenli wenliwindows

 2.設置wenli爲管理員 rabbitmqctl set_user_tags wenli administrator測試

 3.啓動RabbitMQ的web管理 rabbitmq-plugins enable rabbitmq_managementui

 4.建立virtual host spa

5.設置用戶權限3d

點擊用戶名進行設置code

將virtual hosts 權限賦給用戶wenli

 

6.建立Exchanges

 

五.建立C# console

  1.下載RabbitMQ驅動 https://github.com/yswenli/Wenli.Data.RabbitMQ/releases/tag/Release1.0.0

  2.添加引用     

  3.添加配置

     

   4.測試代碼:

    

 1 using System;
 2 using System.Text;
 3 using System.Threading;
 4 using System.Threading.Tasks;
 5 
 6 namespace Wenli.Data.RabbitMQ.Console
 7 {
 8     using Console = System.Console;
 9 
10     class Program
11     {
12         static void Main(string[] args)
13         {
14             Console.Title = "Wenli.Data.RabbitMQ.Console";
15             Console.WriteLine("正鏈接到mq");
16 
17             try
18             {
19                 Test();
20             }
21             catch (Exception ex)
22             {
23                 Console.WriteLine("err:" + ex.Message + ex.Source + ex.StackTrace);
24             }
25 
26             Console.Read();
27         }
28 
29 
30         static void Test()
31         {
32 
33             var topic = "testtopic";
34 
35             var cnn = RabbitMQBuilder.Get(MQConfig.Default).GetConnection();
36 
37             var operation = cnn.GetOperation(topic);
38 
39             Console.WriteLine("正鏈接到訂閱【" + topic + "");
40 
41             operation.Subscribe();
42 
43             Console.WriteLine("正在入隊");
44 
45             Task.Factory.StartNew(() =>
46             {
47                 while (true)
48                 {
49                     operation.Enqueue(Encoding.UTF8.GetBytes(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "     hello!"));
50                     Thread.Sleep(1);
51                 }
52             });
53 
54 
55 
56 
57             Console.WriteLine("正在出隊");
58 
59 
60 
61             Task.Factory.StartNew(() =>
62             {
63                 while (true)
64                 {
65                     var result = operation.Dnqueue();
66 
67                     if (result == null)
68                     {
69                         Thread.Sleep(1);
70                     }
71                     else
72                     {
73                         Console.WriteLine(Encoding.UTF8.GetString(result));
74                     }
75                 }
76             });
77 
78             Console.ReadLine();
79 
80             Console.WriteLine("正在取消訂閱");
81 
82             operation.UnSubscribe();
83 
84             Console.WriteLine("測試完成");
85         }
86     }
87 }

  5.運行結果:

   

至此C# 成功操做Rabbitmq完成

 

 


轉載請標明本文來源:http://www.cnblogs.com/yswenli/p/7446919.html
更多內容歡迎star做者的github:https://github.com/yswenli/Wenli.Data.RabbitMQ若是發現本文有什麼問題和任何建議,也隨時歡迎交流~

相關文章
相關標籤/搜索