Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# rabbitmq消费者成为生产者_C#_.net_Rabbitmq_Message Queue - Fatal编程技术网

C# rabbitmq消费者成为生产者

C# rabbitmq消费者成为生产者,c#,.net,rabbitmq,message-queue,C#,.net,Rabbitmq,Message Queue,我正在消费者内部接收来自RabbitMQ的消息。我必须处理该消息并将处理后的消息发布到另一个队列中。我将如何做到这一点 我的代码是 using (IConnection connection = factory.CreateConnection()) { using (IModel channel = connection.CreateModel()) { if (!String.IsNullOrEmpty(EXCHANGE_NAME))

我正在消费者内部接收来自RabbitMQ的消息。我必须处理该消息并将处理后的消息发布到另一个队列中。我将如何做到这一点

我的代码是

using (IConnection connection = factory.CreateConnection())
{
    using (IModel channel = connection.CreateModel())
    {
        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);

        string data = "";
        EventingBasicConsumer consumer = new EventingBasicConsumer();
        consumer.Received += (o, e) =>
        {
            //This is the received message
            data = data + Encoding.ASCII.GetString(e.Body) + Environment.NewLine;
            string processed_data = "processed data = " + data; 
            //I want to write some code here to post the processed message to a different queue.
            //or other idea is "can I use duplex services? 

        };
        string consumerTag = channel.BasicConsume(QUEUE_NAME, true, consumer);

        channel.QueueBind(QUEUE_NAME, EXCHANGE_NAME, ROUTING_KEY, null);
        channel.QueueUnbind(QUEUE_NAME, EXCHANGE_NAME, ROUTING_KEY, null);
    }
}

底线是,您可以共享线程之间的连接,但不能共享通道。因此,在您的示例中,您可以使用相同的连接,但是当您想要发布时,您需要创建一个新的通道(因为consumer.Received事件将在不同的线程上引发):


确保在您想要停止消费之前,不要处理您的消费者渠道。连接也是如此。这是一个常见的错误。

我的问题与此类似。我编写了一个新方法,将处理后的消息作为输入字符串传递。在该方法中,我创建了一个连接工厂,一个新模型,并将消息发布到另一个队列中。这就是我采取的方法。迈克,谢谢你的指点。这也正是我所关心的。不希望最终陷入死锁或内存泄漏。
using (IConnection connection = factory.CreateConnection())
{
    using (IModel channel = connection.CreateModel())
    {
        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);

        string data = "";
        EventingBasicConsumer consumer = new EventingBasicConsumer();
        consumer.Received += (o, e) =>
        {
            //This is the received message
            data = data + Encoding.ASCII.GetString(e.Body) + Environment.NewLine;
            string processed_data = "processed data = " + data; 
            //I want to write some code here to post the processed message to a different queue.
            //or other idea is "can I use duplex services? 

            using (IModel channel = connection.CreateModel())
            {
                channel.Publish( ... );
            }

        };
        string consumerTag = channel.BasicConsume(QUEUE_NAME, true, consumer);

        channel.QueueBind(QUEUE_NAME, EXCHANGE_NAME, ROUTING_KEY, null);
        channel.QueueUnbind(QUEUE_NAME, EXCHANGE_NAME, ROUTING_KEY, null);

        // don't dispose of your channel until you've finished consuming
    }

    // don't dispose of your connection until you've finished consuming
}