Exemple #1
0
 /**
  * Attach consumer to process payload from queue
  *
  * @param AbstractConsumer $consumer Consumer to process payload and handle possible errors
  * @param bool             $auto_ack Should message been acknowledged upon receive
  *
  * @return mixed
  *
  * @throws SerializerException
  * @throws Exception           Any exception from pre/post-consume handlers and from exception handler
  */
 public function consume(AbstractConsumer $consumer, $auto_ack = false)
 {
     if (!$consumer->active()) {
         // prevent dirty consumer been listening on queue
         return;
     }
     $outside_error = null;
     try {
         $consumer->begin($this);
         $this->queue->consume(function (AMQPEnvelope $envelope) use($consumer) {
             $delivery = $this->builder->build($envelope);
             $this->feed($delivery, $consumer);
             return $consumer->active();
         }, $auto_ack ? AMQP_AUTOACK : AMQP_NOPARAM);
     } catch (Exception $e) {
         $outside_error = $e;
     }
     try {
         $this->queue->cancel();
     } catch (Exception $e) {
     }
     $consumer->end($this, $outside_error);
     if ($outside_error) {
         throw $outside_error;
     }
 }
 /**
  * @covers AMQPy\AbstractConsumer::active
  * @covers AMQPy\AbstractConsumer::activate
  * @covers AMQPy\AbstractConsumer::stop
  */
 public function testActiveAndRelated()
 {
     $this->assertTrue($this->object->active());
     $this->assertFalse($this->object->stop());
     $this->assertFalse($this->object->stop());
     $this->assertFalse($this->object->active());
     $this->assertTrue($this->object->activate());
     $this->assertTrue($this->object->activate());
     $this->assertTrue($this->object->active());
     $this->assertFalse($this->object->stop());
     $this->assertFalse($this->object->active());
     $this->assertTrue($this->object->activate());
     $this->assertTrue($this->object->active());
 }
Exemple #3
0
 public function feed(Delivery $delivery, AbstractConsumer $consumer)
 {
     $consumer->before($delivery, $this);
     $consumer_exception = null;
     $consume_result = null;
     $consume_payload = null;
     try {
         $consume_payload = $this->getSerializers()->get($delivery->getProperties()->getContentType())->parse($delivery->getBody());
         $consume_result = $consumer->consume($consume_payload, $delivery, $this);
     } catch (Exception $e) {
         $consumer_exception = $e;
     }
     if ($consumer_exception) {
         $consumer->failure($consumer_exception, $delivery, $this);
     } else {
         $consumer->after($consume_result, $delivery, $this);
     }
     $consumer->always($consume_result, $consume_payload, $delivery, $this, $consumer_exception);
 }
Exemple #4
0
 public function stop()
 {
     parent::stop();
 }