Already closed: The AMQP operation was interrupted: AMQP close-reason, initiated by Peer, code=505,

C#使用rabbitmq在接收消息事件处理中,同时发布消息报错:

Already closed: The AMQP operation was interrupted: AMQP close-reason, initiated by Peer, code=505, text='UNEXPECTED_FRAME - expected content header for class 60, got non content header frame instead', classId=60, methodId=40

解决办法是将接收事件代码里面末尾加个线程休眠“System.Threading.Thread.Sleep(1);”,不然发布方法调用报错,参考代码

using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleHandleMessage
{
    class Program
    {
        static void Main(string[] args)
        {
            //处理消息
            ConnectionFactory factory = new ConnectionFactory();
            factory.UserName = "guest";
            factory.Password = "guest";
            factory.HostName = "localhost";
            IConnection conn = factory.CreateConnection();
            IModel channel = conn.CreateModel();
			
			//用于写入消息
            ConnectionFactory factory5 = new ConnectionFactory();
            factory5.HostName = "localhost";
            factory5.UserName = "raadmin";
            factory5.Password = "raadmin";
            factory5.AutomaticRecoveryEnabled = true;//自动连接恢复
            IConnection conn5 = factory5.CreateConnection();
            IModel channel5 = conn5.CreateModel();

            //*************订阅消息,生产者生产消息后,会主动推送过来
            //testMq表示一个队列
            channel.QueueDeclare("testMq", false, false, false, null);
            var consumer = new EventingBasicConsumer(channel);//消费者
            //注册消费者订阅,false获取消息后不会从队列移除,true会移除       
            channel.BasicConsume("testMq",false, consumer);

            //此获取消息事件,处理消息时,会自动从最远的时间开始处理,按顺序来的,最新的消息在最后处理
            consumer.Received += (s, e) =>
          {
              var bodyBytes = e.Body;
              var message = Encoding.UTF8.GetString(bodyBytes.ToArray());

               //处理消息业务
              Task.Run(() =>
             {
                 try
                 {
                     LogHelpter.AddLog("收到:" + message);//记录本地txt日志
                     Console.WriteLine("接收的消息" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") + ":" + message + ",ThreadId=" + Thread.CurrentThread.ManagedThreadId);
                     //确认已经收到消息,此行为通知消息队列移除消息
                     channel.BasicAck(e.DeliveryTag, false);
                 }
                 catch (Exception ex)
                 {
                     Console.WriteLine("确认消息报错:" + ex.Message);
                     LogHelpter.AddLog("确认消息报错:" + ex.Message);//记录本地txt日志
                 }                 
             });

              //记录已经处理的消息,写入另一个队列
              try
              {
                  channel5.BasicPublish(exchange: "", routingKey: "testMq_handle", basicProperties: null, body: bodyBytes);
              }
              catch (Exception ex)
              {
                  LogHelpter.AddLog("记录处理消息异常," + ex.Message);//记录本地txt日志
              }
              System.Threading.Thread.Sleep(1);//此行代码必须,不然写入队列消息会报错
//错误消息:Already closed: The AMQP operation was interrupted: AMQP close-reason, initiated by Peer, code=505, text='UNEXPECTED_FRAME - expected content header for class 60, got non content header frame instead', classId=60, methodId=40
          };


            //channel.Close();
            //conn.Close();

            Console.ReadLine();
        }

 
    }
}

 

你可能感兴趣的:(rabbitmq,C#,c#,rabbitmq)