1. <tfoot id='ebT44'></tfoot>
    <i id='ebT44'><tr id='ebT44'><dt id='ebT44'><q id='ebT44'><span id='ebT44'><b id='ebT44'><form id='ebT44'><ins id='ebT44'></ins><ul id='ebT44'></ul><sub id='ebT44'></sub></form><legend id='ebT44'></legend><bdo id='ebT44'><pre id='ebT44'><center id='ebT44'></center></pre></bdo></b><th id='ebT44'></th></span></q></dt></tr></i><div id='ebT44'><tfoot id='ebT44'></tfoot><dl id='ebT44'><fieldset id='ebT44'></fieldset></dl></div>

    <small id='ebT44'></small><noframes id='ebT44'>

        <bdo id='ebT44'></bdo><ul id='ebT44'></ul>
      <legend id='ebT44'><style id='ebT44'><dir id='ebT44'><q id='ebT44'></q></dir></style></legend>

      怎么拖延?- php-amqplib

      How to delay? - php-amqplib(怎么拖延?- php-amqplib)
      <legend id='KXMdl'><style id='KXMdl'><dir id='KXMdl'><q id='KXMdl'></q></dir></style></legend>

      <small id='KXMdl'></small><noframes id='KXMdl'>

        <tfoot id='KXMdl'></tfoot>
      • <i id='KXMdl'><tr id='KXMdl'><dt id='KXMdl'><q id='KXMdl'><span id='KXMdl'><b id='KXMdl'><form id='KXMdl'><ins id='KXMdl'></ins><ul id='KXMdl'></ul><sub id='KXMdl'></sub></form><legend id='KXMdl'></legend><bdo id='KXMdl'><pre id='KXMdl'><center id='KXMdl'></center></pre></bdo></b><th id='KXMdl'></th></span></q></dt></tr></i><div id='KXMdl'><tfoot id='KXMdl'></tfoot><dl id='KXMdl'><fieldset id='KXMdl'></fieldset></dl></div>
          <bdo id='KXMdl'></bdo><ul id='KXMdl'></ul>
              <tbody id='KXMdl'></tbody>

                本文介绍了怎么拖延?- php-amqplib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我想知道如何使用 Amqpphplib 进行延迟.

                I would like to know how to delay with Amqpphplib.

                我使用了这个很棒的咖啡脚本教程:

                I used this great coffee script tutorial :

                https://github.com/jamescarr/rabbitmq-scheduled-delivery

                但它似乎不适用于 PHP-amqplib.

                but it doesn't seems to work with PHP-amqplib.

                消息如我所愿过期,但似乎x-dead-letter-exchange"不起作用.我使用了 RabbitMQ 管理控制台,我可以实时看到所有队列的创建和删除.但是我的消息在过期后确实会进入即时队列.我用的是 RabbitMQ 3.2.3 版本,PHP-amqplib 2.2.* 版本.

                The message expires as I want, but it seems that "x-dead-letter-exchange" don't do the work. I used RabbitMQ management console and I see all queue creation and deletion in live. But my message do go to the immediate queue after expiring. I use RabbitMQ 3.2.3 version, PHP-amqplib 2.2.* version.

                这是我的代码:

                连接类:

                class Connection
                {
                /**
                 * @var $ch
                 */
                public $ch;
                
                /**
                 * @var $consumer_tag
                 */
                public $consumer_tag;
                
                /**
                 * @var $exchange
                 */
                public $exchange;
                
                /**
                 * @var $conn
                 */
                public $conn;
                
                public function __construct($host, $port, $user, $password, $vhost)
                {
                
                    $this->exchange = 'immediate';
                    $this->queue = 'right.now.queue';
                    $this->consumer_tag = 'consumer';
                
                
                    $this->conn = new AMQPConnection($host, $port, $user, $password, $vhost);
                    $this->ch = $this->conn->channel();
                
                    $this->ch->exchange_declare($this->exchange, 'direct', false, true, false);
                
                    $this->ch->queue_declare($this->queue, false, true, false, false, false);
                
                    $this->ch->queue_bind($this->queue, $this->exchange);
                
                
                }
                
                public function createDelayedQueue ($name, $delay_seconds) {
                    $this->ch->queue_declare($name, false, false, false, true, true, array(
                        "x-dead-letter-exchange" => array("S", $this->exchange),
                        "x-message-ttl" => array("I", $delay_seconds*1000),
                        "x-expires" => array("I", $delay_seconds*1000+1000)
                    ));
                }
                }
                

                发布代码

                $name = 'send.later.'.$ts;
                $amqp->createDelayedQueue($name, 2);
                $msg = new AMQPMessage($msg_body, array('content_type' => 'text/plain', 'delivery_mode' => 2));
                $amqp->ch->basic_publish($msg);
                

                消费者代码

                $amqp = $this->getContainer()->get('amqp_connexion');
                
                    $amqp->ch->basic_consume($amqp->queue, $amqp->consumer_tag, false, false, false, false, function ($msg) {
                
                        echo $msg->body;
                        echo "
                --------
                ";
                    });
                
                    $output->writeln('Listening '.$amqp->queue.'...');
                
                    // Loop as long as the channel has callbacks registered
                    while (count($amqp->ch->callbacks)) {
                        $amqp->ch->wait();
                    }
                

                推荐答案

                我刚刚为php写了一个简化的工作版本:

                I just wrote a simplified working version for php:

                /////// simplified ///////
                
                // include the AMQPlib Classes || use an autoloader
                
                // queue/exchange names
                $queueRightNow = 'right.now.queue';
                $exchangeRightNow = 'right.now.exchange';
                $queueDelayed5sec = 'delayed.five.seconds.queue';
                $exchangeDelayed5sec = 'delayed.five.seconds.exchange';
                
                $delay = 5; // delay in seconds
                
                // create connection
                $AMQPConnection = new PhpAmqpLibConnectionAMQPConnection('localhost',5672,'guest','guest');
                
                // create a channel
                $channel = $AMQPConnection->channel();
                
                // create the right.now.queue, the exchange for that queue and bind them together
                $channel->queue_declare($queueRightNow);
                $channel->exchange_declare($exchangeRightNow, 'direct');
                $channel->queue_bind($queueRightNow, $exchangeRightNow);
                
                // now create the delayed queue and the exchange
                $channel->queue_declare(
                        $queueDelayed5sec,
                        false,
                        false,
                        false,
                        true,
                        true,
                        array(
                            'x-message-ttl' => array('I', $delay*1000),   // delay in seconds to milliseconds
                            "x-expires" => array("I", $delay*1000+1000),
                            'x-dead-letter-exchange' => array('S', $exchangeRightNow) // after message expiration in delay queue, move message to the right.now.queue
                        )
                );
                $channel->exchange_declare($exchangeDelayed5sec, 'direct');
                $channel->queue_bind($queueDelayed5sec, $exchangeDelayed5sec);
                
                // now create a message und publish it to the delayed exchange
                $msg = new PhpAmqpLibMessageAMQPMessage(
                    time(),
                    array(
                        'delivery_mode' => 2
                    )
                );
                $channel->basic_publish($msg,$exchangeDelayed5sec);
                
                
                // consume the delayed message
                $consumeCallback = function(PhpAmqpLibMessageAMQPMessage $msg) {
                    $messagePublishedAt = $msg->body;
                    echo 'seconds between publishing and consuming: '
                        . (time()-$messagePublishedAt) . PHP_EOL;
                };
                $channel->basic_consume($queueRightNow, '', false, true, false, false, $consumeCallback);
                
                // start consuming
                while (count($channel->callbacks) > 0) {
                    $channel->wait();
                }
                

                这篇关于怎么拖延?- php-amqplib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                  1. <tfoot id='JK9mQ'></tfoot><legend id='JK9mQ'><style id='JK9mQ'><dir id='JK9mQ'><q id='JK9mQ'></q></dir></style></legend>
                      <bdo id='JK9mQ'></bdo><ul id='JK9mQ'></ul>

                      1. <small id='JK9mQ'></small><noframes id='JK9mQ'>

                          <tbody id='JK9mQ'></tbody>
                        <i id='JK9mQ'><tr id='JK9mQ'><dt id='JK9mQ'><q id='JK9mQ'><span id='JK9mQ'><b id='JK9mQ'><form id='JK9mQ'><ins id='JK9mQ'></ins><ul id='JK9mQ'></ul><sub id='JK9mQ'></sub></form><legend id='JK9mQ'></legend><bdo id='JK9mQ'><pre id='JK9mQ'><center id='JK9mQ'></center></pre></bdo></b><th id='JK9mQ'></th></span></q></dt></tr></i><div id='JK9mQ'><tfoot id='JK9mQ'></tfoot><dl id='JK9mQ'><fieldset id='JK9mQ'></fieldset></dl></div>