Created
November 5, 2015 07:50
-
-
Save ehongyu/2938c107a35dc8a19d6f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require '../../vendor/autoload.php'; | |
define('AMQP_DEBUG', false); | |
$conn = new \PhpAmqpLib\Connection\AMQPStreamConnection( | |
'localhost', '5672', 'guest', 'guest', '/'); | |
var_dump(count($conn->channels)); | |
$ch1 = $conn->channel(1); | |
var_dump(count($conn->channels)); | |
$queue1 = 'q1'; | |
$ch1->queue_declare($queue1, false, true, false, false); | |
$ch1->queue_purge($queue1); | |
$queue2 = 'q2'; | |
$ch2 = $conn->channel(2); | |
$ch2->queue_declare($queue2, false, true, false, false); | |
$ch2->queue_purge($queue2); | |
$msg1 = new \PhpAmqpLib\Message\AMQPMessage( | |
'test1', | |
array('content_type' => 'text/plain', 'delivery_mode' => 2) | |
); | |
$msg2 = new \PhpAmqpLib\Message\AMQPMessage( | |
'test2', | |
array('content_type' => 'text/plain', 'delivery_mode' => 2) | |
); | |
echo "---- Publish message 1\n"; | |
$ch1->basic_publish($msg1, '', $queue1); | |
echo "---- Publish message 2\n"; | |
$ch2->basic_publish($msg2, '', $queue2); | |
$callback2 = function($msg) use ($ch2, $queue2) | |
{ | |
var_dump($msg->body); | |
assert($msg->body == 'test2'); | |
$msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']); | |
$ch2->basic_cancel($queue2); | |
}; | |
$ch2->basic_qos(null, 1, null); | |
$ch2->basic_consume($queue2, '', false, false, false, false, $callback2); | |
echo "---- Fetch message 2\n"; | |
if (count($ch2->callbacks)) { | |
$ch2->wait(); | |
} | |
echo "---- Publish message 1\n"; | |
$ch1->basic_publish($msg1, '', $queue1); | |
$ch1->basic_qos(null, 1, null); | |
$callback1 = function($msg) use ($ch1, $queue1) | |
{ | |
var_dump($msg->body); | |
assert($msg->body == 'test1'); | |
$msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']); | |
$ch1->basic_cancel($queue1); | |
}; | |
$ch1->basic_consume($queue1, '', false, false, false, false, $callback1); | |
echo "---- Fetch message 1\n"; | |
if (count($ch1->callbacks)) { | |
$ch1->wait(); | |
} | |
//$ch2->basic_consume($queue2, '', false, false, false, false, $callback2); | |
echo "---- Fetch message 2\n"; | |
if (count($ch2->callbacks)) { | |
$ch2->wait(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment