Advertisement
Guest User

Untitled

a guest
Nov 11th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. public class RabbitMqConsumer : IRabbitMqConsumer, IDisposable
  2. {
  3. private string _queueName = "uservote";
  4. private IConnection _rabbitMqConnection;
  5. private IModel _queueChannel;
  6.  
  7. public RabbitMqConsumer()
  8. {
  9. var factory = Helpers.CreateConnectionFactory();
  10.  
  11. _rabbitMqConnection = factory.CreateConnection();
  12. _queueChannel = _rabbitMqConnection.CreateModel();
  13.  
  14. _queueChannel.QueueDeclare(queue: _queueName,
  15. durable: false,
  16. exclusive: false,
  17. autoDelete: false,
  18. arguments: null);
  19.  
  20. var consumer = new EventingBasicConsumer(_queueChannel);
  21.  
  22. consumer.Received += Consume;
  23.  
  24. _queueChannel.BasicConsume(queue: _queueName,
  25. autoAck: true,
  26. consumer: consumer);
  27. }
  28.  
  29. private void Consume(object model, BasicDeliverEventArgs eventArgs)
  30. {
  31.  
  32. }
  33.  
  34. public void Dispose()
  35. {
  36. _queueChannel.Close();
  37. _rabbitMqConnection.Close();
  38. }
  39. }
  40.  
  41.  
  42.  
  43.  
  44. public class RabbitMqProducer : IRabbitMqProducer, IDisposable
  45. {
  46. private string _queueName = "uservote";
  47. private string _routingKey = "uservote";
  48. private IConnection _rabbitMqConnection;
  49. private IModel _queueChannel;
  50.  
  51.  
  52. public RabbitMqProducer()
  53. {
  54. var factory = Helpers.CreateConnectionFactory();
  55.  
  56. _rabbitMqConnection = factory.CreateConnection();
  57. _queueChannel = _rabbitMqConnection.CreateModel();
  58.  
  59. _queueChannel.QueueDeclare(queue: _queueName,
  60. durable: false,
  61. exclusive: false,
  62. autoDelete: false,
  63. arguments: null);
  64. }
  65.  
  66. public void Publish(object objectToPublish, string routingKey = "uservote")
  67. {
  68. var body = Helpers.ObjectToByteArray(objectToPublish);
  69.  
  70. _queueChannel.BasicPublish(exchange: "",
  71. routingKey: _routingKey,
  72. basicProperties: null,
  73. body: body);
  74. }
  75.  
  76. public void Dispose()
  77. {
  78. _queueChannel.Close();
  79. _rabbitMqConnection.Close();
  80. }
  81. }
  82.  
  83. public static ConnectionFactory CreateConnectionFactory()
  84. {
  85. return new ConnectionFactory
  86. {
  87. HostName = "localhost",
  88. Password = "guest",
  89. UserName = "guest"
  90. };
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement