PHP使用enqueue/amqp-lib实现rabbitmq任务处理

本文详细介绍了如何在PHP中使用enqueue/amqp-lib库与RabbitMQ进行任务处理,包括连接、声明主题和队列、绑定、发送优先级消息和延时消息,以及消费消息的实现过程。
摘要由CSDN通过智能技术生成

教程下载地址: 网赚博客https://www.piaodoo.com/创业项目排行榜前十名https://www.piaodoo.com/


一:拓展安装

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' => '/',//虚拟主机
    'user' => 'admin',//账号
    'pass' => 'admin',//密码
]);
$context = $factory->createContext();

2:声明主题

//声明并创建主题
$exchangeName = 'exchange';
$fooTopic = $context->createTopic($exchangeName);
$fooTopic->setType(AmqpTopic::TYPE_FANOUT);
$context->declareTopic($fooTopic);

//删除主题
KaTeX parse error: Expected 'EOF', got '&' at position 9: context-&̲gt;deleteTopic(fooTopic);

3:声明队列

//声明并创建队列
$queueName = 'rabbitmq';
$fooQueue = $context->createQueue($queueName);
$fooQueue->addFlag(AmqpQueue::FLAG_DURABLE);
$context->declareQueue($fooQueue);

//删除队列
KaTeX parse error: Expected 'EOF', got '&' at position 9: context-&̲gt;deleteQueue(fooQueue);

4:将队列绑定到主题

$context->bind(new AmqpBind($fooTopic, $fooQueue));

5:发送消息

//向队列发送消息
$message = $context->createMessage('Hello world!');
$context->createProducer()->send($fooQueue, $message);

//向队列发送优先消息
$queueName = ‘rabbitmq’;
$fooQueue = $context->createQueue(queueName);
$fooQueue->addFlag(AmqpQueue::FLAG_DURABLE);
//设置队列的最大优先级
$fooQueue->setArguments([‘x-max-priority’ => 10]);
KaTeX parse error: Expected 'EOF', got '&' at position 9: context-&̲gt;declareQueue…fooQueue);

$message = $context->createMessage(‘Hello world!’);

KaTeX parse error: Expected 'EOF', got '&' at position 9: context-&̲gt;createProduc…fooQueue, $message);

//向队列发送延时消息
$message = $context->createMessage(‘Hello world!’);

KaTeX parse error: Expected 'EOF', got '&' at position 9: context-&̲gt;createProduc…fooQueue, $message);

6:消费消息【接收消息】

//消费消息
$consumer = $context->createConsumer($fooQueue);

$message = $consumer->receive();

// process a message
//业务代码

KaTeX parse error: Expected 'EOF', got '&' at position 10: consumer-&̲gt;acknowledge(message);//ack应答,通知rabbitmq成功,删除对应任务
// KaTeX parse error: Expected 'EOF', got '&' at position 10: consumer-&̲gt;reject(message);ack应答,通知rabbitmq失败,不删除对应任务

//订阅消费者
$fooConsumer = KaTeX parse error: Expected 'EOF', got '&' at position 9: context-&̲gt;createConsum…fooQueue);

$subscriptionConsumer = $context->createSubscriptionConsumer();
KaTeX parse error: Expected 'EOF', got '&' at position 22: …iptionConsumer-&̲gt;subscribe(fooConsumer, function(Message $message, Consumer $consumer) {
// process message
//业务代码
KaTeX parse error: Expected 'EOF', got '&' at position 10: consumer-&̲gt;acknowledge(message);//ack应答,通知rabbitmq成功,删除对应任务
// KaTeX parse error: Expected 'EOF', got '&' at position 10: consumer-&̲gt;reject(message);ack应答,通知rabbitmq失败,不删除对应任务

return true;

});
$subscriptionConsumer->consume();

//清除队列消息
$queueName = ‘rabbitmq’;
$queue = KaTeX parse error: Expected 'EOF', got '&' at position 9: context-&̲gt;createQueue(queueName);
KaTeX parse error: Expected 'EOF', got '&' at position 9: context-&̲gt;purgeQueue(queue);

三:简单实现 

1:发送消息

//连接rabbitmq
$factory = new AmqpConnectionFactory([
    'host' => '192.168.6.88',
    'port' => '5672',
    'vhost' => '/',
    'user' => 'admin',
    'pass' => 'admin',
    'persisted' => false,
]);

$context = $factory->createContext();
//声明主题
$exchangeName = ‘exchange’;
$fooTopic = KaTeX parse error: Expected 'EOF', got '&' at position 9: context-&̲gt;createTopic(exchangeName);
$fooTopic->setType(AmqpTopic::TYPE_FANOUT);
KaTeX parse error: Expected 'EOF', got '&' at position 9: context-&̲gt;declareTopic…fooTopic);

//声明队列
$queueName = ‘rabbitmq’;
$fooQueue = KaTeX parse error: Expected 'EOF', got '&' at position 9: context-&̲gt;createQueue(queueName);
$fooQueue->addFlag(AmqpQueue::FLAG_DURABLE);
KaTeX parse error: Expected 'EOF', got '&' at position 9: context-&̲gt;declareQueue…fooQueue);

//将队列绑定到主题
KaTeX parse error: Expected 'EOF', got '&' at position 9: context-&̲gt;bind(new Amq…fooTopic, $fooQueue));

//发送消息到队列
$message = $context->createMessage(‘Hello world!’);

KaTeX parse error: Expected 'EOF', got '&' at position 9: context-&̲gt;createProduc…fooQueue, $message);

2:消费消息

$factory = new AmqpConnectionFactory([
    'host' => '192.168.6.88',
    'port' => '5672',
    'vhost' => '/',
    'user' => 'admin',
    'pass' => 'admin',
    'persisted' => false,
]);
$context = $factory->createContext();

$queueName = ‘rabbitmq’;
$fooQueue = KaTeX parse error: Expected 'EOF', got '&' at position 9: context-&̲gt;createQueue(queueName);

$fooConsumer = KaTeX parse error: Expected 'EOF', got '&' at position 9: context-&̲gt;createConsum…fooQueue);

$subscriptionConsumer = $context->createSubscriptionConsumer();
KaTeX parse error: Expected 'EOF', got '&' at position 22: …iptionConsumer-&̲gt;subscribe(fooConsumer, function(Message $message, Consumer $consumer) {
// process message
//业务代码
KaTeX parse error: Expected 'EOF', got '&' at position 10: consumer-&̲gt;acknowledge(message);//ack应答,通知rabbitmq成功,删除对应任务
// KaTeX parse error: Expected 'EOF', got '&' at position 10: consumer-&̲gt;reject(message);ack应答,通知rabbitmq失败,不删除对应任务

return true;

});
$subscriptionConsumer->consume();

到此这篇关于PHP使用enqueue/amqp-lib实现rabbitmq任务处理的文章就介绍到这了,更多相关PHP rabbitmq任务处理内容请搜索网赚博客http://www.piaodoo.com/以前的文章或继续浏览下面的相关文章希望大家以后多多支持网赚博客http://www.piaodoo.com/!

                        友情连接:  

茂名一技http://www.szsyby.net/


茂名一技http://www.enechn.com/


美文集http://www.tpyjn.cn/


手游排行前十名http://www.bjkhrx.com/

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值