本次和大家分享的是RedisMQ隊列的用法,前兩篇文章 隊列工廠之(MSMQ) 和 隊列工廠之RabbitMQ 分别簡單介紹對應隊列環境的搭建和常用方法的使用,加上本篇分享的RedisMQ那麼就完成了咋們隊列工廠"三劍客"的目标了哈哈;Redis的作用不僅僅局限于隊列,更多的一般都使用它的key,value的形式來存儲session或者hash的方式存儲一些常用的資料,當然這不是本章分享的内容(之前有些文章有講過redis的使用場景和代碼分享各位可以看下),這 QueueReposity-隊列工廠
最後一篇結束後,筆者後面分享的可能是netcore方面的一些東西了,vs2017出來了簡單建立netcore項目之後發現與之前的版本有些變動,例如:沒有project.json,怎麼配置生成跨平台程式等問題,需要一個一個學習和嘗試,網上搜尋的文章還很少,全靠閱讀全英文的官網來學習了哈哈;希望大家能夠喜歡本篇文章,也希望各位多多"掃碼支援"和"推薦"謝謝!
» Redis安裝和RedisClient工具的使用
» 封裝RedisMQ隊列的讀和寫
» 隊列工廠之RedisMQ測試用例
下面一步一個腳印的來分享:
» Redis安裝和RedisClient工具的使用
首先要使用redis需要下載下傳安裝Redis,這裡由于之前的文章有講解在windows下怎麼搭建redis服務,是以不再贅述,各位可以點選
搭建Redis服務端,并用用戶端連接配接,是以我這裡直接分享怎麼使用RedisClient工具,這工具使用起來比較簡單和友善,首先去這個位址下載下傳:
http://dlsw.baidu.com/sw-search-sp/soft/a2/29740/RedisClient20140730.1406883096.exe
安裝-》打開軟體,能看到如圖的界面:

