Set the number of messages to prefetch from the broker during a call to
AMQPQueue::consume() or AMQPQueue::get(). Any call to this method will
automatically set the prefetch window size to 0, meaning that the
prefetch window size setting will be ignored.
public setPrefetchCount ( integer $count ) : boolean | ||
$count | integer | The number of messages to prefetch. |
return | boolean | TRUE on success or FALSE on failure. |
/** * @param $count * * @return bool */ public function setPrefetchCount($count) { try { return $this->rawChannel->setPrefetchCount($count); } catch (\AMQPConnectionException $e) { ClientHelper::throwRightException($e); } }
/** * @return \AMQPChannel */ protected function getChannel() { if ($this->channel === null) { $this->channel = new \AMQPChannel($this->getConnection()); $this->channel->setPrefetchCount(1); } return $this->channel; }
/** * Initialize if not yet initialized and return channel to AMQP broker * * @return \AMQPChannel * * @throws ConnectionException */ private function getChannel() { if ($this->channel && $this->channel->isConnected()) { return $this->channel; } try { $this->channel = null; if ($this->connection->isConnected()) { $this->connection->disconnect(); } $this->connection->connect(); $channel = new \AMQPChannel($this->connection); $channel->setPrefetchCount(self::DEFAULT_PREFETCH_COUNT); } catch (\AMQPException $e) { throw new ConnectionException(sprintf('AMQP connection error (%s:%s): %s', $this->connection->getHost(), $this->connection->getPort(), $e->getMessage())); } return $this->channel = $channel; }
<?php /** * Created by PhpStorm. * User: seyfer * Date: 11/23/15 */ //Establish connection to AMQP $connection = new AMQPConnection(); $connection->setHost('127.0.0.1'); $connection->setLogin('guest'); $connection->setPassword('guest'); $connection->connect(); //Create and declare channel $channel = new AMQPChannel($connection); $channel->setPrefetchCount(1); $routing_key = 'task_queue'; try { $queue = new AMQPQueue($channel); $queue->setName($routing_key); $queue->setFlags(AMQP_DURABLE); $queue->declareQueue(); } catch (Exception $ex) { print_r($ex); } //Read from stdin $message = implode(' ', array_slice($argv, 1)); if (empty($message)) { $message = "Hello World!"; } $exchange = new AMQPExchange($channel);
/** * @inheritdoc */ public function setPrefetchCount(int $count) { $this->channel->setPrefetchCount($count); }
/** * Get connection * * @param string $name Connection name * * @return array Return connection and channel */ protected function getConnection($name) { if (isset($this->connections[$name])) { return $this->connections[$name]; } $config = $this->getConfig('connection', $name); if (null == $config) { throw new \InvalidArgumentException("Connection '{$name}' doesn't exists."); } $connection = new \AMQPConnection($config); $connection->connect(); $channel = new \AMQPChannel($connection); if (isset($config['prefetch_count'])) { $channel->setPrefetchCount($config['prefetch_count']); } return $this->connections[$name] = ['connection' => $connection, 'channel' => $channel]; }
$container->bind('parsley.brokers.rabbitmq.listener', 'AMQPy\\Listener', true); $container->bind('AMQPy\\Serializers\\SerializersPool', function ($container) { $serializers_pool = new \AMQPy\Serializers\SerializersPool(); $serializers_pool->register($container['config']->get('parsley.amqpy.serializers', [])); return $serializers_pool; }, true); $container->bind('AMQPConnection', function ($container) { $connection = new AMQPConnection($container['config']->get('parsley.amqpy.credentials', [])); $connection->connect(); return $connection; }, true); $container->bind('AMQPChannel', function ($container) { $ch = new AMQPChannel($container->make('AMQPConnection')); $qos = $container['config']->get('parsley.amqpy.qos'); if ($qos != null) { $ch->setPrefetchCount($qos); } return $ch; }, true); $container->bind('AMQPExchange', function ($container) { /** @var AMQPExchange $exchange */ $exchange = new AMQPExchange($container->make('AMQPChannel')); $exchange->setName($container['config']->get('parsley.amqpy.exchange', 'parsley')); $exchange->setType($container['config']->get('parsley.amqpy.exchange_type', AMQP_EX_TYPE_TOPIC)); $exchange->declareExchange(); return $exchange; }, true); $container->bind('AMQPQueue', function ($container) { /** @var AMQPQueue $queue */ $queue = new AMQPQueue($container->make('AMQPChannel')); /** @var \Illuminate\Config\Repository $config */
/** * Declare Channel */ protected function setChannel() { $this->channel = new AMQPChannel($this->connection); $this->channel->setPrefetchCount(1); }