Rabbitmq无法监听后续消息

现象:

消息队列在处理完一条消息后,无法继续监听后续消息。

首先,系统启动时要启动接收方法如下:

 1         protected void Application_Start()
 2         {
 3             RouteTable.Routes.MapHubs();
 4             AreaRegistration.RegisterAllAreas();
 5 
 6             RegisterGlobalFilters(GlobalFilters.Filters);
 7             RegisterRoutes(RouteTable.Routes);
 8             Register(GlobalConfiguration.Configuration);
 9             //GlobalHost.ConnectionManager.GetHubContext<ProcessMonitoring>().Clients.All.receive("ss");
10             //RouteTable.Routes.MapHubs();
11             ReceiveSuccessMessage();
12         }
View Code

其次,接收方法如下,

 1          /// <summary>
 2          /// 反馈成功的消息队列
 3          /// </summary>
 4          public static void ReceiveSuccessMessage()
 5          {
 6              ConnectionFactory factory = CreateConnectionFactory();
 7              using (var connection = factory.CreateConnection())
 8              {
 9                  using (var channel = connection.CreateModel())
10                  {
11                      //输入1,那如果接收一个消息,但是没有应答,则客户端不会收到下一个消息
12                      channel.BasicQos(0, 1, false);
13                      var consumer = new EventingBasicConsumer(channel);
14                      channel.BasicConsume(receiveQueueSuccess, false, consumer);
15                      consumer.Received += (model, ea) =>
16                      {
17                          try
18                          {
19                              var body = ea.Body;
20                              var message = Encoding.UTF8.GetString(body);
21  
22                              //反馈信息处理
23                              ProcessFeedbackInfoForQueue(message);
24                              channel.BasicAck(ea.DeliveryTag, false);
25                          }
26                          catch (Exception ex)
27                          {
28                              Yingu.Common.SysLog.Write("接受消息队列失败:", string.Format("反馈成功消息队列  时间:{0} :失败原因{1}", DateTime.Now, ex.Message + ex.StackTrace));
29                              channel.BasicAck(ea.DeliveryTag, false);
30                          }
31                      };
32  
33                  };
34  
35              };
36          }
View Code

 解决方法:

将第二个方法改为

 1         /// <summary>
 2         /// 反馈成功的消息队列
 3         /// </summary>
 4         public static void ReceiveSuccessMessage()
 5         {
 6             ConnectionFactory factory = CreateConnectionFactory();
 7             var connection = factory.CreateConnection();
 8             var channel = connection.CreateModel();
 9             //输入1,那如果接收一个消息,但是没有应答,则客户端不会收到下一个消息
10             channel.BasicQos(0, 1, false);
11             var consumer = new EventingBasicConsumer(channel);
12             channel.BasicConsume(receiveQueueSuccess,false,consumer);
13             consumer.Received += (model, ea) =>
14             {
15                 try
16                 {
17                     var body = ea.Body;
18                     var message = Encoding.UTF8.GetString(body);
19 
20                     //反馈信息处理
21                     ProcessFeedbackInfoForQueue(message);
22                     channel.BasicAck(ea.DeliveryTag, false);
23                 }
24                 catch (Exception ex)
25                 {
26                     Yingu.Common.SysLog.Write("接受消息队列失败:", string.Format("反馈成功消息队列  时间:{0} :失败原因{1}", DateTime.Now, ex.Message + ex.StackTrace));
27                     channel.BasicAck(ea.DeliveryTag, false);
28                 }
29             };
30         }
View Code

原因是每次处理一条消息后,接收方法会断开连接,无法继续监听后续消息。

 

posted @ 2016-11-14 23:10  王小六Charles  阅读(4362)  评论(1编辑  收藏  举报