隊列工廠之RedisMQ


本次和大家分享的是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

安裝-》打開軟件,能看到如圖的界面:

-》點擊“Server”-》Add-》輸入一個昵稱,你redis服務端的ip,端口-》確認即可:

這個時候你redisclient的配置工作就完成了是不是很簡單啊,-》再來點擊剛才創建昵稱-》雙擊打開redis的第一個數據庫db0(這里就是在沒有指定數據庫位置時候存儲數據的地方)-》能看到你存儲的數據key:

如果想看某個name的數據直接雙擊對應的name就行了-》這里是我redis服務存儲的一個hash數據的截圖:

是不是很方便,這個客戶端可以直接刪除你不想要的數據-》右鍵選中您想刪除的name-》Delete即可刪除:

怎么樣,這個RedisClient工具學會了么,是不是挺簡單的呢;

 

» 封裝RedisMQ隊列的讀和寫

到這里終於來到我們代碼分享的時刻了,盡管QueueReposity-隊列工廠已經開源了源碼,這里還是單獨分享一次只有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工廠代碼就完成了,下面開始分享我們的測試用例;

 

» 隊列工廠之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)就分享完了,希望能給您帶來好的幫助,謝謝閱讀;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM