| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- <?php
- /**
- * 库存管理消费者
- * 处理花材库存补充、扣减等库存操作
- */
- namespace common\components\rabbitmq;
- use common\components\noticeUtil;
- use mikemadisonweb\rabbitmq\components\ConsumerInterface;
- use PhpAmqpLib\Message\AMQPMessage;
- use Yii;
- class stockConsumer implements ConsumerInterface
- {
- /**
- * 执行消费者逻辑
- *
- * @param AMQPMessage $msg 消息对象
- * @return string 消息处理结果
- *
- * ConsumerInterface::MSG_ACK - 确认消息(标记为已处理)并从队列中删除
- * ConsumerInterface::MSG_REJECT - 拒绝并从队列中删除消息
- * ConsumerInterface::MSG_REQUEUE - 拒绝并重新入队消息
- */
- public function execute(AMQPMessage $msg)
- {
- try {
- // 反序列化消息体
- $data = unserialize($msg->body);
- if (!is_array($data)) {
- noticeUtil::push("库存的消费者报错:Invalid notify message format: {$msg->body}", '15280215347');
- return ConsumerInterface::MSG_REJECT;
- }
- print_r($data);
- // 根据操作类型分发处理
- $type = $data['type'] ?? null;
- switch ($type) {
- case 'add':
- $result = true;
- echo '持久化OK---';
- print_r($data);
- break;
- default:
- noticeUtil::push("库存的消费者报错,未知 type: {$type}");
- $result = false;
- }
- if ($result) {
- return ConsumerInterface::MSG_ACK;
- } else {
- noticeUtil::push("库存的消费者报错:Stock message processing failed");
- return ConsumerInterface::MSG_REQUEUE;
- }
- } catch (\Exception $e) {
- noticeUtil::push("库存的消费者报错:" . $e->getMessage());
- return ConsumerInterface::MSG_REQUEUE;
- }
- }
- }
|