Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RabbitMQ how to split jobs to tasks and handle results

I have the following use case on a Spring-based Web application:

  • I need to apply the Competing Consumers EIP with the following twists: the messages in the queue are actually split tasks belonging to the same job. Therefore, I need to properly track when all tasks of a job get completed and their completion status in order to save the scenario either as COMPLETED or FAILED, log the outcome and notify by e.g. e-mail the users accordingly

So, given the requirements I described above, my question is:

  1. Can this be done with RabbitMQ and if yes how?
like image 293
kmandalas Avatar asked Jun 09 '17 18:06

kmandalas


Video Answer


2 Answers

I created a quick gist to show a very crude example of how one could do it. In this example, there is one producer and 2 consumers, 2 queues, one for sending by the producer ("SEND"), consumed by the consumers, and vice versa, consumers publish to the "RECV" queue and is consumed by the producer.

Now bear in mind this is a pretty crude example, as the Producer in that case send simply one job (a random amount of tasks between 0 and 5), and block until the job is done. A way to circumvent this would be to store in a Map a job id and the number of tasks, and every time check that the number of tasks done reported per job id.

like image 183
Adonis Avatar answered Oct 08 '22 05:10

Adonis


What you are trying to do is beyond the scope of RabbitMQ. RabbitMQ is for sending and receiving messages with ability to queue them. It can't track your job tasks for you.

You will need to have a "Job Storage" service. Whenever your consumer finishes the task, its updates the Job Storage service, marking task as done. Job storage service knows about how many tasks are in the job, and when last task is done, completes jobs as succeeded. There in this service, you will also implement all your other business logic, such as when to treat job as failed.

like image 33
Alex Buyny Avatar answered Oct 08 '22 04:10

Alex Buyny