App_Amqp_Consumer_MqConsumer.proxy.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. declare (strict_types=1);
  3. namespace App\Amqp\Consumer;
  4. use Hyperf\Amqp\Result;
  5. use Hyperf\Amqp\Annotation\Consumer;
  6. use Hyperf\Amqp\Message\ConsumerMessage;
  7. use Hyperf\Di\Annotation\Inject;
  8. use PhpAmqpLib\Message\AMQPMessage;
  9. use App\JsonRpc\ChatServiceInterface;
  10. #[Consumer(exchange: 'hyperf', routingKey: 'hyperf', queue: 'hyperf', name: "MqConsumer", nums: 1)]
  11. class MqConsumer extends ConsumerMessage
  12. {
  13. use \Hyperf\Di\Aop\ProxyTrait;
  14. use \Hyperf\Di\Aop\PropertyHandlerTrait;
  15. function __construct()
  16. {
  17. if (method_exists(parent::class, '__construct')) {
  18. parent::__construct(...func_get_args());
  19. }
  20. $this->__handlePropertyHandler(__CLASS__);
  21. }
  22. /**
  23. * @var ChatServiceInterface
  24. */
  25. #[Inject]
  26. private $chatServiceClient;
  27. public function consumeMessage($data, AMQPMessage $message) : Result
  28. {
  29. //数据存储
  30. var_dump($data, $message->getBodySize());
  31. $result = $this->chatServiceClient->addTalkRecords($data);
  32. var_dump("消费成功:", $result);
  33. return Result::ACK;
  34. }
  35. // public function isEnable(): bool
  36. // {
  37. // return false;
  38. // }
  39. }