RabbitMQ wait for multiple queues to finish

后端 未结 5 1789
傲寒
傲寒 2021-02-05 15:15

Ok here is an overview of what\'s going on:

    M <-- Message with unique id of 1234
    |
    +-Start Queue
    |
    |
    | <-- Exchange
   /|\\
  / | \         


        
5条回答
  •  青春惊慌失措
    2021-02-05 15:33

    I can show you how you can do it with enqueue-bundle.

    So install it with composer and register as any other bundle. Then configure:

    // app/config/config.yml
    
    enqueue:
      transport:
        default: 'amnqp://'
      client: ~
    

    This approach is based on RPC. Here's how you do it:

    get('enqueue.client.producer');
    
    $promises = new SplObjectStorage();
    
    $promises->attach($producer->sendCommand('task1', 'the task data', true));
    $promises->attach($producer->sendCommand('task2', 'the task data', true));
    $promises->attach($producer->sendCommand('task3', 'the task data', true));
    
    while (count($promises)) {
        foreach ($promises as $promise) {
            if ($replyMessage = $promise->receiveNoWait()) {
                // you may want to check the response here
                $promises->detach($promise);
            }
        }
    }
    
    $producer->sendCommand('task4', 'the task data');
    

    The consumer processor looks like this:

    use Enqueue\Client\CommandSubscriberInterface;
    use Enqueue\Consumption\Result;
    use Enqueue\Psr\PsrContext;
    use Enqueue\Psr\PsrMessage;
    use Enqueue\Psr\PsrProcessor;
    
    class Task1Processor implements PsrProcessor, CommandSubscriberInterface
    {
        public function process(PsrMessage $message, PsrContext $context)
        {
            // do task job
    
            return Result::reply($context->createMessage('the reply data'));
        }
    
        public static function getSubscribedCommand()
        {
            // you can simply return 'task1'; if you do not need a custom queue, and you are fine to use what enqueue chooses. 
    
            return [
              'processorName' => 'task1',
              'queueName' => 'Q1',
              'queueNameHardcoded' => true,
              'exclusive' => true,
            ];
        }
    }
    

    Add it to your container as a service with a tag enqueue.client.processor and run command bin/console enqueue:consume --setup-broker -vvv

    Here's the plain PHP version.

提交回复
热议问题