Advertisement
Guest User

Untitled

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