本文整理汇总了PHP中AMQPQueue类的典型用法代码示例。如果您正苦于以下问题:PHP AMQPQueue类的具体用法?PHP AMQPQueue怎么用?PHP AMQPQueue使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了AMQPQueue类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: _initPublisher
private function _initPublisher()
{
if (!$this->open()) {
return false;
}
if ($this->_exchange) {
return true;
}
$channel = new AMQPChannel($this->_conn);
//创建exchange
$this->_exchange = new AMQPExchange($channel);
$this->_exchange->setName($this->_connInfo['exchangeName']);
$this->_exchange->setType(AMQP_EX_TYPE_DIRECT);
$this->_exchange->setFlags(AMQP_DURABLE);
if (!$this->_exchange->declareExchange()) {
$this->close();
return false;
}
//创建队列,生产者和消费者都要创建队列
$queue = new AMQPQueue($channel);
$queue->setName($this->_connInfo['queueName']);
$queue->setFlags(AMQP_DURABLE);
$queue->declareQueue();
$queue->bind($this->_connInfo['exchangeName'], $this->_connInfo['routingKey']);
return true;
}
示例2: execute
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln(sprintf('Move messages from queue "%s" (vhost: "%s") to exchange "%s" with routingKey "%s" (vhost: "%s")', $input->getArgument('from_queue'), $input->getArgument('from_vhost'), $input->getArgument('to_exchange'), $input->getArgument('to_routing_key'), $input->getArgument('to_vhost')));
$fromChannel = $this->getChannel($input->getArgument('from_connection'), $input->getArgument('from_vhost'));
if (null === ($toConnectionName = $input->getOption('to_connection'))) {
$toChannel = $this->getChannel($input->getArgument('from_connection'), $input->getArgument('to_vhost'));
} else {
$toChannel = $this->getChannel($input->getOption('to_connection'), $input->getArgument('to_vhost'));
}
$queue = new \AMQPQueue($fromChannel);
$queue->setName($input->getArgument('from_queue'));
$exchange = new \AMQPExchange($toChannel);
$exchange->setName($input->getArgument('to_exchange'));
$messageProvider = new PeclPackageMessageProvider($queue);
$messagePublisher = new PeclPackageMessagePublisher($exchange);
$options = array();
$stack = new \Swarrot\Processor\Stack\Builder();
if (0 !== ($max = (int) $input->getOption('max-messages'))) {
$stack->push('Swarrot\\Processor\\MaxMessages\\MaxMessagesProcessor');
$options['max_messages'] = $max;
}
$stack->push('Swarrot\\Processor\\Insomniac\\InsomniacProcessor');
$stack->push('Swarrot\\Processor\\Ack\\AckProcessor', $messageProvider);
$processor = $stack->resolve(new MoveProcessor($messagePublisher, $input->getArgument('to_routing_key')));
$consumer = new Consumer($messageProvider, $processor);
$consumer->consume($options);
}
示例3: process
/**
* @param \AMQPEnvelope $envelope
* @param \AMQPQueue $queue
* @param PushPipe $pushPipe
* @param string $errorQueue
* @param ReceiveCancellationToken $cancellationToken
* @param EndpointControlToken $endpointControlToken
*
* @return bool
*/
public function process(\AMQPEnvelope $envelope, \AMQPQueue $queue, PushPipe $pushPipe, $errorQueue, ReceiveCancellationToken $cancellationToken, EndpointControlToken $endpointControlToken)
{
try {
$messageId = '';
$headers = [];
$pushMessage = false;
try {
$messageId = $this->messageConverter->retrieveMessageId($envelope);
$headers = $this->messageConverter->retrieveHeaders($envelope);
$pushMessage = true;
} catch (\Exception $e) {
$this->routingTopology->sendToQueue($this->brokerModel, $errorQueue, $envelope->getBody(), ['headers' => $envelope->getHeaders()]);
}
if ($pushMessage) {
$pushPipe->push(new PushContext($messageId, $headers, $envelope->getBody() ?: '', $cancellationToken, $endpointControlToken));
}
if ($cancellationToken->isCancellationRequested()) {
$queue->reject($envelope->getDeliveryTag(), AMQP_REQUEUE);
} else {
$queue->ack($envelope->getDeliveryTag());
}
} catch (CriticalErrorException $e) {
// just ... die
throw $e;
} catch (\Exception $e) {
$queue->reject($envelope->getDeliveryTag(), AMQP_REQUEUE);
}
if ($endpointControlToken->isShutdownRequested()) {
return false;
}
return true;
}
示例4: Run
public function Run()
{
// Declare a new exchange
$ex = new AMQPExchange($this->cnn);
$ex->declare('game', AMQP_EX_TYPE_FANOUT);
// Create a new queue
$q1 = new AMQPQueue($this->cnn);
$q1->declare('queue1');
$q2 = new AMQPQueue($this->cnn);
$q2->declare('queue2');
// Bind it on the exchange to routing.key
//$ex->bind('queue1', 'broadcast=true,target=queue1,x-match=any');
$ex->bind('queue1', '');
$ex->bind('queue2', '');
$msgBody = 'hello';
// Publish a message to the exchange with a routing key
$ex->publish($msgBody, 'foo');
// Read from the queue
$msg = $q1->consume();
$this->AssertEquals(count($msg), 1);
$this->AssertEquals($msg[0]['message_body'], $msgBody, 'message not equal');
// Read from the queue
$msg = $q2->consume();
$this->AssertEquals(count($msg), 1);
$this->AssertEquals($msg[0]['message_body'], $msgBody, 'message not equal');
$this->AddMessage(var_export($msg[0], true));
}
示例5: bind
/**
* Bind queue to exchange using dispatcher event names as routing keys
*
* @return void
* @throws AMQPExchangeException
*/
public function bind(\AMQPQueue $queue, \AMQPExchange $exchange)
{
$events = preg_grep($this->pattern, array_keys($this->getDispatcher()->getListeners()));
foreach ($events as $eventName) {
$queue->bind($exchange->getName(), $eventName);
}
$this->dispatcher->dispatch(static::BIND_EVENT, new SymfonyEvent($events, ["pattern" => $this->pattern, "exchange" => $exchange->getName(), "queue" => $queue->getName()]));
}
示例6: declareQueue
public function declareQueue($name, $flags = AMQP_DURABLE)
{
$queue = new \AMQPQueue($this->channel);
$queue->setName($name);
$queue->setFlags($flags);
$queue->declareQueue();
return $queue;
}
示例7: getAMQPQueue
protected function getAMQPQueue($name)
{
$connection = new \AMQPConnection(array('vhost' => 'swarrot'));
$connection->connect();
$channel = new \AMQPChannel($connection);
$queue = new \AMQPQueue($channel);
$queue->setName($name);
return $queue;
}
示例8: getQueue
/**
* @param string $name
*
* @return \AMQPQueue
*/
protected function getQueue($name)
{
if (!isset($this->queues[$name])) {
$queue = new \AMQPQueue($this->getChannel());
$queue->setName($name);
$this->queues[$name] = $queue;
}
return $this->queues[$name];
}
示例9: register
/**
* {@inheritDoc}
*/
public function register(Container $c)
{
$c['amqp.connections.initializer'] = function ($c) {
$config = $c['amqp.options'];
$connections = array();
if (isset($config['connections'])) {
foreach ($config['connections'] as $name => $options) {
$connections[$name] = new \AMQPConnection($options);
}
return $connections;
}
if (isset($config['connection'])) {
return array('default' => new \AMQPConnection($config['connection']));
}
throw new \LogicException('No connection defined');
};
$c['queue.factory'] = function ($c) {
$connections = $c['amqp.connections.initializer'];
return function ($queueName, $connectionName = null) use($connections) {
$names = array_keys($connections);
if (null === $connectionName) {
$connectionName = reset($names);
}
if (!array_key_exists($connectionName, $connections)) {
throw new \InvalidArgumentException(sprintf('Unknown connection "%s". Available: [%s]', $connectionName, implode(', ', $names)));
}
$connection = $connections[$connectionName];
if (!$connection->isConnected()) {
$connection->connect();
}
$channel = new \AMQPChannel($connection);
$queue = new \AMQPQueue($channel);
$queue->setName($queueName);
return $queue;
};
};
$c['exchange.factory'] = function ($c) {
$connections = $c['amqp.connections.initializer'];
return function ($exchangeName, $connectionName = null) use($connections) {
$names = array_keys($connections);
if (null === $connectionName) {
$connectionName = reset($names);
}
if (!array_key_exists($connectionName, $connections)) {
throw new \InvalidArgumentException(sprintf('Unknown connection "%s". Available: [%s]', $connectionName, implode(', ', $names)));
}
$connection = $connections[$connectionName];
if (!$connection->isConnected()) {
$connection->connect();
}
$channel = new \AMQPChannel($connection);
$exchange = new \AMQPExchange($channel);
$exchange->setName($exchangeName);
return $exchange;
};
};
}
示例10: get
public function get($queueName, $timeout = null)
{
if (null === $this->queue) {
$queue = new \AMQPQueue($this->channel);
$queue->setName($queueName);
$queue->declare();
}
return unserialize($queue->consume());
}
示例11: createQueue
/**
* @inheritdoc
*/
public function createQueue(ChannelInterface $channel, $name = null, $flags = null, array $args = [])
{
$delegate = new \AMQPQueue($channel->getDelegate());
$delegate->setFlags(Queue::convertToDelegateFlags($flags));
$delegate->setArguments($args);
if (null !== $name) {
$delegate->setName($name);
}
return new Queue($delegate, $channel);
}
示例12: read
/**
* @param \AMQPQueue $queue
* @return bool|string
*/
public static function read(\AMQPQueue $queue)
{
usleep(10000);
$envelope = $queue->get(\AMQP_NOPARAM);
if ($envelope) {
$message = $envelope->getBody();
$queue->ack($envelope->getDeliveryTag());
return $message;
}
return false;
}
示例13: Run
public function Run()
{
// Declare a new exchange
$ex = new AMQPExchange($this->cnn);
$ex->declare('game', AMQP_EX_TYPE_TOPIC);
// Create a new queue
$q1 = new AMQPQueue($this->cnn);
$q1->declare('queue1');
$q1->purge('queue1');
$q2 = new AMQPQueue($this->cnn);
$q2->declare('queue2');
$q1->purge('queue2');
$q3 = new AMQPQueue($this->cnn);
$q3->declare('queue3');
$q3->purge('queue3');
$options = array('min' => 0, 'max' => 10, 'ack' => true);
// Bind it on the exchange to routing.key
$ex->bind('queue1', 'game1.#');
$ex->bind('queue2', 'game1.#');
$ex->bind('queue3', 'game1.#');
$ex->bind('queue3', 'queue3.#');
$msgbody1 = 'hello';
// Publish a message to the exchange with a routing key
$result = $ex->publish($msgbody1, 'game1.msg');
$this->AssertEquals($result, TRUE, 'publish message failed');
$msgbody2 = 'world';
// Publish a message to the exchange with a routing key
$result = $ex->publish($msgbody2, 'game1.msg');
$this->AssertEquals($result, TRUE, 'publish message failed');
$msgbody3 = 'hello player3';
// Publish a message to the exchange with a routing key
$result = $ex->publish($msgbody3, 'queue3.command');
$this->AssertEquals($result, TRUE, 'publish message failed');
// Read from the queue
$msg = $q1->consume($options);
$this->AddMessage(var_export($msg, true));
$this->AssertEquals(count($msg), 2);
$this->AssertEquals($msg[0]['message_body'], $msgbody1, 'message not equal');
$this->AssertEquals($msg[1]['message_body'], $msgbody2, 'message not equal');
// Read from the queue
$msg = $q2->consume($options);
$this->AssertEquals(count($msg), 2);
$this->AssertEquals($msg[0]['message_body'], $msgbody1, 'message not equal');
$this->AssertEquals($msg[1]['message_body'], $msgbody2, 'message not equal');
// Read from the queue
$msg = $q3->consume($options);
$this->AddMessage(var_export($msg, true));
$this->AssertEquals(count($msg), 3);
$this->AssertEquals($msg[0]['message_body'], $msgbody1, 'message not equal');
$this->AssertEquals($msg[1]['message_body'], $msgbody2, 'message not equal');
$this->AssertEquals($msg[2]['message_body'], $msgbody3, 'message not equal');
$msg = $q3->consume($options);
$this->AssertEquals(count($msg), 0);
}
示例14: take
public function take()
{
$q = new \AMQPQueue($this->amqpChannel);
$q->setName($this->name);
$msg = $q->get();
if (empty($msg)) {
return false;
} else {
return new MessageTakeControl($q, $msg);
}
}
示例15: getQueue
/**
* getQueue.
*
* @param string $name
* @param string $connection
*
* @return \AMQPQueue
*/
public function getQueue($name, $connection)
{
if (!isset($this->queues[$connection][$name])) {
if (!isset($this->queues[$connection])) {
$this->queues[$connection] = array();
}
$queue = new \AMQPQueue($this->getChannel($connection));
$queue->setName($name);
$this->queues[$connection][$name] = $queue;
}
return $this->queues[$connection][$name];
}