PHP使用enqueue/amqp-lib實(shí)現(xiàn)rabbitmq任務(wù)處理
一:拓展安裝
composer require enqueue/amqp-lib
文檔地址:https://github.com/php-enqueue/enqueue-dev/blob/master/docs/transport/amqp_lib.md
二:方法介紹
1:連接rabbitmq
$factory = new AmqpConnectionFactory([
'host' => '192.168.6.88',//host
'port' => '5672',//端口
'vhost' => '/',//虛擬主機(jī)
'user' => 'admin',//賬號(hào)
'pass' => 'admin',//密碼
]);
$context = $factory->createContext();
2:聲明主題
//聲明并創(chuàng)建主題 $exchangeName = 'exchange'; $fooTopic = $context->createTopic($exchangeName); $fooTopic->setType(AmqpTopic::TYPE_FANOUT); $context->declareTopic($fooTopic); //刪除主題 $context->deleteTopic($fooTopic);
3:聲明隊(duì)列
//聲明并創(chuàng)建隊(duì)列 $queueName = 'rabbitmq'; $fooQueue = $context->createQueue($queueName); $fooQueue->addFlag(AmqpQueue::FLAG_DURABLE); $context->declareQueue($fooQueue); //刪除隊(duì)列 $context->deleteQueue($fooQueue);
4:將隊(duì)列綁定到主題
$context->bind(new AmqpBind($fooTopic, $fooQueue));
5:發(fā)送消息
//向隊(duì)列發(fā)送消息
$message = $context->createMessage('Hello world!');
$context->createProducer()->send($fooQueue, $message);
//向隊(duì)列發(fā)送優(yōu)先消息
$queueName = 'rabbitmq';
$fooQueue = $context->createQueue(queueName);
$fooQueue->addFlag(AmqpQueue::FLAG_DURABLE);
//設(shè)置隊(duì)列的最大優(yōu)先級(jí)
$fooQueue->setArguments(['x-max-priority' => 10]);
$context->declareQueue($fooQueue);
$message = $context->createMessage('Hello world!');
$context->createProducer()
->setPriority(5) //設(shè)置優(yōu)先級(jí),優(yōu)先級(jí)越高,消息越快到達(dá)消費(fèi)者
->send($fooQueue, $message);
//向隊(duì)列發(fā)送延時(shí)消息
$message = $context->createMessage('Hello world!');
$context->createProducer()
->setDelayStrategy(new RabbitMqDlxDelayStrategy())
->setDeliveryDelay(5000) //消息延時(shí)5秒
->send($fooQueue, $message);
6:消費(fèi)消息【接收消息】
//消費(fèi)消息
$consumer = $context->createConsumer($fooQueue);
$message = $consumer->receive();
// process a message
//業(yè)務(wù)代碼
$consumer->acknowledge($message);//ack應(yīng)答,通知rabbitmq成功,刪除對(duì)應(yīng)任務(wù)
// $consumer->reject($message);ack應(yīng)答,通知rabbitmq失敗,不刪除對(duì)應(yīng)任務(wù)
//訂閱消費(fèi)者
$fooConsumer = $context->createConsumer($fooQueue);
$subscriptionConsumer = $context->createSubscriptionConsumer();
$subscriptionConsumer->subscribe($fooConsumer, function(Message $message, Consumer $consumer) {
// process message
//業(yè)務(wù)代碼
$consumer->acknowledge($message);//ack應(yīng)答,通知rabbitmq成功,刪除對(duì)應(yīng)任務(wù)
// $consumer->reject($message);ack應(yīng)答,通知rabbitmq失敗,不刪除對(duì)應(yīng)任務(wù)
return true;
});
$subscriptionConsumer->consume();
//清除隊(duì)列消息
$queueName = 'rabbitmq';
$queue = $context->createQueue($queueName);
$context->purgeQueue($queue);
三:簡單實(shí)現(xiàn)
1:發(fā)送消息
//連接rabbitmq
$factory = new AmqpConnectionFactory([
'host' => '192.168.6.88',
'port' => '5672',
'vhost' => '/',
'user' => 'admin',
'pass' => 'admin',
'persisted' => false,
]);
$context = $factory->createContext();
//聲明主題
$exchangeName = 'exchange';
$fooTopic = $context->createTopic($exchangeName);
$fooTopic->setType(AmqpTopic::TYPE_FANOUT);
$context->declareTopic($fooTopic);
//聲明隊(duì)列
$queueName = 'rabbitmq';
$fooQueue = $context->createQueue($queueName);
$fooQueue->addFlag(AmqpQueue::FLAG_DURABLE);
$context->declareQueue($fooQueue);
//將隊(duì)列綁定到主題
$context->bind(new AmqpBind($fooTopic, $fooQueue));
//發(fā)送消息到隊(duì)列
$message = $context->createMessage('Hello world!');
$context->createProducer()->send($fooQueue, $message);
2:消費(fèi)消息
$factory = new AmqpConnectionFactory([
'host' => '192.168.6.88',
'port' => '5672',
'vhost' => '/',
'user' => 'admin',
'pass' => 'admin',
'persisted' => false,
]);
$context = $factory->createContext();
$queueName = 'rabbitmq';
$fooQueue = $context->createQueue($queueName);
$fooConsumer = $context->createConsumer($fooQueue);
$subscriptionConsumer = $context->createSubscriptionConsumer();
$subscriptionConsumer->subscribe($fooConsumer, function(Message $message, Consumer $consumer) {
// process message
//業(yè)務(wù)代碼
$consumer->acknowledge($message);//ack應(yīng)答,通知rabbitmq成功,刪除對(duì)應(yīng)任務(wù)
// $consumer->reject($message);ack應(yīng)答,通知rabbitmq失敗,不刪除對(duì)應(yīng)任務(wù)
return true;
});
$subscriptionConsumer->consume();到此這篇關(guān)于PHP使用enqueue/amqp-lib實(shí)現(xiàn)rabbitmq任務(wù)處理的文章就介紹到這了,更多相關(guān)PHP rabbitmq任務(wù)處理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
PHP定時(shí)執(zhí)行計(jì)劃任務(wù)的多種方法小結(jié)
PHP不支持多線程,有時(shí)候處理問題不是那么爽,今天談?wù)撘幌翽HP定時(shí)執(zhí)行的方法2011-12-12
網(wǎng)站用php實(shí)現(xiàn)paypal整合方法
雖然在中國paypal不是很流行,但如果把范圍擴(kuò)大到世界的話,那paypal無疑就是老大了。2010-11-11
PHP批量上傳圖片的具體實(shí)現(xiàn)方法介紹.
這篇文章主要介紹了PHP批量上傳圖片的具體實(shí)現(xiàn)方法。需要的朋友可以過來參考下,希望對(duì)大家有所幫助2014-02-02
PHP應(yīng)用中處理限流和API節(jié)流的最佳實(shí)踐
限流和 API 節(jié)流對(duì)于確保 Web 應(yīng)用程序的可靠性、安全性和可擴(kuò)展性至關(guān)重要,本文將詳細(xì)介紹PHP應(yīng)用中處理限流和API節(jié)流的最佳實(shí)踐,下面就來和小編一起學(xué)習(xí)一下吧2025-09-09
docker中實(shí)現(xiàn)安裝php拓展步驟講解
這篇文章主要介紹了docker中實(shí)現(xiàn)安裝php拓展步驟講解,有不會(huì)安裝的同學(xué)可以跟著操作下2021-01-01
php-perl哈希算法實(shí)現(xiàn)(times33哈希算法)
php-perl哈希實(shí)現(xiàn)算法–DJBX33A(Daniel J. Bernstein, Times 33 with Addition)APR哈希默認(rèn)算法2013-12-12

