RedisMQ in the queue factory and redismq in the queue
This article shares with you the usage of RedisMQ queues. The first two articlesQueue Factory (MSMQ)AndQueue factory-RabbitMQThis section briefly introduces the establishment of the corresponding queue environment and the use of common methods. In addition, the RedisMQ shared in this article completes our queue factory"Sanjianke"Target Haha; the role of Redis is not limited to queues, but more often uses its key and value to store session or hash data, of course, this is not the content shared in this chapter. (You can refer to the usage scenarios and code sharing of redis in some previous articles .)QueueReposity-queue FactoryAfter the last article, I will share some netcore things later. After vs2017 was launched to create a netcore project, I found some changes with the previous version, for example: no project. json, how to configure and generate cross-platform programs, and so on, you need to learn and try one by one. There are still few articles to search online, and you have to read the English official website. Haha; I hope you will like this article and I hope you will enjoy it"Support for code scanning"And"Recommendation"Thank you!
» Redis installation and use of RedisClient
» Encapsulate read and write operations on the RedisMQ queue
» RedisMQ test cases for queue factories
Let's share it one step at a time:
» Redis installation and use of RedisClient
First, you need to download and install redis to use Redis. The previous article explains how to set up the redis service in windows, so I will not repeat it here. You can clickSet up the Redis server and connect it with the clientSo I will share with you how to use it.RedisClient ToolThe tool is simple and convenient to use. First, download it at this address:
Http://dlsw.baidu.com/sw-search-sp/soft/a2/29740/RedisClient20140730.1406883096.exe
Install-open the software and you will see the following interface:
-"Click" Server "-" Add-"and enter a nickname. Check the ip address and port of your redis Server:
At this time, your redisclient configuration is complete, isn't it easy, -Click Create nickname. Double-click to open the first database db0 in redis (where data is stored when no database location is specified) -"To view the data key you stored:
If you want to view the data of a name, double-click the corresponding name.-"Here is a hash data stored in my redis service:
Is it very convenient? This client can directly Delete the data you do not want-> right-click the name you want to Delete-> Delete to Delete:
How about this RedisClient tool? Is it quite simple;
» Encapsulate read and write operations on the RedisMQ queue
The time has finally come to share our code, even thoughQueueReposity-queue FactoryThe source code is already open-source. Here we will share only the RedisMQ code at a time. First, we will create a class named QRedisMQ to inherit the PublicClass. confClass <T>-> implements the IQueue interface, and finally comes with the code for implementing the interface method body:
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 ("to create a QRedisMQ queue, you must specify the queue: 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 ("Create a queue connection first");} 20 if (string. isNullOrWhiteSpace (name) {throw new Exception ("name cannot be blank");} 21 22 return redis. getListCount (name); 23} 24 25 public Message Read (string name = "Redis_01") 26 {27 if (redis = null) {throw new Excep Tion ("Create a queue connection first");} 28 if (string. isNullOrWhiteSpace (name) {throw new Exception ("name cannot be blank");} 29 30 var message = new Message (); 31 try32 {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 publi C bool Write (string content, string name = "Redis_01") 46 {47 if (redis = null) {throw new Exception ("Create a queue connection first ");} 48 if (string. isNullOrWhiteSpace (content) | string. isNullOrWhiteSpace (name) {throw new Exception ("content and name cannot be blank");} 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}
The Redis dll used here references the related nuget package:
The encapsulated Redis queue factory process is also: Create-Read-Write release (Dispose); with a specific RedisMQ implementation class, then you need to use the method provided by the factory mode to create the instance of this class:
1 /// <summary> 2 /// ============================== 3 // author: shiniu walk 3 4 // des: this factory is open-source, including queue MSMQ, RedisMQ, RabbitMQ 5 // blogs: http://www.cnblogs.com/wangrudong003/ 6 /// ============================== 7 // queue factory 8 /// </summary> 9 public class queueReposity <T> where T: class, IQueue, new () 10 {11 public static IQueue Current12 {13 get14 {15 return PublicClass. confClass <T>. current; 16} 17} 18}
Now the RedisMQ Factory Code is complete. Let's start to share our test cases;
» RedisMQ test cases for queue factories
By configuring the environment and encapsulating your own method above, a simple test case is written here, which can be divided into Server (add to Message Queue) and Client (get Message Queue). First, let's look at the Server code:
1 /// <summary> 2 // Queue Server Test Case 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 // instantiate the QRedisMQ object 18 var mq = QueueReposity <QRedisMQ>. current; 19 20 try21 {22 Console. writeLine ("Server creation: RedisMQ instance"); 23 mq. create (); 24 25 var num = 0; 26 do27 {28 Console. writeLin E ("Number of input loops (0 indicates end):"); 29 var readStr = Console. ReadLine (); 30 num = string. IsNullOrWhiteSpace (readStr )? 0: Convert. toInt32 (readStr); 31 32 Console. writeLine ("insert data:"); 33 for (int I = 0; I <num; I ++) 34 {35 var str = "My number is: "+ I; 36 mq. write (str); 37 Console. writeLine (str); 38} 39} while (num> 0); 40} 41 catch (Exception ex) 42 {43} 44 finally45 {46 Console. writeLine ("release. "); 47 mq. Dispose (); 48} 49 Console. ReadLine (); 50}
You can use our queue factory by creating (Create)-Read (Read) | Write-release (Dispose). Now we run this Server, then, enter four parameters:
The text description is displayed. the test data is inserted into the redis queue. Next we can view the data through the RedisClient tool described in section 1. Click the queue name, for example:
We can see the data we just inserted through the tool, and then we can read the queue through the client of the test case. The Code is as follows:
1 /// <summary> 2 // queue client Test Case 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 // instantiate the QRedisMQ object 18 var mq = QueueReposity <QRedisMQ>. current; 19 try20 {21 Console. writeLine ("Client creation: RedisMQ instance"); 22 mq. create (); 23 24 while (true) 25 {26 try27 {28 var total = Mq. total (); 29 if (total> 0) {Console. writeLine ("Number of Queues:" + total);} 30 31 var result = mq. read (); 32 if (result. body = null) {continue;} 33 Console. writeLine (string. format ("Accept queue {0 }:{ 1}", result. label, result. body); 34} 35 catch (Exception ex) 36 {Console. writeLine ("exception information:" + ex. message);} 37} 38} 39 catch (Exception ex) 40 {41 throw ex; 42} 43 finally44 {45 Console. writeLine ("release. "); 46 mq. Dispose (); 47} 48}
Run the generated exe to see the effect:
The figure shows that the data in the read queue is read in sequence as we thought. The test case test code for RedisMQ is okay. The above describes the code sharing and environment setup for RedisMQ encapsulation, here, the queue Factory (MSMQ, RabbitMQ, RedisMQ) is all over. I hope it will help you. Thank you for reading;