PHP code example of wantp / reliable-queue
1. Go to this page and download the library: Download wantp/reliable-queue 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/ */
wantp / reliable-queue example snippets
bitMqConfig = [
'host' => '192.168.56.10',
'port' => 35672,
'username' => 'test',
'password' => 'test',
'vhost' => '/test'
];
$module = 'wantp-reliable';
$retryInterval = [10, 20, 60, 120];
$reliableQueue = new \Wantp\ReliableQueue\ReliableQueue($rabbitMqConfig, $module, $retryInterval);
for ($i = 0; $i < 10; $i++) {
$data = ['index' => $i];
$reliableQueue->publishNormalMsg($data);
}
bitMqConfig = [
'host' => '192.168.56.10',
'port' => 35672,
'username' => 'test',
'password' => 'test',
'vhost' => '/test'
];
$module = 'wantp-reliable';
$retryInterval = [10, 20, 60, 120];
$reliableQueue = new \Wantp\ReliableQueue\ReliableQueue($rabbitMqConfig, $module, $retryInterval);
$reliableQueue->consume('_handler','_failHandler');
function _handler(\PhpAmqpLib\Message\AMQPMessage $msg)
{
//消费队列时会调用此方法,处理成功必须返回true
$data = json_decode($msg->getBody(), true);
if (((int)$data['index']) % 2 == 0) {
return true;
}
return false;
}
function _failHandler(\PhpAmqpLib\Message\AMQPMessage $msg)
{
//进入失败队列后会调用此方法
logFail();
}