multithreading - Concurrent message processing in RabbitMQ consumer -
i new rabbitmq please excuse me if question sound trivial. want publish message on rabbitmq processed rabbitmq consumer.
my consumer machine multi core machine (preferably worker role on azure). queuebasicconsumer pushes 1 message @ time. how can program utilize core can process multiple message concurrently.
- one solution open multiple channels in multiple threads , process message on there. in case how decide number of threads.
- another approach read message on main thread , create task , pass message task. in case have stop consuming messages in case there many message (after threshold) in progress. not sure how implemented.
thanks in advance
your second option sounds more reasonable - consume on single channel , spawn multiple tasks handle messages. implement concurrency control, use semaphore control number of tasks in flight. before starting task, wait semaphore become available, , after task has finished, signal semaphore allow other tasks run.
you haven't specified language/technology stack of choice, whatever - try utilise thread pool instead of creating , managing threads yourself. in .net, mean using task.run
process messages asynchronously.
example c# code:
using (var semaphore = new semaphoreslim(maxmessages)) { while (true) { var args = (basicdelivereventargs)consumer.queue.dequeue(); semaphore.wait(); task.run(() => processmessage(args)) .continuewith(() => semaphore.release()); } }
instead of controlling concurrency level yourself, might find easier enable explicit ack control on channel, , use rabbitmq consumer prefetch set maximum number of unacknowledged messages. way, never receive more messages wanted @ once.
Comments
Post a Comment