0

Would like to know how to enable multiple consumers for Apache.NMS.AMQP client. Have tried with multiple sessions for same queue with different consumer - but the listener is only getting called for one consumer per queue. Below is the sample code. Ignore the connection per queue as I thought that might be the cause, but doesn't work. Given consumer one name -ConsumerName to identify consumer being called.

var queueClientLogger = loggerFactory.CreateLogger<QueueClient>();
var queueClient1 = new QueueClient(queueClientLogger, "Q/test1");
await queueClient1.InitializeAsync();

var queueClient2 = new QueueClient(queueClientLogger, "Q/test1");
await queueClient2.InitializeAsync();

var queueClient3 = new QueueClient(queueClientLogger, "Q/test2");
await queueClient3.InitializeAsync();

-----------------------------------------------
internal class QueueClient : IDisposable
{
  private readonly ILogger<QueueClient> logger;
  private IMessageConsumer consumer;
  private bool disposedValue;

  #region constructor

  public QueueClient(ILogger<QueueClient> logger, string queueName)
  {
      this.logger = logger;
      QueueName = queueName;
      ConsumerName = $"{QueueName}-{Guid.NewGuid()}";
  }

  #endregion

  #region Properties

  internal string? QueueName { get; private set; }
  internal string ConsumerName { get; private set; }
  internal Apache.NMS.ISession Session { get; private set; }
  internal Apache.NMS.IConnection Connection { get; private set; }


  #endregion

  #region Methods

  internal async Task InitializeAsync()
  {
      string brokerUri = $"amqp://localhost:5672";  // Default port
      NMSConnectionFactory factory = new NMSConnectionFactory(brokerUri);
      Connection = await factory.CreateConnectionAsync();
      await Connection.StartAsync();
      Session = await Connection.CreateSessionAsync(AcknowledgementMode.AutoAcknowledge);
      Apache.NMS.IDestination dest = await Session.GetQueueAsync(QueueName);
      consumer = await Session.CreateConsumerAsync(dest);
      consumer.Listener += Consumer_Listener;
  }

  private void Consumer_Listener(Apache.NMS.IMessage message)
  {
      logger.LogInformation($"{ConsumerName}: Message from queue - {QueueName}");            
      Thread.Sleep(1000);
      string content = string.Empty;
      if (message is ITextMessage)
      {
          ITextMessage? txtMsg = message as ITextMessage;
          content = txtMsg?.Text ?? "";
      }
      else if (message is IBytesMessage)
      {
          IBytesMessage? bytesMsg = message as IBytesMessage;
          if (bytesMsg == null)
          {
              content = $"NULL message received";
          }
          else
          {
              content = Encoding.UTF8.GetString(bytesMsg.Content);
          }
      }
      else
      {
          content = "Unexpected message type: " + message.GetType().Name;
      }
      logger.LogInformation($"{content}");
  }
  
  //Ignore IDosposable code
}
Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
Subasish
  • 92
  • 11
  • I'd recommend simplifying this example to make it clearer what the issue is, as well the description as it is quite hard to tease out what your actual problem is. – Tim Bish Jun 14 '22 at 14:02
  • Sure ..will try.. Problem statement: Want to add multiple client consumers(Apache.NMS.AMQP) instead of one for consuming messages received in queue using AMQP protocol. Reason being, order of messages is not important and I can speed up on the consumption side. To enable this read somewhere that adding more sessions per queue helps us adding multiple consumers per session. Within same session, we cant add multiple consumers. But I tried doing that as seen in the code, but it always calls the last consumer for a particular queue. – Subasish Jun 17 '22 at 08:36
  • @TimBish In the sample code to make things simpler, I have added different instance of QueueClient which internally creates a different session and also connection(not needed) for a particular queue. I have deliberately passed same queuename(Q/test1) for instance queueClient1 and queueClient2 to add two consumers. Does this help? – Subasish Jun 17 '22 at 08:41

0 Answers0