zoukankan      html  css  js  c++  java
  • rabbitmq新版本使用

    生产者

    <?php
    $exchangeName = "sendMessage";
    $queueName = "sendMessageQueue";
    $routeKey = "sendMessageKey";
    
    $message = "hello rabbitmq message!";
    
    $connection  = new AMQPConnection(['host'=>'127.0.0.1','port'=>'5672','vhost'=>'/','login'=>'guest','password'=>'guest']);
    $connection->connect() or die('can not connect broker server !');
    
    $channel = new AMQPChannel($connection);
    //$channel->setPerfetchCount(1);
    
    $exchange = new AMQPExchange($channel);
    $exchange->setName($exchangeName);
    $exchange->setType(AMQP_EX_TYPE_DIRECT);
    $exchange->setFlags(AMQP_DURABLE);
    $exchange->declareExchange();
    
    $queue = new AMQPQueue($channel);
    $queue->setName($queueName);
    $queue->setFlags(AMQP_DURABLE);
    $queue->declareQueue();
    
    $queue->bind($exchangeName, $routeKey);
    
    for($i=0;$i<100;$i++) {
        $exchange->publish($message, $routeKey);
    }
    $connection->disconnect();

    消费者

    <?php
    $exchangeName = 'sendMessage';
    $queueName="sendMessageQueue";
    $routeKey = 'sendMessageKey';
    
    $connection = new AMQPConnection(['host'=>'127.0.0.1','port'=>'5672','vhost'=>'/','login'=>'guest','password'=>'guest']);
    $connection->connect() or die('can not connect broker server !');
    
    $channel = new AMQPChannel($connection);
    //$channel->setPerfetchCount(1);
    
    $exchange = new AMQPExchange($channel);
    $exchange->setName($exchangeName);
    $exchange->setType(AMQP_EX_TYPE_DIRECT);
    $exchange->setFlags(AMQP_DURABLE);
    $exchange->declareExchange();
    
    $queue = new AMQPQueue($channel);
    $queue->setName($queueName);
    $queue->setFlags(AMQP_DURABLE);
    $queue->declareQueue();
    
    $queue->bind($exchangeName, $routeKey);
    
    echo "message ........";
    while(true){
        $queue->consume('dealMsg');
    }
    
    $connection->disconnect();
    
    function dealMsg($event, $q){
        $msg = $event->getBody();
        echo $msg."
    ";
        $q->ack($event->getDeliveryTag());
    }
  • 相关阅读:
    CSS样式
    hdu 6038 Function
    hdu 6034 Balala Power!
    错排公式 (递推)
    快速求排列组合 lucas定理
    fzu Problem 2275 Game(kmp)
    HDU 3635 Dragon Balls(并查集)
    HDU 3172 Virtual Friends(map+并查集)
    hdu 2818 Building Block(并查集,有点点复杂)
    hdu 1272 小希的迷宫(并查集)
  • 原文地址:https://www.cnblogs.com/dawuge/p/13612685.html
Copyright © 2011-2022 走看看