@@ -41,11 +41,6 @@ class AmqpConsumer implements InteropAmqpConsumer
4141 */
4242 private $ receiveMethod ;
4343
44- /**
45- * @var InteropAmqpMessage
46- */
47- private $ receivedMessage ;
48-
4944 /**
5045 * @var int
5146 */
@@ -164,8 +159,6 @@ public function acknowledge(PsrMessage $message)
164159 {
165160 InvalidMessageException ::assertMessageInstanceOf ($ message , InteropAmqpMessage::class);
166161
167- var_dump ($ message ->getDeliveryTag ());
168-
169162 $ this ->channel ->basic_ack ($ message ->getDeliveryTag ());
170163 }
171164
@@ -233,16 +226,9 @@ private function receiveBasicConsume($timeout)
233226 $ receivedMessage = $ this ->convertMessage ($ message );
234227 $ receivedMessage ->setConsumerTag ($ message ->delivery_info ['consumer_tag ' ]);
235228
236- if ($ this ->consumerTag === $ receivedMessage ->getConsumerTag ()) {
237- $ this ->receivedMessage = $ receivedMessage ;
238- } else {
239- // not our message, put it to buffer and continue.
240- $ this ->buffer ->push ($ receivedMessage ->getConsumerTag (), $ receivedMessage );
241- }
229+ $ this ->buffer ->push ($ receivedMessage ->getConsumerTag (), $ receivedMessage );
242230 };
243231
244- $ this ->channel ->basic_qos (0 , 1 , false );
245-
246232 $ consumerTag = $ this ->channel ->basic_consume (
247233 $ this ->queue ->getQueueName (),
248234 $ this ->getConsumerTag () ?: $ this ->getQueue ()->getConsumerTag (),
@@ -253,7 +239,7 @@ private function receiveBasicConsume($timeout)
253239 $ callback
254240 );
255241
256- $ this ->consumerTag = $ consumerTag ?: $ this ->queue ->getConsumerTag ();
242+ $ this ->consumerTag = $ consumerTag ?: $ this ->getQueue () ->getConsumerTag ();
257243
258244 if (empty ($ this ->consumerTag )) {
259245 throw new Exception ('Got empty consumer tag ' );
@@ -266,16 +252,31 @@ private function receiveBasicConsume($timeout)
266252 return $ message ;
267253 }
268254
269- $ this ->receivedMessage = null ;
270-
271255 try {
272- echo 'here ' , PHP_EOL ;
273- $ this ->channel ->wait (null , false , $ timeout );
274- echo 'here1 ' , PHP_EOL ;
275- } catch (AMQPTimeoutException $ e ) {
276- echo 'here2 ' , PHP_EOL ;
277- }
256+ while (true ) {
257+ $ start = microtime (true );
258+
259+ $ this ->channel ->wait (null , false , $ timeout / 1000 );
278260
279- return $ this ->receivedMessage ;
261+ if ($ message = $ this ->buffer ->pop ($ this ->consumerTag )) {
262+ return $ message ;
263+ }
264+
265+ // is here when consumed message is not for this consumer
266+
267+ // as timeout is infinite have to continue consumption, but it can overflow message buffer
268+ if ($ timeout <= 0 ) {
269+ continue ;
270+ }
271+
272+ // compute remaining timeout and continue until time is up
273+ $ stop = microtime (true );
274+ $ timeout -= ($ stop - $ start ) * 1000 ;
275+
276+ if ($ timeout <= 0 ) {
277+ break ;
278+ }
279+ }
280+ } catch (AMQPTimeoutException $ e ) {}
280281 }
281282}
0 commit comments