Asp.net MVC using EasyNetQ operating RabbitMQ

.Net operating RabbitMQ SDK is the most commonly used and EasyNetQ RabbitMQ.Client, EasyNetQ simple operation, easier to use.

Related Articles lot, but mostly translated from the official Demo, a bunch of console programs do [message publish / subscribe] in a production environment is clearly not what we expected, so this paper Asp.net MVC for example, describes how to use EasynetQ .

1. Create Asp.net MVC project

Project structure shown above, Services folder contains the message queue operations interfaces, Models folder contains the need to use test model.

 

2. Add dependence

Components used in this article include: EasyNetQ, Autofac, NlLog.

Add dependence:

  • Autofac, Autofac.Mvc5
  • EasyNetQ
  • NLog

Other dependencies such as: RabbitMQ.Client, Newtonsoft.Json automatically added, no separate manual added.

 

3. Message Queue Interface

3.1 IMQService

1     public interface IMQService
2     {
3         void InitMQ();
4 
5         void PublishMessage<T>(T message) where T : class;
6 
7         void SubscribeMessage();
8 
9     }

The interface we added three interfaces:

  • InitMQ:初始化消息队列
  • PublishMessage:消息发布
  • SubscribeMessage:消息订阅
 

3.2 RabbitMQService

 
 1     public class RabbitMQService:IMQService
 2     {
 3         IBus bus;
 4 
 5         public RabbitMQService()
 6         {
 7 
 8         }
 9         public void InitMQ()
10         {
11             bus = RabbitHutch.CreateBus("host=localhost", x => x.Register<IConsumerErrorStrategy>(_ => new AlwaysRequeueErrorStrategy()));
12 
13             //订阅消息
14             SubscribeMessage();
15         }
16 
17         public void PublishMessage<T>(T message)
18             where T:class
19         {
20             bus.Publish<T>(message);
21         }
22 
23         public void SubscribeMessage()
24         {
25             bus.SubscribeAsync<Question>("subscribe_question", x => HandleMessageAsync(x).Invoke(1));
26         }
27 
28         private Func<int,Task> HandleMessageAsync(Question question)
29         {
30             return async (id) =>
31             {
32                 if (new Random().Next(0, 2) == 0)
33                 {
34                     Console.WriteLine("Exception Happened!!!!");
35                     NLogHelper.Info("Exception Happened!!!!" + "   " + question.Text);
36                     throw new Exception("Error Hanppened!" + "   " + question.Text);
37                 }
38                 else
39                 {
40                     NLogHelper.Info("BEGIN");
41                     Thread.Sleep(10000);
42                     Console.WriteLine(string.Format("worker:{0},content:{1}", id, question.Text));
43                     NLogHelper.Info(string.Format("worker:{0},content:{1}", id, question.Text));
44                 }
45             };
46         }
47     }
 

RabbitMQService是对消息队列接口的实现,包含了队列的初始化、发布、订阅。

初始化方法仅需要在程序启动时注册一次
SubscribeMessage方法指明了对类型为 Question 的消息使用方法 HandleMessageAsync 处理。

未完待续。。。。


参考:
https://github.com/EasyNetQ/EasyNetQ/issues/734
https://github.com/EasyNetQ/EasyNetQ/issues/504
https://blog.csdn.net/chenludaniel/article/details/86138288



Guess you like

Origin www.cnblogs.com/imstrive/p/11078335.html