Rabbit MQ发布者发送消息但订阅者无法触发

时间:2017-12-11 14:51:46

标签: c# rabbitmq rabbitmq-exchange

我是兔子新手。我已经实现了发布者和订阅者方法来推送和阅读消息。

我无法读取已经推入队列的消息。例如,我的订阅者项目已关闭,发布者应用程序正在运行。 Publisher将数据推送到rabbit mq。现在我启动订阅者应用程序。我的方法已订阅并成功,但我没有收到已经推送的消息。

MessageQueueBase.cs

                ConnectionFactory connectionFactory = new ConnectionFactory();

                //Start :Connectoin Factory for RabbitMQ
                connectionFactory.HostName = QueueSetting.MessageQueueHostName;
                connectionFactory.UserName = QueueSetting.MessageQueueUserName;
                connectionFactory.Password = QueueSetting.MessageQueuePassword;
                connectionFactory.VirtualHost = "/";
                if(QueueSetting.Port > 0 )
                {
                    connectionFactory.Port = QueueSetting.Port;
                }

                //End :Connectoin Factory for RabbitMQ

                Connection = connectionFactory.CreateConnection();
                Model = Connection.CreateModel();

                bool durable = true;

                if (!String.IsNullOrEmpty(QueueSetting.MessageQueueExchangeName))
                {
                    Model.ExchangeDeclare(QueueSetting.MessageQueueExchangeName, QueueSetting.ExchangeType, durable, false);
                }

Publisher.cs

public class QueuePublisher : MessageQueueBase, IQueuePublisher
    {
        IBasicProperties basicProperties;
        private bool queueReachable = false;

        public QueuePublisher(QueueSettings queueSetting)
            : base(queueSetting)
        {
            queueReachable = base.ConnectToRabbitMQ();
            basicProperties = Model.CreateBasicProperties();
            basicProperties.Persistent = true;
        }


        public void SendMessage<T>(T data)
        {
            if (!queueReachable)
            {
                throw new Exception(string.Format($"Unable to reach the queue { QueueSetting.MessageQueueExchangeName }"));   // TODO: Pass proper exception here.
            }

            var message = MessageQueueHelpers.Serialize(data);
            Model.BasicPublish(QueueSetting.MessageQueueExchangeName, string.Empty, basicProperties, message);
        }


        public void SendMessage<T>(T data, QueueRoutingKey routingKey)
        {
            if (!queueReachable)
            {
                throw new Exception(string.Format($"Unable to reach the queue {QueueSetting.MessageQueueExchangeName}"));   // TODO: Pass proper exception here.
            }

            var message = MessageQueueHelpers.Serialize(data);
            Model.BasicPublish(QueueSetting.MessageQueueExchangeName, routingKey.ToString(), basicProperties, message);

        }

Subscriber.cs

public class QueueSubscriber : MessageQueueBase, IQueueSubscriber
    {
        private Subscription mSubscription;
        private QueueRoutingKey routingKey;
        private bool queueReachable = false;
        private string queueName = string.Empty;


        public QueueSubscriber(QueueSettings queueSetting)
            : base(queueSetting)
        {
            routingKey = queueSetting.RouteKey;
            queueReachable = base.ConnectToRabbitMQ();
        }


        private delegate void ConsumeDelegate();
        Action<byte[]> messageReceived;


        public void StartSubscribe(Action<byte[]> onMessageReceived)
        {
            if (onMessageReceived == null)
            {
                throw new ArgumentNullException("onMessageReceived", "Subscriber method is not found");
            }

            if (!queueReachable)
            {
                throw new Exception(string.Format($"Unable to reach the queue {QueueSetting.MessageQueueExchangeName}"));   // TODO: Pass proper exception here.
            }

            messageReceived = onMessageReceived;
            Model.BasicQos(0, 1, false);
            queueName = Model.QueueDeclare();
            Model.QueueBind(queueName, QueueSetting.ExchangeType, routingKey.ToString());
            IsConsuming = true;
            ConsumeDelegate c = new ConsumeDelegate(Consume);
            c.BeginInvoke(null, null);
        }


        /// <summary>Trigger Consume method</summary>
        private void Consume()
        {
            bool autoAck = false;

            //Create a subscription
            mSubscription = new Subscription(Model, queueName, autoAck);

            while (IsConsuming)
            {
                BasicDeliverEventArgs e = mSubscription.Next();
                byte[] body = e.Body;
                messageReceived(body);
                mSubscription.Ack(e);
            }
        }

将我的消息推送为

publisher.SendMessage(acknowledgement, routingKey);

订阅方法为

IQueueSubscriber subscriber = new QueueSubscriber(queueSettings);
            subscriber.StartSubscribe(ProcessData);

void ProcessData(byte[] data)
{
   // Deserialize the data and do work
}

0 个答案:

没有答案