【发布时间】:2013-03-25 13:09:15
【问题描述】:
我在消费者内部接收来自 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);
}
}
【问题讨论】:
-
我的问题与此类似。 stackoverflow.com/questions/3972756/…
-
我编写了一个新方法,将处理后的消息作为输入字符串传递。在该方法中,我创建了一个连接工厂、一个新模型并将消息发布到不同的队列中。这就是我采取的方法。
标签: c# .net rabbitmq message-queue