1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| require_once 'vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection; use PhpAmqpLib\Message\AMQPMessage; use PhpAmqpLib\Wire\AMQPTable;
$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest'); $channel = $connection->channel(); try { $routingKey = 'priorityRoute'; $exchange = 'priorityExchange'; $queue = 'priorityQueue'; $channel->exchange_declare($exchange, 'direct');
$args = new AMQPTable(); $args->set('x-max-priority', 100); $channel->queue_declare($queue, false, true, false, false, false, $args); $channel->queue_bind($queue, $exchange, $routingKey);
$message = new AMQPMessage('优先级1', ['delivery_mode'=>2, 'priority' => 1]); $channel->basic_publish($message, $exchange, $routingKey, true);
$message = new AMQPMessage('优先级100', ['delivery_mode'=>2, 'priority' => 100]); $channel->basic_publish($message, $exchange, $routingKey, true);
$message = new AMQPMessage('优先级20', ['delivery_mode'=>2, 'priority' => 20]); $channel->basic_publish($message, $exchange, $routingKey, true);
$channel->wait_for_pending_acks(2);
} catch (\Exception $e){ echo "exception: " . $e->getMessage(); } $channel->close(); $connection->close();
|