PHP code example of fangchaogang / phpmq
1. Go to this page and download the library: Download fangchaogang/phpmq 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/ */
fangchaogang / phpmq example snippets
use phpmq\drivers\amqp_interop\Queue;
$config = [
"host"=>"127.0.0.1",
"port"=>5672,
"user" => "root",
"password" => "root",
"vhost"=>"/"
];
$queue = new Queue($config);
//---发消息
$job = new \phpmq\tests\TestJob();
$job->data = ['delay' => '5',];
//直接发
$queue->push($job);
//延时发
$queue->delay(1)->push($job);
//遇到错误N秒重试发
$queue->ttr(10)->push($job);
//带routingKey发
$queue->setRoutingKey('modify')->push($job);
//其他参考源码
//---监听
//直接监听
$queue->listen();
//带routingKey监听
$queue->regRoutingKeyCallback('modify', function ($messageData) {
var_dump('this is modify routingKey', $messageData);
})->listen();
use phpmq\drivers\redis\Queue;
$config = [
'host' => '127.0.0.1',
'port' => 6379
];
$queue = new Queue($config);
//---发消息
$job = new \phpmq\tests\TestJob();
$job->data = ['delay' => '5',];
//直接发
$queue->push($job);
//延时发
$queue->delay(1)->push($job);
//遇到错误N秒重试发
$queue->ttr(10)->push($job);
//---监听
//直接监听
$queue->listen();
use phpmq\drivers\beanstalk\Queue;
$config = [
'host'=>'150.158.185.89',
];
$queue = new Queue($config);
//---发消息
$job = new \phpmq\tests\TestJob();
$job->data = ['delay' => '5',];
//直接发
$queue->push($job);
//延时发
$queue->delay(1)->push($job);
//遇到错误N秒重试发
$queue->ttr(10)->push($job);
//---监听
//直接监听
$queue->listen();