/** * Check if the message is requeued or not correctly. * * @dataProvider processMessageProvider */ public function testProcessMessage($processFlag, $expectedMethod, $expectedRequeue = null) { $amqpConnection = $this->getMockBuilder('\\PhpAmqpLib\\Connection\\AMQPConnection')->disableOriginalConstructor()->getMock(); $amqpChannel = $this->getMockBuilder('\\PhpAmqpLib\\Channel\\AMQPChannel')->disableOriginalConstructor()->getMock(); $consumer = new Consumer($amqpConnection, $amqpChannel); $callbackFunction = function () use($processFlag) { return $processFlag; }; // Create a callback function with a return value set by the data provider. $consumer->setCallback($callbackFunction); // Create a default message $amqpMessage = new AMQPMessage('foo body'); $amqpMessage->delivery_info['channel'] = $amqpChannel; $amqpMessage->delivery_info['delivery_tag'] = 0; $amqpChannel->expects($this->any())->method('basic_reject')->will($this->returnCallback(function ($delivery_tag, $requeue) use($expectedMethod, $expectedRequeue) { \PHPUnit_Framework_Assert::assertSame($expectedMethod, 'basic_reject'); // Check if this function should be called. \PHPUnit_Framework_Assert::assertSame($requeue, $expectedRequeue); // Check if the message should be requeued. })); $amqpChannel->expects($this->any())->method('basic_ack')->will($this->returnCallback(function ($delivery_tag) use($expectedMethod) { \PHPUnit_Framework_Assert::assertSame($expectedMethod, 'basic_ack'); // Check if this function should be called. })); $consumer->processMessage($amqpMessage); }
/** * Executes the current command. * * @param InputInterface $input An InputInterface instance * @param OutputInterface $output An OutputInterface instance * * @return integer 0 if everything went fine, or an error code * * @throws \InvalidArgumentException When the number of messages to consume is less than 0 * @throws \BadFunctionCallException When the pcntl is not installed and option -s is true */ protected function execute(InputInterface $input, OutputInterface $output) { if (defined('AMQP_DEBUG') === false) { define('AMQP_DEBUG', (bool) $input->getOption('debug')); } $this->amount = $input->getOption('messages'); if (0 > $this->amount) { throw new \InvalidArgumentException("The -m option should be null or greater than 0"); } $this->consumer = $this->getConsumerInstance($input); if (!is_null($input->getOption('memory-limit')) && ctype_digit((string) $input->getOption('memory-limit')) && $input->getOption('memory-limit') > 0) { $this->consumer->setMemoryLimit($input->getOption('memory-limit')); } $this->consumer->setRoutingKey($input->getOption('route')); $this->consumer->consume($this->amount); }
public function consume($msgAmount) { try { parent::consume($msgAmount); $this->consumeRetryCount = 0; } catch (\Exception $e) { $this->consumeRetryCount++; if ($this->consumeRetryCount < 10) { parent::consume($msgAmount); return; } throw $e; } }
private function loadConsumers($app) { $app['rabbit.consumer'] = $app->share(function ($app) { if (!isset($app['rabbit.consumers'])) { return; } $consumers = []; foreach ($app['rabbit.consumers'] as $name => $options) { $connection = $this->getConnection($app, $options, $app['rabbit.connections']); $consumer = new Consumer($connection); $consumer->setExchangeOptions($options['exchange_options']); $consumer->setQueueOptions($options['queue_options']); $consumer->setCallback(array($app[$options['callback']], 'execute')); if (array_key_exists('qos_options', $options)) { $consumer->setQosOptions($options['qos_options']['prefetch_size'], $options['qos_options']['prefetch_count'], $options['qos_options']['global']); } if (array_key_exists('qos_options', $options)) { $consumer->setIdleTimeout($options['idle_timeout']); } if (array_key_exists('auto_setup_fabric', $options) && !$options['auto_setup_fabric']) { $consumer->disableAutoSetupFabric(); } $consumers[$name] = $consumer; } return $consumers; }); }
/** * @expectedException \PhpAmqpLib\Exception\AMQPRuntimeException */ public function testLazyConnection() { $amqpLazyConnection = new AMQPLazyConnection('localhost', 123, 'lazy_user', 'lazy_password'); $consumer = new Consumer($amqpLazyConnection, null); $consumer->getChannel(); }
public function __construct(AMQPConnection $conn) { parent::__construct($conn); $this->setQueueOptions(array('name' => '', 'passive' => false, 'durable' => false, 'exclusive' => true, 'auto_delete' => true, 'nowait' => false, 'arguments' => null, 'ticket' => null)); }
protected function setupConsumer() { $this->mergeQueueOptions(); parent::setupConsumer(); }
/** * Overridden to add support for timeout * @param int $msgAmount * @param int $timeout */ public function consume($msgAmount, $timeout = 0) { if ($timeout > 0) { // save initial time $loopBegin = time(); $remaining = $timeout; // reimplement parent::consume() to inject the timeout $this->target = $msgAmount; $this->setupConsumer(); while (count($this->getChannel()->callbacks)) { // avoid waiting more than timeout seconds for message reception $this->setIdleTimeout($remaining); $this->maybeStopConsumer(); try { $this->getChannel()->wait(null, true, $this->getIdleTimeout()); } catch (AMQPTimeoutException $e) { return; } $remaining = $loopBegin + $timeout - time(); if ($remaining <= 0) { $this->forceStopConsumer(); } } } else { $this->loopBegin = null; parent::consume($msgAmount); } }