本文整理汇总了PHP中AMQPExchange类的典型用法代码示例。如果您正苦于以下问题:PHP AMQPExchange类的具体用法?PHP AMQPExchange怎么用?PHP AMQPExchange使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了AMQPExchange类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: sendActivityNotice
/**
* Send an activity notice using AMQP
* @param ActivityNotice $notice
* @return bool
*/
public function sendActivityNotice($notice)
{
if (!isset($notice)) {
return false;
}
/** @var array $setting */
$setting = $this->params['amqpSetting'];
try {
if ($this->amqpClientLibrary == "PhpAmqpLib") {
$connection = new AMQPStreamConnection($setting['host'], $setting['port'], $setting['user'], $setting['password']);
$channel = $connection->channel();
$msg = new AMQPMessage(JsonHelper::encode($notice));
$channel->basic_publish($msg, $setting['exchangeName'], $setting['routingKey']);
$channel->close();
$connection->close();
} elseif ($this->amqpClientLibrary == "PECL") {
$connection = new \AMQPConnection(['host' => $setting['host'], 'port' => $setting['port'], 'login' => $setting['user'], 'password' => $setting['password']]);
$connection->connect();
if ($connection->isConnected()) {
$channel = new \AMQPChannel($connection);
$exchange = new \AMQPExchange($channel);
$exchange->setName($setting['exchangeName']);
$exchange->publish(JsonHelper::encode($notice), $setting['routingKey']);
$connection->disconnect();
}
} else {
return false;
}
} catch (\Exception $e) {
return false;
}
return true;
}
示例2: 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));
}
示例3: publish
/**
* On dispatch event listener - called on any event
* AMQP_MANDATORY: When publishing a message, the message must be routed to a valid queue. If it is not, an error will be returned.
* if the client publishes a message with the "mandatory" flag set to an exchange of "direct" type which is not bound to a queue.
* AMQP_IMMEDIATE: When publishing a message, mark this message for immediate processing by the broker.
* REMOVED from rabbitmq > 3-0 http://www.rabbitmq.com/blog/2012/11/19/breaking-things-with-rabbitmq-3-0
* @param Event $event
* @param string $eventName
* @return void
*/
public function publish(Amqp\PublishEvent $event, $eventName, \AMQPExchange $exchange)
{
$success = $exchange->publish($event->getBody(), $eventName, $event->getFlags(), $event->jsonSerialize());
if ($this->stopPropagation) {
$event->stopPropagation();
}
}
示例4: 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);
}
示例5: send
public function send($msg)
{
$ex = new \AMQPExchange($this->amqpChannel);
$ex->setName($this->name);
$ex->publish($msg);
return $this;
}
示例6: 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()]));
}
示例7: publishToExchange
/**
* Publish the message to AMQP exchange
*
* @param Message $message
* @param \AMQPExchange $exchange
* @return bool
* @throws \AMQPException
*/
protected function publishToExchange(Message $message, \AMQPExchange $exchange)
{
$isPublished = $exchange->publish($message->getMessage(), $message->getRoutingKey(), $message->getFlags(), $message->getAttributes());
if (!$isPublished) {
throw FailedToPublishException::fromMessage($message);
}
return $isPublished;
}
示例8: setBroadcast
/**
* 广播形式写入队列
*
* @Author tianyunzi
* @DateTime 2015-08-19T18:44:42+0800
*/
public function setBroadcast($exName, $routingKey, $value)
{
//创建交换机
$ex = new \AMQPExchange($this->channel);
$ex->setName($exName);
//入队列
$ex->publish($value, $routingKey, AMQP_NOPARAM, array('delivery_mode' => '2'));
}
示例9: PostToExchange
/**
* Post a task to exchange specified in $details
* @param AMQPConnection $connection Connection object
* @param array $details Array of connection details
* @param string $task JSON-encoded task
* @param array $params AMQP message parameters
*/
function PostToExchange($connection, $details, $task, $params)
{
$ch = $connection->channel;
$xchg = new AMQPExchange($ch);
$xchg->setName($details['exchange']);
$success = $xchg->publish($task, $details['binding'], 0, $params);
return $success;
}
示例10: 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;
};
};
}
示例11: basic_publish
/**
* Publishes a message
*
* @param AMQPMessage $msg
* @param string $exchange
* @param string $routing_key
* @param bool $mandatory
* @param bool $immediate
* @param null $ticket
*/
public function basic_publish($msg, $exchange = '', $routing_key = '', $mandatory = false, $immediate = false, $ticket = null)
{
$flags = AMQP_NOPARAM;
$flags += $mandatory ? AMQP_MANDATORY : 0;
$flags += $immediate ? AMQP_IMMEDIATE : 0;
$xchange = new \AMQPExchange($this->channel);
$xchange->setName($exchange);
$xchange->publish($msg->body, $routing_key, $flags, $msg->get_properties());
}
示例12: getExchange
/**
* @param string $name
*
* @return \AMQPExchange
*/
protected function getExchange($name)
{
if (!isset($this->exchanges[$name])) {
$exchange = new \AMQPExchange($this->getChannel());
$exchange->setName($name);
$this->exchanges[$name] = $exchange;
}
return $this->exchanges[$name];
}
示例13: getAMQPExchange
protected function getAMQPExchange()
{
$connection = new \AMQPConnection(array('vhost' => 'swarrot'));
$connection->connect();
$channel = new \AMQPChannel($connection);
$exchange = new \AMQPExchange($channel);
$exchange->setName('exchange');
return $exchange;
}
示例14: createExchange
/**
* @inheritdoc
*/
public function createExchange(ChannelInterface $channel, $name, $type = ExchangeInterface::TYPE_DIRECT, $flags = null, array $args = [])
{
$delegate = new \AMQPExchange($channel->getDelegate());
$delegate->setName($name);
$delegate->setType($type);
$delegate->setFlags(Exchange::convertToDelegateFlags($flags));
$delegate->setArguments($args);
return new Exchange($delegate, $channel);
}
示例15: resend
public static function resend($message, $routekey, $exchange = 'momo_nd', $appid = 0, $uid = 0)
{
try {
$obj_conn = new AMQPConnect(Kohana::config('uap.rabbitmq'));
$obj_exchange = new AMQPExchange($obj_conn, $exchange);
$obj_exchange->publish($message, $routekey, AMQP_MANDATORY, array('app_id' => "{$appid}", 'user_id' => "{$uid}"));
} catch (Exception $e) {
Kohana::log('error', "再次发送消息失败 message = " . $message . "\n" . $e->getTraceAsString());
}
}