hyperf3.0--amqp 投递者简单复用

2023-06-26  本文已影响0人  geeooooz

Index控制器中:

<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Controller;
use Hyperf\Context\ApplicationContext;
use App\Amqp\Producer\DemoProducer;
use Hyperf\Amqp\Producer;
use Hyperf\Di\Annotation\Inject;

class IndexController extends AbstractController
{
    #[Inject]
    protected Producer $producer;

    public function index()
    {
        $message = new DemoProducer(110);
        $producer = ApplicationContext::getContainer()->get(Producer::class);
        $result = $producer->produce($message);
        var_dump($result);
    }
}

投递类修改:

<?php

declare(strict_types=1);

namespace App\Amqp\Producer;

use Hyperf\Amqp\Message\ProducerMessage;

class DemoProducer extends ProducerMessage
{
    public function __construct($data,$setting)
    {
        $this->setExchange($setting['exchange']);
        $this->setRoutingKey($setting['routingKey']);
        $this->payload = $data;
    }
}

上一篇下一篇

猜你喜欢

热点阅读