RedisMQ of Queue Factory

Keywords: C# Redis RabbitMQ Database Session

This time I share with you the usage of RedisMQ queue, the first two articles Queue Factory (MSMQ) and RabbitMQ of Queue Factory Briefly introduce the construction of the corresponding queue environment and the use of common methods, together with the EDIS MQ shared in this article, we have achieved the goal of "three swordsmen" in our queue factory. Redis's role is not limited to the queue, but more commonly uses its key, value form to store session or hash to store some commonly used data, of course, this is not the point of this chapter. Enjoy the content (some articles have talked about redis usage scenarios and code sharing before you can see), this Queue Reposity - Queue Factory At the end of the last article, the author may share something about netcore. After the simple creation of NETCORE project in vs2017, some changes have been found with the previous version, such as: without project.json, how to configure and generate cross-platform programs, etc. It needs a study and try. There are few articles searched on the internet, which rely solely on reading the official website in English. I hope you can like this article, and I hope you can have more "scanner support" and "recommendation" thank you!

 

Redis installation and use of RedisClient tools

Write and read encapsulated RedisMQ queues

RedisMQ test cases for queue factories

 

The next step is to share one footprint:

Redis installation and use of RedisClient tools

First of all, to use redis, you need to download and install Redis. Because the previous article has explained how to build redis service under windows, so I won't elaborate any more. You can click here. Build Redis server and connect with client So I'll share how to use RedisClient tool directly. It's easy 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 can see the interface as shown in the figure:

-> Click on "Server" - "Add-" and enter a nickname. You can confirm the ip, port - "redis server":

At this time, your redisclient configuration work is completed is not very simple, -"click on the nickname just created -" double-click open redis first database db0 (here is the place to store data when there is no designated database location) -"you can see the stored data key:?

If you want to see the data of a name, just double-click the corresponding name - "Here is a screenshot of hash data stored by my redis service:

Is it convenient for this client to delete the data you don't want directly - "Right-click on the name -"Delete"you want to delete and then delete:?

How about this RedisClient tool? Is it simple?

 

Write and read encapsulated RedisMQ queues

Now it's time for us to share our code, though Queue Reposity - Queue Factory Source code has been sourced, and only RedisMQ code is shared once. First, we create a class - "QRedisMQ" inheritance PublicClass. ConfClass < T > - "and then implement the interface IQueue. Finally, we implement the interface body code:

 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("Establish QRedisMQ Queues need to be specified: 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 Can not be empty"); }
21 
22             return redis.GetListCount(name);
23         }
24 
25         public Message Read(string name = "Redis_01")
26         {
27             if (redis == null) { throw new Exception("Create a queue connection first"); }
28             if (string.IsNullOrWhiteSpace(name)) { throw new Exception("name Can not be empty"); }
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("Create a queue connection first"); }
48             if (string.IsNullOrWhiteSpace(content) || string.IsNullOrWhiteSpace(name)) { throw new Exception("content and name Can not be empty"); }
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 dll of Redis used here refers to the relevant nuget package:

The encapsulated queue Redis factory process is the same: Create - "Read" - "Write" - "Dispose"; have a specific RedisMQ implementation class, and then use the method provided by the factory pattern to create an instance of this class:

 1   /// <summary>
 2     /// ==================
 3     /// author: Shenniu Walking 3
 4     /// des: The queue factory is open source, including queues with 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 Current
12         {
13             get
14             {
15                 return PublicClass.ConfClass<T>.Current;
16             }
17         }
18     }

Now that the RedisMQ factory code is complete, let's start sharing our test cases.

 

RedisMQ test cases for queue factories

By configuring the environment and encapsulating the method above, we write a simple test case, which is divided into Server (joining the message queue) and Client (getting the message queue). First, let's look at the code of the Server side:

 1 /// <summary>
 2     /// Queue server-side test cases
 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             //instantiation QRedisMQ object
18             var mq = QueueReposity<QRedisMQ>.Current;
19 
20             try
21             {
22                 Console.WriteLine("Server End creation: RedisMQ Example");
23                 mq.Create();
24 
25                 var num = 0;
26                 do
27                 {
28                     Console.WriteLine("Number of input loops(number,0 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             finally
45             {
46                 Console.WriteLine("Release.");
47                 mq.Dispose();
48             }
49             Console.ReadLine();
50         }

We use our queue factory by creating a process called Read | Write -, Dispose, and then we run the Server side and enter the parameters four times, respectively: Write -, Write -, Write -, Dispose, Write -, Write -, Write -, Write -, Write -, Write -, Write -, Writ

You can see the text description of the screenshot. These test data are inserted into the redis queue. Next, we use the RedisClient tool described in the first section to view the data. Click on the queue name, such as:

We can see the data we just inserted through the tool, and then we can read the queue through the client side of the test case, the specific code:

 1   /// <summary>
 2     /// Queue client test cases
 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             //instantiation QRedisMQ object
18             var mq = QueueReposity<QRedisMQ>.Current;
19             try
20             {
21                 Console.WriteLine("Client End creation: RedisMQ Example");
22                 mq.Create();
23 
24                 while (true)
25                 {
26                     try
27                     {
28                         var total = mq.Total();
29                         if (total > 0) { Console.WriteLine("Queue number:" + total); }
30 
31                         var result = mq.Read();
32                         if (result.Body == null) { continue; }
33                         Console.WriteLine(string.Format("Acceptance 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             finally
44             {
45                 Console.WriteLine("Release.");
46                 mq.Dispose();
47             }
48         }

Run the generated exe to see the effect:

It can be seen from the graph that the data of the read queue is read in turn as we want, and the code of the test case test RedisMQ is not a problem; the above explanations of code sharing and environment construction for encapsulating RedisMQ are shared here at the queue factory (MSMQ, RabbitMQ, RedisMQ), hoping to bring you good help, thank you for reading;

Posted by rajns on Sun, 21 Apr 2019 12:30:34 -0700