• php操作rabbitmq


    <?php
    
    header('Content-Type:text/html;charset=utf8;');
    $time = 10;
    
    $params = array(
        'exchangeName' => 'test_cache_exchange'."_".$time,
        'queueName' => 'test_cache_queue'."_".$time,
        'routeKey' => 'test_cache_route'."_".$time,
    );
    
    $connectConfig = array(
        'host' => '192.168.33.60',
        'port' => 5672,
        'login' => 'admin',
        'password' => 'admin',
        'vhost' => '/'
    );
    
    //var_dump(extension_loaded('amqp'));
    //
    //exit();
    try {
        $conn = new AMQPConnection($connectConfig);
        $conn->connect();
        if (!$conn->isConnected()) {
            //die('Conexiune esuata');
            //TODO 记录日志
            echo 'rabbit-mq 连接错误:', json_encode($connectConfig);
            exit();
        }
        $channel = new AMQPChannel($conn);
        if (!$channel->isConnected()) {
            // die('Connection through channel failed');
            //TODO 记录日志
            echo 'rabbit-mq Connection through channel failed:', json_encode($connectConfig);
            exit();
        }
        $exchange = new AMQPExchange($channel);
        $exchange->setFlags(AMQP_DURABLE);//持久化
        $exchange->setName($params['exchangeName']?:'');
        $exchange->setType(AMQP_EX_TYPE_DIRECT); //direct类型
        $exchange->declareExchange();
    
        //$channel->startTransaction();
    
        $queue = new AMQPQueue($channel);
        $queue->setName($params['queueName']?:'');
        $queue->setFlags(AMQP_DURABLE);
    
        // 和普通生产者区别 在这 下面是过期时间和转发到的路由
        $queue->setArguments(array(
            'x-dead-letter-exchange' => 'delay_exchange',
            'x-dead-letter-routing-key' => 'delay_route',
            'x-message-ttl' => 20000,
        ));
        $queue->declareQueue();
    
        //绑定
        $queue->bind($params['exchangeName'], $params['routeKey']);
    } catch(Exception $e) {
    
    }
    
    
    //$num = mt_rand(100, 500);
    $num = 1;
    
    //生成消息
    $exchange->publish(date("Y-m-d H:i:s"), $params['routeKey'], AMQP_MANDATORY, array('delivery_mode'=>2));
  • 相关阅读:
    洛谷P3275 [SCOI2011]糖果
    2018年12月30&31日
    洛谷P4114 Qtree1
    洛谷P4116 Qtree3
    洛谷P4315 月下“毛景树”
    洛谷P1505 [国家集训队]旅游
    洛谷P2253 好一个一中腰鼓!
    CF616D Longest k-Good Segment
    洛谷P3979 遥远的国度
    洛谷P2486 [SDOI2011]染色
  • 原文地址:https://www.cnblogs.com/brady-wang/p/13543033.html
Copyright © 2020-2023  润新知