Skip to content

Commit c5ee3a6

Browse files
committed
[amqp-ext] move basic consume methods to context.
1 parent e52b476 commit c5ee3a6

4 files changed

Lines changed: 60 additions & 56 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Enqueue\AmqpBunny\Tests\Spec;
4+
5+
use Enqueue\AmqpBunny\AmqpConnectionFactory;
6+
use Interop\Queue\Spec\Amqp\BasicConsumeBreakOnFalseSpec;
7+
8+
/**
9+
* @group functional
10+
*/
11+
class AmqpBasicConsumeBreakOnFalseTest extends BasicConsumeBreakOnFalseSpec
12+
{
13+
/**
14+
* {@inheritdoc}
15+
*/
16+
protected function createContext()
17+
{
18+
$factory = new AmqpConnectionFactory(getenv('AMQP_DSN'));
19+
20+
return $factory->createContext();
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Enqueue\AmqpBunny\Tests\Spec;
4+
5+
use Enqueue\AmqpBunny\AmqpConnectionFactory;
6+
use Interop\Queue\Spec\Amqp\BasicConsumeFromAllSubscribedQueuesSpec;
7+
8+
/**
9+
* @group functional
10+
*/
11+
class AmqpBasicConsumeFromAllSubscribedQueuesTest extends BasicConsumeFromAllSubscribedQueuesSpec
12+
{
13+
/**
14+
* {@inheritdoc}
15+
*/
16+
protected function createContext()
17+
{
18+
$factory = new AmqpConnectionFactory(getenv('AMQP_DSN'));
19+
20+
return $factory->createContext();
21+
}
22+
}

‎pkg/amqp-ext/AmqpConsumer.php‎

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -188,52 +188,6 @@ public function reject(PsrMessage $message, $requeue = false)
188188
);
189189
}
190190

191-
/**
192-
* @param float|int $timeout milliseconds, consumes endlessly if zero set
193-
* @param callable $callback A callback function to which the
194-
* consumed message will be passed. The
195-
* function must accept at a minimum
196-
* one parameter, an \Interop\Amqp\AmqpMessage object,
197-
* and an optional second parameter
198-
* the \Interop\Amqp\AmqpConsumer from which the message was
199-
* consumed. The \Interop\Amqp\AmqpConsumer::consume() will
200-
* not return the processing thread back to
201-
* the PHP script until the callback
202-
* function returns FALSE.
203-
*/
204-
public function basicConsume($timeout, callable $callback = null)
205-
{
206-
/** @var \AMQPQueue $extQueue */
207-
$extConnection = $this->getExtQueue()->getChannel()->getConnection();
208-
209-
$originalTimeout = $extConnection->getReadTimeout();
210-
try {
211-
$extConnection->setReadTimeout($timeout / 1000);
212-
213-
if ($callback) {
214-
$this->getExtQueue()->consume(function (\AMQPEnvelope $extEnvelope, \AMQPQueue $q) use (&$callback) {
215-
$message = $this->convertMessage($extEnvelope);
216-
$message->setConsumerTag($q->getConsumerTag());
217-
218-
$queue = $this->context->createQueue($q->getName());
219-
$consumer = $this->context->createConsumer($queue);
220-
221-
return call_user_func($callback, $message, $consumer);
222-
}, AMQP_JUST_CONSUME);
223-
} else {
224-
$this->getExtQueue()->consume(null, Flags::convertConsumerFlags($this->flags), $this->consumerTag);
225-
}
226-
} catch (\AMQPQueueException $e) {
227-
if ('Consumer timeout exceed' == $e->getMessage()) {
228-
return null;
229-
}
230-
231-
throw $e;
232-
} finally {
233-
$extConnection->setReadTimeout($originalTimeout);
234-
}
235-
}
236-
237191
/**
238192
* @param int $timeout
239193
*

‎pkg/amqp-ext/AmqpContext.php‎

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,10 @@ public function getExtChannel()
314314
*/
315315
public function basicConsumeSubscribe(InteropAmqpConsumer $consumer, callable $callback)
316316
{
317+
if ($consumer->getConsumerTag() && array_key_exists($consumer->getConsumerTag(), $this->basicConsumeSubscribers)) {
318+
return;
319+
}
320+
317321
$extQueue = new \AMQPQueue($this->getExtChannel());
318322
$extQueue->setName($consumer->getQueue()->getQueueName());
319323

@@ -326,16 +330,18 @@ public function basicConsumeSubscribe(InteropAmqpConsumer $consumer, callable $c
326330

327331
public function basicConsumeUnsubscribe(InteropAmqpConsumer $consumer)
328332
{
329-
// if ($consumer->getConsumerTag()) {
330-
// $consumerTag = $consumer->getConsumerTag();
331-
// $consumer->setConsumerTag(null);
332-
//
333-
// $extQueue = new \AMQPQueue($this->getExtChannel());
334-
// $extQueue->setName($consumer->getQueue()->getQueueName());
335-
//
336-
// $extQueue->cancel($consumerTag);
337-
// unset($this->basicConsumeSubscribers[$consumerTag]);
338-
// }
333+
if (false == $consumer->getConsumerTag()) {
334+
return;
335+
}
336+
337+
$consumerTag = $consumer->getConsumerTag();
338+
$consumer->setConsumerTag(null);
339+
340+
$extQueue = new \AMQPQueue($this->getExtChannel());
341+
$extQueue->setName($consumer->getQueue()->getQueueName());
342+
343+
$extQueue->cancel($consumerTag);
344+
unset($this->basicConsumeSubscribers[$consumerTag]);
339345
}
340346

341347
/**

0 commit comments

Comments
 (0)