@@ -33,16 +33,23 @@ class AmqpConsumer implements PsrConsumer
3333 */
3434 private $ isInit ;
3535
36+ /**
37+ * @var string
38+ */
39+ private $ receiveMethod ;
40+
3641 /**
3742 * @param AmqpContext $context
3843 * @param AmqpQueue $queue
3944 * @param Buffer $buffer
45+ * @param string $receiveMethod
4046 */
41- public function __construct (AmqpContext $ context , AmqpQueue $ queue , Buffer $ buffer )
47+ public function __construct (AmqpContext $ context , AmqpQueue $ queue , Buffer $ buffer, $ receiveMethod )
4248 {
4349 $ this ->queue = $ queue ;
4450 $ this ->context = $ context ;
4551 $ this ->buffer = $ buffer ;
52+ $ this ->receiveMethod = $ receiveMethod ;
4653
4754 $ this ->isInit = false ;
4855 }
@@ -64,61 +71,69 @@ public function getQueue()
6471 */
6572 public function receive ($ timeout = 0 )
6673 {
67- // @see https://github.com/php-enqueue/enqueue-dev/issues/110 and https://github.com/pdezwart/php-amqp/issues/281
68- $ end = microtime (true ) + ($ timeout / 1000 );
74+ if ( ' basic_get ' == $ this -> receiveMethod ) {
75+ $ end = microtime (true ) + ($ timeout / 1000 );
6976
70- while (0 === $ timeout || microtime (true ) < $ end ) {
71- if ($ message = $ this ->receiveNoWait ()) {
72- return $ message ;
77+ while (0 === $ timeout || microtime (true ) < $ end ) {
78+ if ($ message = $ this ->receiveNoWait ()) {
79+ return $ message ;
80+ }
7381 }
82+
83+ return ;
84+ }
85+
86+ if (false == (version_compare (phpversion ('amqp ' ), '1.9.1 ' , '>= ' ) || phpversion ('amqp ' ) == '1.9.1-dev ' )) {
87+ // @see https://github.com/php-enqueue/enqueue-dev/issues/110 and https://github.com/pdezwart/php-amqp/issues/281
88+ throw new \LogicException ('The "basic_consume" method does not work on amqp extension prior 1.9.1 version. ' );
7489 }
7590
76- // if ($this->isInit && $message = $this->buffer->pop($this->getExtQueue()->getConsumerTag())) {
77- // return $message;
78- // }
79- //
80- // /** @var \AMQPQueue $extQueue */
81- // $extConnection = $this->getExtQueue()->getChannel()->getConnection();
82- //
83- // $originalTimeout = $extConnection->getReadTimeout();
84- // try {
85- // $extConnection->setReadTimeout($timeout / 1000);
86- //
87- // if (false == $this->isInit) {
88- // $this->getExtQueue()->consume(null, AMQP_NOPARAM);
89- //
90- // $this->isInit = true;
91- // }
92- //
93- // /** @var AmqpMessage|null $message */
94- // $message = null;
95- //
96- // $this->getExtQueue()->consume(function (\AMQPEnvelope $extEnvelope, \AMQPQueue $q) use (&$message) {
97- // $message = $this->convertMessage($extEnvelope);
98- // $message->setConsumerTag($q->getConsumerTag());
99- //
100- // if ($this->getExtQueue()->getConsumerTag() == $q->getConsumerTag()) {
101- // return false;
102- // }
103- //
104- // // not our message, put it to buffer and continue.
105- // $this->buffer->push($q->getConsumerTag(), $message);
106- //
107- // $message = null;
108- //
109- // return true;
110- // }, AMQP_JUST_CONSUME);
111- //
112- // return $message;
113- // } catch (\AMQPQueueException $e) {
114- // if ('Consumer timeout exceed' == $e->getMessage()) {
115- // return null;
116- // }
117- //
118- // throw $e;
119- // } finally {
120- // $extConnection->setReadTimeout($originalTimeout);
121- // }
91+ if ($ this ->isInit && $ message = $ this ->buffer ->pop ($ this ->getExtQueue ()->getConsumerTag ())) {
92+ return $ message ;
93+ }
94+
95+ /** @var \AMQPQueue $extQueue */
96+ $ extConnection = $ this ->getExtQueue ()->getChannel ()->getConnection ();
97+
98+ $ originalTimeout = $ extConnection ->getReadTimeout ();
99+ try {
100+ $ extConnection ->setReadTimeout ($ timeout / 1000 );
101+
102+ if (false == $ this ->isInit ) {
103+ $ this ->getExtQueue ()->consume (null , AMQP_NOPARAM );
104+
105+ $ this ->isInit = true ;
106+ }
107+
108+ /** @var AmqpMessage|null $message */
109+ $ message = null ;
110+
111+ $ this ->getExtQueue ()->consume (function (\AMQPEnvelope $ extEnvelope , \AMQPQueue $ q ) use (&$ message ) {
112+ $ message = $ this ->convertMessage ($ extEnvelope );
113+ $ message ->setConsumerTag ($ q ->getConsumerTag ());
114+
115+ if ($ this ->getExtQueue ()->getConsumerTag () == $ q ->getConsumerTag ()) {
116+ return false ;
117+ }
118+
119+ // not our message, put it to buffer and continue.
120+ $ this ->buffer ->push ($ q ->getConsumerTag (), $ message );
121+
122+ $ message = null ;
123+
124+ return true ;
125+ }, AMQP_JUST_CONSUME );
126+
127+ return $ message ;
128+ } catch (\AMQPQueueException $ e ) {
129+ if ('Consumer timeout exceed ' == $ e ->getMessage ()) {
130+ return null ;
131+ }
132+
133+ throw $ e ;
134+ } finally {
135+ $ extConnection ->setReadTimeout ($ originalTimeout );
136+ }
122137 }
123138
124139 /**
0 commit comments