怎麼樣,這個RedisClient工具學會了麼,是不是挺簡單的呢;
到這裡終于來到我們代碼分享的時刻了,盡管
已經開源了源碼,這裡還是單獨分享一次隻有RedisMQ的代碼;首先建立一個名稱為:QRedisMQ的class-》繼承 PublicClass.ConfClass<T>-》再實作接口IQueue,最後就有了我們實作接口方法體代碼:1 /// <summary>
2 /// RedisMQ
3 /// </summary>
4 public class QRedisMQ : PublicClass.ConfClass<QRedisMQ>, IQueue
5 {
6 private IRedisClient redis = null;
7
8 public void Create()
9 {
10 if (string.IsNullOrWhiteSpace(this.ApiUrl) ||
11 string.IsNullOrWhiteSpace(this.UserPwd)) { throw new Exception("建立QRedisMQ隊列需要指定隊列:ApiUrl,UserPwd"); }
12
13 this.ApiKey = string.IsNullOrWhiteSpace(this.ApiKey) ? "6379" : this.ApiKey;
14 redis = redis ?? new RedisClient(this.ApiUrl, Convert.ToInt32(this.ApiKey), this.UserPwd);
15 }
16
17 public long Total(string name = "Redis_01")
18 {
19 if (redis == null) { throw new Exception("請先建立隊列連接配接"); }
20 if (string.IsNullOrWhiteSpace(name)) { throw new Exception("name不能為空"); }
21
22 return redis.GetListCount(name);
23 }
24
25 public Message Read(string name = "Redis_01")
26 {
27 if (redis == null) { throw new Exception("請先建立隊列連接配接"); }
28 if (string.IsNullOrWhiteSpace(name)) { throw new Exception("name不能為空"); }
29
30 var message = new Message();
31 try
32 {
33 message.Label = name;
34 var result = redis.DequeueItemFromList(name);
35 if (string.IsNullOrWhiteSpace(result)) { return message; }
36 message.Body = result;
37 }
38 catch (Exception ex)
39 {
40 throw new Exception(ex.Message);
41 }
42 return message;
43 }
44
45 public bool Write(string content, string name = "Redis_01")
46 {
47 if (redis == null) { throw new Exception("請先建立隊列連接配接"); }
48 if (string.IsNullOrWhiteSpace(content) || string.IsNullOrWhiteSpace(name)) { throw new Exception("content和name不能為空"); }
49 redis.EnqueueItemOnList(name, content);
50 return true;
51 }
52
53 public void Dispose()
54 {
55 if (redis != null)
56 {
57 redis.Dispose();
58 redis = null;
59 }
60 }
61
62
63 //public List<Message> ReadAll()
64 //{
65 // throw new NotImplementedException();
66 //}
67 }
這裡用到的Redis的dll是引用了相關的nuget包:
封裝的隊列Redis工廠流程同樣是:建立(Create)-》讀(Read)|寫(Write)-》釋放(Dispose);有了具體的RedisMQ實作類,然後還需利用工廠模式提供的方法來建立這個類的執行個體:
1 /// <summary>
2 /// ==================
3 /// author:神牛步行3
4 /// des:該列工廠開源,包括隊列有MSMQ,RedisMQ,RabbitMQ
5 /// blogs:http://www.cnblogs.com/wangrudong003/
6 /// ==================
7 /// 隊列工廠
8 /// </summary>
9 public class QueueReposity<T> where T : class,IQueue, new()
10 {
11 public static IQueue Current
12 {
13 get
14 {
15 return PublicClass.ConfClass<T>.Current;
16 }
17 }
18 }
到這兒RedisMQ工廠代碼就完成了,下面開始分享我們的測試用例;
通過上面配置環境和封裝自己的方法,這裡寫了一個簡單的測試用例,分為Server(加入消息隊列)和Client(擷取消息隊列),首先來看Server端的代碼:
1 /// <summary>
2 /// 隊列服務端測試用例
3 /// </summary>
4 class Program
5 {
6 static void Main(string[] args)
7 {
8 Redis_Server();
9
10 // RabbitMQ_Server();
11
12 //MSMQ_Server();
13 }
14
15 private static void Redis_Server()
16 {
17 //執行個體化QRedisMQ對象
18 var mq = QueueReposity<QRedisMQ>.Current;
19
20 try
21 {
22 Console.WriteLine("Server端建立:RedisMQ執行個體");
23 mq.Create();
24
25 var num = 0;
26 do
27 {
28 Console.WriteLine("輸入循環數量(數字,0表示結束):");
29 var readStr = Console.ReadLine();
30 num = string.IsNullOrWhiteSpace(readStr) ? 0 : Convert.ToInt32(readStr);
31
32 Console.WriteLine("插入資料:");
33 for (int i = 0; i < num; i++)
34 {
35 var str = "我的編号是:" + i;
36 mq.Write(str);
37 Console.WriteLine(str);
38 }
39 } while (num > 0);
40 }
41 catch (Exception ex)
42 {
43 }
44 finally
45 {
46 Console.WriteLine("釋放。");
47 mq.Dispose();
48 }
49 Console.ReadLine();
50 }
通過:建立(Create)-》讀(Read)|寫(Write)-》釋放(Dispose) 的流程來使用我們的隊列工廠,此時我們運作下這個Server端,然後分别錄入4次參數:
能看到截圖的文字描述,這些測試資料插入到了redis的隊列中,下面我們通過第一節說的RedisClient工具檢視資料,點選隊列名稱如:
通過工具能看到我們剛才插入的資料,然後我們來通過測試用例的client端讀取隊列,具體代碼:
1 /// <summary>
2 /// 隊列用戶端測試用例
3 /// </summary>
4 class Program
5 {
6 static void Main(string[] args)
7 {
8 RedisMQ_Client();
9
10 // RabbitMQ_Client();
11
12 //MSMQ_Client();
13 }
14
15 private static void RedisMQ_Client()
16 {
17 //執行個體化QRedisMQ對象
18 var mq = QueueReposity<QRedisMQ>.Current;
19 try
20 {
21 Console.WriteLine("Client端建立:RedisMQ執行個體");
22 mq.Create();
23
24 while (true)
25 {
26 try
27 {
28 var total = mq.Total();
29 if (total > 0) { Console.WriteLine("隊列條數:" + total); }
30
31 var result = mq.Read();
32 if (result.Body == null) { continue; }
33 Console.WriteLine(string.Format("接受隊列{0}:{1}", result.Label, result.Body));
34 }
35 catch (Exception ex)
36 { Console.WriteLine("異常資訊:" + ex.Message); }
37 }
38 }
39 catch (Exception ex)
40 {
41 throw ex;
42 }
43 finally
44 {
45 Console.WriteLine("釋放。");
46 mq.Dispose();
47 }
48 }
運作生成的exe,看效果:
通過圖形能看出讀取隊列的資料正如我們想的那樣依次讀取,測試用例測試RedisMQ的代碼沒問題;以上對封裝RedisMQ的代碼分享和環境搭建講解,到這裡隊列工廠(MSMQ,RabbitMQ,RedisMQ)就分享完了,希望能給您帶來好的幫助,謝謝閱讀;