RabbitMQ在foreach循环中无法正常工作(仅适用于第一次迭代)

时间:2017-06-30 09:55:28

标签: php laravel foreach rabbitmq

以下是代码的快照:

在.. \ app \ commands {command_name}中删除代码:

$results = Auction::get();

foreach($results as $result) {
           $user = User::where('email','=',$person[0]['user_id'])->get();
           $workerSender = new Worker();                    
           $workerSender->execute($person[0]['user_id']);
           $workerReceiver = new WorkerReceiver();
           $workerReceiver->listen();
}

Sender.php中的代码片段

use PhpAmqpLib\Connection\AMQPConnection;
use PhpAmqpLib\Message\AMQPMessage;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

class Worker {


private $log;

public function __construct()
{
    $this->log = new Logger('workerSend');
    $this->log->pushHandler(new StreamHandler('logs/workerSend.log', Logger::INFO));
}


public function execute($invoiceNum)
{

    $this->log->addInfo('Received invoice for processing: ' . $invoiceNum);

    $connection = new AMQPConnection('localhost', 5672, 'guest', 'guest');

    $channel = $connection->channel();

    $channel->queue_declare(
        'invoice_queue', 
        false,              
        true,               
        false,              
        false            
        );

    $msg = new AMQPMessage(
        $invoiceNum,
        array('delivery_mode' => 2) 
        );

    $channel->basic_publish(
        $msg,                
        '',                 
        'invoice_queue'     
        );

    $this->log->addInfo('Published task to worker');

    $channel->close();
    $connection->close();
  }

 }

在Receiver.php中捕捉代码:

use PhpAmqpLib\Connection\AMQPConnection;
use PhpAmqpLib\Message\AMQPMessage;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

class WorkerReceiver
{
/**
 * @var Logger
 */
private $log;

public function __construct()
{
    $this->log = new Logger('workerReceive');
    $this->log->pushHandler(new StreamHandler('logs/workerReceive.log', Logger::INFO));
}


  public function listen()
  {
    $this->log->addInfo('Begin listen routine');

    $connection = new AMQPConnection('localhost', 5672, 'guest', 'guest');
    $channel = $connection->channel();

    $channel->queue_declare(
        'invoice_queue',   
        false,          
        true,           
        false,          
        false           
        );


    $channel->basic_qos(
        null,   
        1,      
        null    
        );

    $channel->basic_consume(
        'invoice_queue',               
        '',                         
        false,                      
        false,                      
        false,                      
        false,                      
        array($this, 'process') 
        );

    $this->log->addInfo('Consuming from queue');

    while(count($channel->callbacks)) {
        $this->log->addInfo('Waiting for incoming messages');
        echo "Waiting for incoming messages\n";
        $channel->wait();
    }

    $channel->close();
    $connection->close();
}

 public function process(AMQPMessage $msg)
 {
    $this->log->addInfo('Received message: ' . $msg->body);
    echo "Received message:" ,$msg->body, "\n";

    //$this->generatePdf()->sendEmail();
    $this->sendEmail($msg);

           $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
}



private function sendEmail(AMQPMessage $msg)
{
    $this->log->addInfo('Sending email...');
    echo "Sending Email:" ,$msg->body, "\n";


      Mail::send('message', array('name'=>$msg->body), function ($message) use($msg){

                $message->to($msg->body, $msg->body)->subject('Congratulations!');
            });

     //ssleep(mt_rand(1,3));

     $this->log->addInfo('Email sent');
     echo "Email sent:" ,$msg->body, "\n";
  }
 }

RabbitMQ在foreach循环中无法正常工作(仅适用于第一次迭代)

0 个答案:

没有答案