初步MQ
This commit is contained in:
82
app/command/BirthdayGiftSms.php
Normal file
82
app/command/BirthdayGiftSms.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace app\command;
|
||||
|
||||
use app\common\model\MqLog;
|
||||
use app\common\model\OrderInfo;
|
||||
use app\common\model\RabbitMqConfig;
|
||||
use ba\Random;
|
||||
use PhpAmqpLib\Connection\AMQPStreamConnection;
|
||||
use support\think\Db;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use support\Log;
|
||||
use Webman\RedisQueue\Redis;
|
||||
|
||||
// 生日有礼
|
||||
class BirthdayGiftSms extends Command
|
||||
{
|
||||
protected static $defaultName = 'birthdaygiftsms';
|
||||
protected static $defaultDescription = 'birthdaygiftsms';
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
protected function configure()
|
||||
{
|
||||
$this->addArgument('name', InputArgument::OPTIONAL, 'Name description');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param InputInterface $input
|
||||
* @param OutputInterface $output
|
||||
* @return int
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
|
||||
$host = RabbitMqConfig::$host;
|
||||
$port = RabbitMqConfig::$port;
|
||||
$user = RabbitMqConfig::$user;
|
||||
$password = RabbitMqConfig::$password;
|
||||
$queue = RabbitMqConfig::$queue_t . '-birthday.gift.sms';
|
||||
|
||||
// 防止空闲时间断线必须设置心跳
|
||||
$connection = new AMQPStreamConnection($host, $port, $user, $password,
|
||||
'/',
|
||||
false,
|
||||
'AMQPLAIN',
|
||||
null,
|
||||
'en_US',
|
||||
60,
|
||||
60,
|
||||
null,
|
||||
false,
|
||||
30
|
||||
);
|
||||
$rabbit_channel = $connection->channel();
|
||||
$rabbit_channel->queue_declare($queue, false, true, false, false, false, [
|
||||
'x-message-ttl' => ['I', 180000]
|
||||
]);
|
||||
|
||||
$callback = function ($msg) use ($queue){
|
||||
$date_time = date('Y-m-d H:i:s');
|
||||
$data = $msg->body;
|
||||
Log::info('收到消息--->' . $data . '--->' . $date_time . "\n");
|
||||
// 发给队列
|
||||
Redis::send('birthday.gift.sms', $data);
|
||||
$msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
|
||||
};
|
||||
$rabbit_channel->basic_consume($queue, '', false, false, false, false, $callback);
|
||||
while ($rabbit_channel->is_consuming()) {
|
||||
$rabbit_channel->wait();
|
||||
}
|
||||
$rabbit_channel->close();
|
||||
$connection->close();
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user