RabbitMQ将消息传递回调用客户端

本文关键字:调用 客户端 回调 消息传递 RabbitMQ | 更新日期: 2023-09-27 18:06:22

这是我第一次尝试在stackoverflow上发布问题。如果问题或方法有问题,请耐心等待。为了找到问题的答案,我做了大量的搜索,但还是没有找到答案。这就是我想做的。我编写了一个WCF服务,它连接到RabbitMQ本地主机来检索消息。我编写了一个使用WCF服务的控制台程序。现在我想让WCF从RabbitMQ接收到的任何消息都被传递回控制台程序,并且WCF仍在等待接收任何即将到来的消息。我看到的例子是使用委托和事件将消息传递回Windows窗体应用程序。我在为控制台程序实现这一点时遇到了困难。下面是我的WCF代码。

public class MessageQueueSvc : IService1
{
    public string HOST_NAME = "localhost";
    public string EXCHANGE_NAME = "MyExchange";
    public string QUEUE_NAME = "MyMessageQ1";
    public string ROUTING_KEY = "";
    protected bool isConsuming;
    public delegate void onReceiveMessage(byte[] message);
    public event onReceiveMessage onMessageReceived;
    public IModel Model { get; set; }
    public IConnection Connection { get; set; }
    public Subscription mSubscription { get; set; }
    public string Hello(string name)
    {
        return "Hello";
    }
    public void StartConsuming()
    {
        isConsuming = true;
        var connectionFactory = new ConnectionFactory();
        connectionFactory.HostName = "localhost";
        Connection = connectionFactory.CreateConnection();
        //connect the model, exchange, queue and bind them together
        bool durable = true;
        //after connection create a channel so that you can communicate with the broker thru this channel. 
        IModel channel = Connection.CreateModel();
        //after this declare an exchange and a queue and bind them together to this channel 
        if (!String.IsNullOrEmpty(EXCHANGE_NAME))
            channel.ExchangeDeclare(EXCHANGE_NAME, ExchangeType.Direct, durable);
        if (!String.IsNullOrEmpty(QUEUE_NAME))
        {
            channel.QueueDeclare(QUEUE_NAME, false, false, false, null);
            channel.QueueBind(QUEUE_NAME, EXCHANGE_NAME, ROUTING_KEY, null);
        }
        //once model,exchange, queue is created then start cosuming it. 
        bool autoAck = false;
        //create a subscription
        mSubscription = new Subscription(Model, QUEUE_NAME, autoAck);
        while (isConsuming)
        {
            BasicDeliverEventArgs e = mSubscription.Next();
            byte[] body = e.Body;
            String tempStr = System.Text.Encoding.UTF8.GetString(body);
            tempStr = "Processed message = " + tempStr;
            body = System.Text.Encoding.UTF8.GetBytes(tempStr);
            if (onMessageReceived != null)
            {
                //this is not working. I have to write an event handler or some sort of delegate to pass the message back to the calling program
                //and still waiting here for further messages from the server. 
                onMessageReceived(body);
            }
            mSubscription.Ack(e);
        }
    }
}

RabbitMQ将消息传递回调用客户端

我觉得你的方法有点不对。

在实例化MessageQueueSvc类时,您需要创建连接、通道和消费者,就像您目前在StartConsuming方法中所做的那样。但是,我不会创建一个无限循环来执行消息消费。

当控制台应用程序调用WCF客户端时,你应该从队列中消费。

public string GetMessage()
{
  BasicDeliverEventArgs e = mSubscription.Next();
  byte[] body = e.Body;
  String tempStr = System.Text.Encoding.UTF8.GetString(body);
  tempStr = "Processed message = " + tempStr;
  body = System.Text.Encoding.UTF8.GetBytes(tempStr);
  mSubscription.Ack(e);
  if (onMessageReceived != null)
  {
    return tempStr;
  } 
  else
  {
    return null; //or throw some kind of exception...
  }
}