标签归档:RabbitMQ

RabbitMQ 简单队列

我想要的是定时任务延时队列,似乎不满足需求,现在用redis做的队列又可以满足大部分需求,先放着吧

    # 推送到队列
    public function aa032() {
        $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
        $channel = $connection->channel();
        
        $channel->queue_declare('hello', false, false, false, false);
        
        $msg = new AMQPMessage('Hello World 233!');
        $channel->basic_publish($msg, '', 'hello');
        
        echo " [x] Sent 'Hello World 233!'\n";
        
        $channel->close();
        $connection->close();
    }

    # 守护处理队列
    public function aa033() {
        $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
        $channel = $connection->channel();
        
        $channel->queue_declare('hello', false, false, false, false);
        
        echo " [*] Waiting for messages. To exit press CTRL+C\n";
        
        $callback = function ($msg) {
            echo ' [x] Received ', $msg->body, "\n";
        };
        
        $channel->basic_consume('hello', '', false, true, false, false, $callback);
        
        while (count($channel->callbacks)) {
            $channel->wait();
        }
    }