当前位置:   article > 正文

rabbitmq 延迟队列 php

rabbitmq 延迟队列 php
<?php

require_once '../vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
use PhpAmqpLib\Wire\AMQPTable;
use PhpAmqpLib\Exchange\AMQPExchangeType;

$connection = new AMQPStreamConnection('139.159.252.21', 5672, 'mq', 'mq123', '/');
$channel = $connection->channel();
//给cache发送  使其过期然后定向到另一个
//声明两个队列
$channel->exchange_declare('delay_exchange', 'direct',false,false,false);
$channel->exchange_declare('cache_exchange', 'direct',false,false,false);
 
$tale = new AMQPTable();
$tale->set('x-dead-letter-exchange', 'delay_exchange');//****很关键  表示过期后由哪个exchange处理
$tale->set('x-dead-letter-routing-key','delay_exchange');//****很关键  表示过期后由哪个exchange处理
//$tale->set('x-message-ttl',15000);  //存活时长   下面的过期时间不能超过
 
$channel->queue_declare('cache_queue',false,true,false,false,false,$tale);
$channel->queue_bind('cache_queue', 'cache_exchange','cache_exchange');
 
$channel->queue_declare('delay_queue',false,true,false,false,false);
$channel->queue_bind('delay_queue', 'delay_exchange','delay_exchange');
 
 
$msg = new AMQPMessage($argv[1], array(
    'expiration' => intval($argv[2]),
    'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT
 
));
 
$channel->basic_publish($msg,'cache_exchange','cache_exchange');
echo date('Y-m-d H:i:s')." [x] Sent {$argv[1]} ".PHP_EOL;
 
$channel->close();
$connection->close();
  • 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
  • 37
  • 38
<?php
require_once '../vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;

$connection = new AMQPStreamConnection('139.159.252.21', 5672, 'mq', 'mq123', '/');
$channel = $connection->channel();
$channel->exchange_declare('delay_exchange', 'direct',false,false,false);
 
$channel->queue_declare('delay_queue',false,true,false,false,false);
$channel->queue_bind('delay_queue', 'delay_exchange','delay_exchange');
 
echo ' [*] Waiting for message. To exit press CTRL+C '.PHP_EOL;
 
$callback = function ($msg){
    echo date('Y-m-d H:i:s')." [x] Received",$msg->body,PHP_EOL;
 
    // $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
 
};
 
//只有consumer已经处理并确认了上一条message时queue才分派新的message给它
// $channel->basic_qos(null, 1, null);
$channel->basic_consume('delay_queue','',false,true,false,false,$callback);
 
 
while (count($channel->callbacks)) {
    $channel->wait();
}
$channel->close();
$connection->close();
  • 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
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/575026
推荐阅读
相关标签
  

闽ICP备14008679号