1. Go to this page and download the library: Download lys/easy-swoole-rabbitmq library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
namespace EasySwoole\EasySwoole;
use EasySwoole\Component\Timer;
use EasySwoole\EasySwoole\Swoole\EventRegister;
use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\Http\Request;
use EasySwoole\Http\Response;
use EasySwoole\RabbitMq\MqQueue;
use EasySwoole\RabbitMq\MqJob;
use EasySwoole\RabbitMq\RabbitMqQueueDriver;
use App\Utility\MqQueueProcess;
class EasySwooleEvent implements Event
{
public static function initialize()
{
// TODO: Implement initialize() method.
date_default_timezone_set('Asia/Shanghai');
}
public static function mainServerCreate(EventRegister $register)
{
$driver = new RabbitMqQueueDriver('127.0.0.1', 5672, 'test', 'test',"/");
MqQueue::getInstance($driver);
$processConfig= new \EasySwoole\Component\Process\Config();
$processConfig->setProcessGroup('Test');//设置进程组
$processConfig->setArg(['a'=>123]);//传参
$processConfig->setRedirectStdinStdout(false);//是否重定向标准io
$processConfig->setPipeType($processConfig::PIPE_TYPE_SOCK_DGRAM);//设置管道类型
$processConfig->setEnableCoroutine(true);//是否自动开启协程
$processConfig->setMaxExitWaitTime(3);//最大退出等待时间
$processConfig->setProcessName('MqQueueProcessComposer');
\EasySwoole\EasySwoole\ServerManager::getInstance()->addProcess(new MqQueueProcess($processConfig));
}
public static function onRequest(Request $request, Response $response): bool
{
// TODO: Implement onRequest() method.
return true;
}
public static function afterRequest(Request $request, Response $response): void
{
// TODO: Implement afterAction() method.
}
}
namespace App\Utility;
use EasySwoole\RabbitMq\MqQueue;
use EasySwoole\RabbitMq\MqJob;
class MqComposer{
public static function push(){
$job = new MqJob();
$job->setJobData('composer hello word'.date('Y-m-d H:i:s', time()));
$res = MqQueue::getInstance()->producer()->setConfig($exchange = 'kd_sms_send_ex',$routingKey = 'hello',$mqType = 'direct', $queueName = 'hello')->push($job);
if($res){
var_dump('发布成功');
}else{
var_dump('发布失败');
}
//主动关闭链接
/* MqQueue::getInstance()->closeConnection(function (\Exception $e){
//这边是主动关闭异常处理
}); */
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.