php AMQP


class MyAMQP
{
    public \AMQPExchange $AMQPExchange;

    public function init(): array
    {
        $AMQPConnection = new \AMQPConnection([
            'host' => '****',
            'port' => 5672,
            'login' => '****',
            'password' => '****',
            'vhost' => '/',
            'heartbeat' => 40,
        ]);
        try {
            if (!$AMQPConnection->connect()) return err(1, 'amqp conn err');
        } catch (\AMQPConnectionException $e) {
            return err(2, $e->getMessage());
        }
        try {
            $AMQPChannel = new \AMQPChannel($AMQPConnection);
        } catch (\AMQPConnectionException $e) {
            return err(3, $e->getMessage());
        }
        try {
            $this->AMQPExchange = new \AMQPExchange($AMQPChannel);
        } catch (\AMQPConnectionException $e) {
            return err(4, $e->getMessage());
        } catch (\AMQPExchangeException $e) {
            return err(5, $e->getMessage());
        }
        $this->AMQPExchange->setName('common_exchange');
        $this->AMQPExchange->setType(AMQP_EX_TYPE_FANOUT);
        /* 看具体情况
        try {
            $this->AMQPExchange->declareExchange();
        } catch (\AMQPChannelException $e) {
            return err(6, $e->getMessage());
        } catch (\AMQPConnectionException $e) {
            return err(7, $e->getMessage());
        } catch (\AMQPExchangeException $e) {
            return err(8, $e->getMessage());
        }
        */
        return succ();
    }

    public function send($msg): array
    {
        try {
            if (!$this->AMQPExchange->publish($msg)) return err(9, 'publish failed');
        } catch (\AMQPChannelException $e) {
            return err(10, $e->getMessage());
        } catch (\AMQPConnectionException $e) {
            return err(11, $e->getMessage());
        } catch (\AMQPExchangeException $e) {
            return err(12, $e->getMessage());
        }
        return succ();
    }
}

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注