A complete example of C # RabbitMQ operations,
1. Download RabbitMQ
Http://www.rabbitmq.com/install-windows.html
Ii. Download OTP
Http://www.erlang.org/downloads
3. Install OTP and RabbitMQ
4. Configure RabbitMQ
Find the bat directory
Execute related commands
1. Add User PasswordRabbitmqctl add_user wenli
2. Set wenli as AdministratorRabbitmqctl set_user_tags wenli administrator
3. Start RabbitMQ web ManagementRabbitmq-plugins enable rabbitmq_management
4. Create a virtual host
5. Set User Permissions
Click the user name to set
Grant the virtual hosts permission to the user wenli
6. Create Exchanges
5. Create a C # console
1. Download RabbitMQ driver https://github.com/yswenli/Wenli.Data.RabbitMQ/releases/tag/Release1.0.0
2. Add reference
3. Add Configuration
4. Test code:
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 Program11 {12 static void Main (string [] args) 13 {14 Console. title = "Wenli. data. rabbitMQ. console "; 15 Console. writeLine ("connecting to mq"); 16 17 try18 {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 ("connecting to subscription [" + topic + "]"); 40 41 operation. subscribe (); 42 43 Console. writeLine ("joining"); 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 ("coming out"); 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 else72 {73 Console. writeLine (Encoding. UTF8.GetString (result); 74} 75} 76}); 77 78 Console. readLine (); 79 80 Console. writeLine ("canceling subscription"); 81 82 operation. unSubscribe (); 83 84 Console. writeLine ("test completed"); 85} 86} 87}
5. Running result:
So far, C # has successfully operated Rabbitmq.
Reprinted please indicate the source of this article: http://www.cnblogs.com/yswenli/p/7446919.html
For more information, see the star author's github: https://github.com/yswenli/Wenli.Data.RabbitMQ.
If you find any questions or suggestions in this article, feel free to contact us ~