99use PhpAmqpLib \Channel \AMQPChannel ;
1010use PhpAmqpLib \Connection \AbstractConnection ;
1111use PhpAmqpLib \Connection \AMQPStreamConnection ;
12+ use PhpAmqpLib \Connection \Heartbeat \PCNTLHeartbeatSender ;
1213use PhpAmqpLib \Exception \AMQPProtocolChannelException ;
14+ use PhpAmqpLib \Exception \AMQPRuntimeException ;
1315use PhpAmqpLib \Exception \AMQPTimeoutException ;
1416use PhpAmqpLib \Message \AMQPMessage ;
1517use PhpAmqpLib \Wire \AMQPTable ;
@@ -83,6 +85,11 @@ class AmqpQueueProvider extends AbstractQueueProvider
8385 */
8486 private $ _slowPushThreshold = 0 ;
8587
88+ /**
89+ * @var ?PCNTLHeartbeatSender
90+ */
91+ protected $ _heartbeatSender ;
92+
8693 protected function _construct ()
8794 {
8895 $ this ->_fixedConsumerCallback = [$ this , 'consumerCallback ' ];
@@ -116,9 +123,8 @@ public function pushBatch(array $batch, $persistent = null)
116123 $ returnCallback = null ;
117124 if ($ mandatory )
118125 {
119- $ returnCallback = function ($ replyCode , $ replyText , $ exchange , $ routingKey ) use
120- (&$ needRetry , &$ needDeclare , &$ autoDeclare , $ declareAttempts , $ declareRetryLimit )
121- {
126+ $ returnCallback = function ($ replyCode , $ replyText , $ exchange , $ routingKey )
127+ use (&$ needRetry , &$ needDeclare , &$ autoDeclare , $ declareAttempts , $ declareRetryLimit ) {
122128 if ($ autoDeclare && ($ declareAttempts < $ declareRetryLimit ) && ($ replyCode == 312 ))
123129 {
124130 $ needDeclare = true ;
@@ -520,7 +526,17 @@ protected function _getConnection($connectionMode)
520526 $ host ,
521527 $ config ->getItem ('port ' , 5672 ),
522528 $ config ->getItem ('username ' , 'guest ' ),
523- $ config ->getItem ('password ' , 'guest ' )
529+ $ config ->getItem ('password ' , 'guest ' ),
530+ $ config ->getItem ('vhost ' , '/ ' ),
531+ false ,
532+ 'AMQPLAIN ' ,
533+ null ,
534+ 'en_US ' ,
535+ $ config ->getItem ('connection_timeout ' , 3 ),
536+ $ config ->getItem ('read_write_timeout ' , 3 ),
537+ null ,
538+ (bool )$ config ->getItem ('keepalive ' , false ),
539+ $ config ->getItem ('heartbeat ' , 0 )
524540 );
525541 }
526542 catch (Exception $ e )
@@ -534,6 +550,20 @@ protected function _getConnection($connectionMode)
534550 );
535551 $ this ->_lastConnectTimes [$ connectionMode ] = time ();
536552 }
553+
554+ try
555+ {
556+ if ($ this ->_heartbeatSender )
557+ {
558+ $ this ->_heartbeatSender ->unregister ();
559+ }
560+ $ this ->_heartbeatSender = new PCNTLHeartbeatSender ($ this ->_connections [$ connectionMode ]);
561+ $ this ->_heartbeatSender ->register ();
562+ }
563+ catch (AMQPRuntimeException $ e )
564+ {
565+ }
566+
537567 return $ this ->_connections [$ connectionMode ];
538568 }
539569
@@ -630,6 +660,12 @@ private function _disconnect($connectionMode)
630660 {
631661 }
632662 $ this ->_channels [$ connectionMode ] = null ;
663+
664+ if ($ this ->_heartbeatSender )
665+ {
666+ $ this ->_heartbeatSender ->unregister ();
667+ $ this ->_heartbeatSender = null ;
668+ }
633669 try
634670 {
635671 if ((!empty ($ this ->_connections [$ connectionMode ]))
0 commit comments