当前位置:   article > 正文

RabbitMQ RPC Consumer_rabbitmq rpcconsumer

rabbitmq rpcconsumer
 Task.Run(() => RbtMQHelp.Start());

  1. public class RbtMQHelp
  2. {
  3. static bool stopflag = true;
  4. public RbtMQHelp()
  5. {
  6. //
  7. // TODO: Add constructor logic here
  8. //
  9. }
  10. public static void Stop()
  11. {
  12. stopflag = false;
  13. }
  14. public static void Start()
  15. {
  16. var factory = new ConnectionFactory() { HostName = "127.0.0.1", UserName = "admin", Password = "6666m06" };
  17. var connection = factory.CreateConnection();
  18. var channel = connection.CreateModel();
  19. channel.QueueDeclare(queue: "DC", durable: true, exclusive: false, autoDelete: false, arguments: null);
  20. channel.BasicQos(0, 1, false);
  21. var consumer = new EventingBasicConsumer(channel);
  22. channel.BasicConsume(queue: "DC", autoAck: false, consumer: consumer);
  23. consumer.Received += (model, ea) =>
  24. {
  25. string response = null;
  26. var body = ea.Body;
  27. var props = ea.BasicProperties;
  28. var replyProps = channel.CreateBasicProperties();
  29. replyProps.CorrelationId = props.CorrelationId;
  30. try
  31. {
  32. var message = Encoding.UTF8.GetString(body);
  33. response = Control.SetResponse(message);
  34. }
  35. catch (Exception e)
  36. {
  37. response = e.ToString();
  38. log.Error(e.ToString());
  39. }
  40. finally
  41. {
  42. var responseBytes = Encoding.UTF8.GetBytes(response);
  43. channel.BasicPublish(exchange: "", routingKey: props.ReplyTo, basicProperties: replyProps, body: responseBytes);
  44. channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
  45. }
  46. };
  47. while (stopflag)
  48. {
  49. Thread.Sleep(500000);
  50. }
  51. }
  52. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/424590
推荐阅读
相关标签
  

闽ICP备14008679号