简体   繁体   English

Rabbit Mq作为任务运行器

[英]Rabbit Mq as Task Runner

I am beginner to RabbitMQ integration. 我是RabbitMQ集成的初学者。 I was doing some experiment with RabbitMq and making it as task runner. 我正在使用RabbitMq做一些实验,并将其作为任务运行器。

For eg: Let say I have a class Tasks , which has a method called Foo () 例如:假设我有一个Tasks类,它具有一个称为Foo ()的方法。

public static string Foo(string test, int id)
{
  return "Admin" + test + id.ToString();
}

I have another class called Producer , which is declaring Queue for RabbitMQ. 我还有一个名为Producer的类,该类声明Queue for RabbitMQ。

using (var conn = factory.CreateConnection())
                {
                    using (var channel = conn.CreateModel())
                    {
                        channel.QueueDeclare(queue: "KKQueue", durable: false, exclusive: false, autoDelete: false, arguments: null);
                        var message = "Hello World";
                        channel.BasicPublish(exchange: string.Empty, routingKey: "KKQueue", basicProperties: null, body: Encoding.UTF8.GetBytes(message ));
                        Console.WriteLine(" [x] Sent {0}", message);
                    }
                    Console.WriteLine(" Press [enter] to exit.");
                }

Here we have to pass string as message to consumer. 在这里,我们必须将字符串作为消息传递给消费者。

Consumer class code will consume this queue message 消费者类代码将消耗此队列消息

using (var connection = factory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    channel.QueueDeclare(queue: "KKQueue", durable: false, exclusive: false, autoDelete: false, arguments: null);
                    var consumer = new EventingBasicConsumer(channel);
                    consumer.Received += (model, ea) =>
                    {
                        var message = Encoding.UTF8.GetString(ea.Body);


                        Console.WriteLine(" [x] Received {0}", message);
                    };
                    channel.BasicConsume(queue: "KKQueue", noAck: true, consumer: consumer);
                    Console.WriteLine(" Press [enter] to exit.");
                    Console.ReadLine();
                }

which is accepting string message . 这正在接受字符串消息 Is there a way, by which I can pass put my function call to Producer or Rabbit MQ Queue and let Consumee execute those function calls which are in queue. 有没有一种方法,我可以将函数调用传递给Producer或Rabbit MQ Queue,然后让Consumee执行队列中的那些函数调用。

I tried by serializing/deserializing the object and then using reflection to invoke the method in consumer code..I am looking for some alternate solution to it by using delegate or something. 我尝试通过序列化/反序列化对象,然后使用反射在消费者代码中调用该方法。.我正在寻找使用委托或其他方法的替代解决方案。 Any help will be greatly appreciated. 任何帮助将不胜感激。

No, you can't. 不,你不能。 You need to "ship" all the assembly code with the message, which is definitively a very bad idea. 您需要用消息“运送”所有汇编代码,这绝对是一个非常糟糕的主意。

You should add you task code library to you consumer app and then use the message to govern the task execution, but without sending the executable code with it. 您应该将任务代码库添加到消费者应用程序,然后使用该消息来控制任务执行,但不要随其发送可执行代码。

I personally use MassTransit for this kind of producer/consumer scenario; 我个人将MassTransit用于这种生产者/消费者方案。 for example you can have multiple consumers, one for each task, and you can activate the right consumer simply sending a different type of message through RMQ. 例如,您可以有多个使用者,每个使用者一个,并且您可以激活适当的使用者,只需通过RMQ发送不同类型的消息即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM