Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

InMemoryConsumerClient.cs 2.0 KiB

4 лет назад
4 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using DotNetCore.CAP.Messages;
  5. using DotNetCore.CAP.Transport;
  6. using Microsoft.Extensions.Logging;
  7. namespace DotNetCore.CAP.Test.FakeInMemoryQueue
  8. {
  9. internal sealed class InMemoryConsumerClient : IConsumerClient
  10. {
  11. private readonly ILogger _logger;
  12. private readonly InMemoryQueue _queue;
  13. private readonly string _subscriptionName;
  14. public InMemoryConsumerClient(
  15. ILogger logger,
  16. InMemoryQueue queue,
  17. string subscriptionName)
  18. {
  19. _logger = logger;
  20. _queue = queue;
  21. _subscriptionName = subscriptionName;
  22. }
  23. public event EventHandler<TransportMessage> OnMessageReceived;
  24. public event EventHandler<LogMessageEventArgs> OnLog;
  25. public BrokerAddress BrokerAddress => new BrokerAddress("InMemory", string.Empty);
  26. public void Subscribe(IEnumerable<string> topics)
  27. {
  28. if (topics == null) throw new ArgumentNullException(nameof(topics));
  29. foreach (var topic in topics)
  30. {
  31. _queue.Subscribe(_subscriptionName, OnConsumerReceived, topic);
  32. _logger.LogInformation($"InMemory message queue initialize the topic: {topic}");
  33. }
  34. }
  35. public void Listening(TimeSpan timeout, CancellationToken cancellationToken)
  36. {
  37. while (!cancellationToken.IsCancellationRequested)
  38. {
  39. cancellationToken.WaitHandle.WaitOne(timeout);
  40. }
  41. }
  42. public void Commit(object sender)
  43. {
  44. // ignore
  45. }
  46. public void Reject(object sender)
  47. {
  48. // ignore
  49. }
  50. public void Dispose()
  51. {
  52. _queue.ClearSubscriber();
  53. }
  54. #region private methods
  55. private void OnConsumerReceived(TransportMessage e)
  56. {
  57. OnMessageReceived?.Invoke(null, e);
  58. }
  59. #endregion private methods
  60. }
  61. }