pardahlman / RawRabbit

A modern .NET framework for communication over RabbitMq
MIT License
746 stars 144 forks source link

Consuming messages one at a time from the queue #377

Closed arrkaye closed 5 years ago

arrkaye commented 5 years ago

Hi,

I have the following code, with the intention that each message is taken in turn and handled by the Invoke(), before the next one is processed.

Please can you help me with what I'm doing wrong. I've been unable to figure it out from the docs.

public async Task Enqueue(T t) { await _busClient.PublishAsync(t, c => c.UsePublishAcknowledge(false).UsePublishConfiguration(a => a.OnDeclaredExchange(e => e.WithName(t.GetType().GetExchangeName())))); }

public async void Start() { await _busClient.SubscribeAsync<T>(async t => { try { await OnMessage?.Invoke(t); return new Ack(); } catch (RetryLaterException rle) { return Retry.In(rle.Retry); } catch { return Retry.In(_retry); } }, c => c.UseSubscribeConfiguration(s => s.FromDeclaredQueue(e => e.WithName(typeof(T).GetExchangeName())).OnDeclaredExchange(e => e.WithName(typeof(T).GetExchangeName())))); }

pardahlman commented 5 years ago

It sounds like you want to use the set ConsumerConcurrency, see this test case for an concrete example

await subscriber.SubscribeAsync<BasicMessage>(message =>
{
    // Handle here
}, c => c.UseConsumerConcurrency(1));

Hope this helps!

arrkaye commented 5 years ago

Amazing, Thanks!