App_Amqp_Consumer_MqConsumer.proxy.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. $result = $this->chatServiceClient->addTalkRecords($data);
  31. var_dump("消费成功:", $result);
  32. return Result::ACK;
  33. }
  34. // public function isEnable(): bool
  35. // {
  36. // return false;
  37. // }
  38. }