hyperf / amqp

MIT License
19 stars 15 forks source link

hi, hyperf is awesome framework im so thankful, but i have issue when produce message on RabbitMQ, i dont wanna re-declear exchange and queue #20

Open FarzadForuozanfar opened 5 months ago

FarzadForuozanfar commented 5 months ago
<?php

declare(strict_types=1);

namespace App\Amqp\Producer;

use Hyperf\Amqp\Message\ProducerMessage;
use Hyperf\Amqp\Annotation\Producer;

/**
 * @Producer(exchange="ex.priority", routingKey="priority", exchangeType="direct", durable=true)
 */
class SmsProducer extends ProducerMessage
{
    public function __construct($priority, $message)
    {
        $this->payload = $message;
        $this->setRoutingKey('priority' . $priority);
        $this->setExchange('ex.priority.' . $priority);
        $this->setType('direct');
    }

    public function getExchange(): string
    {
        return 'ex.priority.' . $this->getRoutingKeySuffix();
    }

    public function getRoutingKey(): string
    {
        return 'priority' . $this->getRoutingKeySuffix();
    }

    public function getQueue(): string
    {
        return 'q.priority.' . $this->getRoutingKeySuffix();
    }

    private function getRoutingKeySuffix(): string
    {
        return substr($this->routingKey, -1);
    }

    public function buildExchange(): void
    {
        // return (new ExchangeBuilder())
        //     ->withName($this->getExchange())
        //     ->withType('direct')
        //     ->withDurable(true);
    }

    public function buildQueue(): void
    {
        // return (new QueueBuilder())
        //     ->withName($this->getQueue())
        //     ->withDurable(true);
    }
}
limingxinleo commented 5 months ago

You can try removed annotation #[Producer] from you class.