简体   繁体   中英

How do I do async RPC calls with RabbitMq

I'm trying to do a RestApi (asp.net core) that calls the backend (C#) through RabbitMq. To handle many requests I will need to call the backend asynchronously.

For me the example code from rabbitmq seem not to be thread-safe because it dequeues messages until the one with the correct correlation id is returned. All others will be ignored. (link: https://www.rabbitmq.com/tutorials/tutorial-six-dotnet.html )

  while(true)
  {
      var ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
      if(ea.BasicProperties.CorrelationId == corrId)
      {
          return Encoding.UTF8.GetString(ea.Body);
      }
  }

I'm thinking in the following possibilities:


Possibility 1:

I could use the SimpleRpcClient and create for each request a own instance. This will cause that for each request a new queue to reply gets created.

Possibility 2:

Create a own RPC client that creates one reply queue (probably per request type) and returns the right response to the right request depending on the correlation id.


What is the best practice to make multiple calls asynchronous? Are there already implementations for the second possibility or do I need to implement this by myself?

  1. Design a job queue, Push job to queue from generator and forget so that job generator remains responsive
  2. Have multiple workers equal to number of available CPU threads ( for optimized performance ) to process jobs
  3. Each worker to deque job from main queue and put it with results along in new queue.
  4. Keep features for
    1. Not to process *too old** jobs.
    2. Terminate long running jobs.
    3. Pick high priority jobs first.
    4. If permitted design remote job runner nodes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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