DelayCommand.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Command;
  4. use App\Amqp\Producer\DelayDirectProducer;
  5. use App\Amqp\Producer\MqProducer;
  6. use Hyperf\Amqp\Producer;
  7. use Hyperf\Command\Command as HyperfCommand;
  8. use Hyperf\Command\Annotation\Command;
  9. use Hyperf\Context\ApplicationContext as ContextApplicationContext;
  10. use Hyperf\Utils\ApplicationContext;
  11. use Psr\Container\ContainerInterface;
  12. /**
  13. * @Command
  14. */
  15. #[Command]
  16. class DelayCommand extends HyperfCommand
  17. {
  18. /**
  19. * @var ContainerInterface
  20. */
  21. protected $container;
  22. public function __construct(ContainerInterface $container)
  23. {
  24. $this->container = $container;
  25. parent::__construct('demo:queue');
  26. }
  27. public function configure()
  28. {
  29. parent::configure();
  30. $this->setDescription('生产消息发布 ');
  31. }
  32. public function handle()
  33. {
  34. for ($i = 0; $i < 100; $i++) {
  35. $data = [
  36. "ss" => 122,
  37. "num" => $i,
  38. "number" => mt_rand(1111, 9999),
  39. "time" => time(),
  40. ];
  41. $message = new MqProducer($data);
  42. $producer = ContextApplicationContext::getContainer()->get(Producer::class);
  43. $ret = $producer->produce($message);
  44. }
  45. print_r($ret);
  46. $this->line('Hello Hyperf!', 'info');
  47. exit();
  48. }
  49. }