本文整理汇总了PHP中AMQPExchange::publish方法的典型用法代码示例。如果您正苦于以下问题:PHP AMQPExchange::publish方法的具体用法?PHP AMQPExchange::publish怎么用?PHP AMQPExchange::publish使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类AMQPExchange
的用法示例。
在下文中一共展示了AMQPExchange::publish方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: call
public function call($value)
{
$this->response = NULL;
$this->corrId = uniqid();
try {
//Declare an nonymous channel
$this->queue = new AMQPQueue($this->channel);
$this->queue->setFlags(AMQP_EXCLUSIVE);
$this->queue->declareQueue();
$this->callbackQueueName = $this->queue->getName();
//Set Publish Attributes
$attributes = ['correlation_id' => $this->corrId, 'reply_to' => $this->callbackQueueName];
$this->exchange->publish($value, $this->rpcQueue, AMQP_NOPARAM, $attributes);
$callback = function (AMQPEnvelope $message, AMQPQueue $q) {
if ($message->getCorrelationId() == $this->corrId) {
//echo sprintf("CorrelationID: %s",$message->getCorrelationId()), PHP_EOL;
//echo sprintf("Response: %s",$message->getBody()), PHP_EOL;
$this->response = $message->getBody();
$q->nack($message->getDeliveryTag());
return false;
}
};
$this->queue->consume($callback);
//Return RPC Results
return $this->response;
} catch (AMQPQueueException $ex) {
print_r($ex);
} catch (Exception $ex) {
print_r($ex);
}
}
示例2: sendRequest
/**
* @param Request $rpcRequest
*
* @return Response|null
*/
protected function sendRequest(Request $rpcRequest)
{
$message = $this->serializer->serialize($rpcRequest, $this->protocol, ['encoding' => $this->encoding]);
$routingKey = $this->routingKey;
$flags = $this->flags;
$attributes = ['content_type' => 'application/' . $this->encoding, 'rpc_type' => $this->protocol];
$this->exchange->publish($message, $routingKey, $flags, $attributes);
}
示例3: 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);
}
示例4: publish
/**
* {@inheritdoc}
*/
public function publish($message, $routingKey = '', array $properties = array())
{
if (!isset($properties['timestamp'])) {
$properties['timestamp'] = microtime(true);
}
// make messages durable by default
if (!isset($properties['delivery_mode'])) {
$properties['delivery_mode'] = 2;
}
$response = $this->exchange->publish($message, $routingKey, AMQP_NOPARAM, $properties);
return $response;
}
示例5: enqueue
/**
* Enqueues given $message
*
* @throws FailedEnqueueException
*/
public function enqueue(Message $message, $timeout = 0, callable $onTimeout = null)
{
if ($message instanceof Delayable) {
if ($message->delayedUntil() < new \DateTime('now')) {
throw new \OutOfBoundsException('Cannot enqueue messages in the past');
// do something
}
}
$body = $message->getBody();
$route = $message->getRoutingKey();
$props = $message->getHeaders();
return $this->exchange->publish($body, $route, $flags, $props);
}
示例6: Run
public function Run()
{
// Declare a new exchange
$ex = new AMQPExchange($this->cnn);
$ex->declare('game', AMQP_EX_TYPE_HEADER);
// 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', 'broadcast=1,target=1,x-match=any');
$ex->bind('queue2', 'broadcast=1,target=2,x-match=any');
$ex->bind('queue3', 'broadcast=1,target=3,x-match=any');
$msgbody1 = 'hello';
// Publish a message to the exchange with a routing key
$result = $ex->publish($msgbody1, NULL, AMQP_IMMEDIATE, array('headers' => array('broadcast' => 1)));
$this->AssertEquals($result, TRUE, 'publish message failed');
$msgbody2 = 'world';
// Publish a message to the exchange with a routing key
$result = $ex->publish($msgbody2, NULL, AMQP_IMMEDIATE, array('headers' => array('broadcast' => 1)));
$this->AssertEquals($result, TRUE, 'publish message failed');
$msgbody3 = 'queue3';
// Publish a message to the exchange with a routing key
$result = $ex->publish($msgbody1, NULL, AMQP_IMMEDIATE, array('headers' => array('target' => 3)));
$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->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');
}
示例7: publish
/**
* Publish a message to an exchange.
*
* Publish a message to the exchange represented by the AMQPExchange object.
*
* @param string $message The message to publish.
* @param string $routingKey The optional routing key to which to publish to.
* @param integer $flags One or more of AMQP_MANDATORY and AMQP_IMMEDIATE.
* @param array $attributes One of content_type, content_encoding,
* message_id, user_id, app_id, delivery_mode,
* priority, timestamp, expiration, type
* or reply_to, headers.
*
* @throws \AMQPExchangeException On failure.
* @throws \AMQPChannelException If the channel is not open.
* @throws \AMQPConnectionException If the connection to the broker was lost.
*/
public function publish($message, $routingKey = null, $flags = AMQP_NOPARAM, array $attributes = [])
{
if (!$this->exchangeDeclared) {
$this->exchangeDeclare();
}
$this->exchange->publish($message, $routingKey, $flags, $attributes);
}
示例8: 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();
}
}
示例9: 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));
}
示例10: 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;
}
示例11: send
public function send($msg)
{
$ex = new \AMQPExchange($this->amqpChannel);
$ex->setName($this->name);
$ex->publish($msg);
return $this;
}
示例12: 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;
}
示例13: publish
/**
* @param mixed $message
* @param string $routingKey
* @param int $flags
* @param array $attributes
*
* @return bool
*/
public function publish($message, $routingKey = null, $flags = Client::NOPARAM, array $attributes = [])
{
try {
return $this->rawExchange->publish($this->encodeStrategy->encode($message), $routingKey, $flags, $attributes);
} catch (\Exception $e) {
ClientHelper::throwRightException($e);
}
}
示例14: doPush
/**
* @param string $data
* @param array $context
*/
protected function doPush($data, array $context)
{
$config = $this->getConfig();
if (false === $this->connected) {
if (!extension_loaded('amqp')) {
throw new \RuntimeException(sprintf('%s pusher require %s php extension', get_class($this), 'amqp'));
}
$this->connection = new \AMQPConnection($config);
$this->connection->connect();
list(, $this->exchange) = Utils::setupConnection($this->connection, $config);
$this->setConnected();
}
$resolver = new OptionsResolver();
$resolver->setDefaults(['routing_key' => null, 'publish_flags' => AMQP_NOPARAM, 'attributes' => array()]);
$context = $resolver->resolve($context);
$this->exchange->publish($data, $context['routing_key'], $context['publish_flags'], $context['attributes']);
}
示例15: 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'));
}