diff --git a/src/Symfony/Component/Messenger/Bridge/Amqp/Transport/Connection.php b/src/Symfony/Component/Messenger/Bridge/Amqp/Transport/Connection.php index e5a9d59eaa3bb..cd2f989d06800 100644 --- a/src/Symfony/Component/Messenger/Bridge/Amqp/Transport/Connection.php +++ b/src/Symfony/Component/Messenger/Bridge/Amqp/Transport/Connection.php @@ -102,6 +102,9 @@ class Connection */ private $amqpDelayExchange; + /** @var int */ + private $lastActivityTime = 0; + public function __construct(array $connectionOptions, array $exchangeOptions, array $queuesOptions, AmqpFactory $amqpFactory = null) { if (!\extension_loaded('amqp')) { @@ -347,6 +350,8 @@ private function publishOnExchange(\AMQPExchange $exchange, string $body, string $attributes['delivery_mode'] = $attributes['delivery_mode'] ?? 2; $attributes['timestamp'] = $attributes['timestamp'] ?? time(); + $this->lastActivityTime = time(); + $exchange->publish( $body, $routingKey, @@ -510,6 +515,11 @@ static function (): bool { } ); } + + $this->lastActivityTime = time(); + } elseif (!empty($this->connectionOptions['heartbeat']) && time() > $this->lastActivityTime + ($this->connectionOptions['heartbeat'] * 2)) { + $disconnectMethod = 'true' === ($this->connectionOptions['persistent'] ?? 'false') ? 'pdisconnect' : 'disconnect'; + $this->amqpChannel->getConnection()->{$disconnectMethod}(); } return $this->amqpChannel;