/**
  * send some object as message to an exchange
  *
  * If $message is not a string, it will be converted to such.
  * Objects and arrays will be JSON encoded, Primitive types
  * will be casted into a string.
  *
  * If the $silent parameter is set to true (default) any exceptions from the
  * service are suppressed. The return value is true or false then.
  *
  * @param array|object|string $message
  * @param string $exchange
  * @param null|string $routingKey
  * @param boolean $silent
  * @return bool
  * @throws Exception
  * @throws PhpAmqpLib\Exception\AMQPRuntimeException
  */
 public function sendToExchange($message, $exchange, $routingKey = NULL, $silent = TRUE)
 {
     $message = $this->castMessageToString($message);
     $amqpMessage = new \PhpAmqpLib\Message\AMQPMessage($message);
     try {
         $this->amqpService->send($amqpMessage, $exchange, $routingKey);
         return TRUE;
     } catch (\PhpAmqpLib\Exception\AMQPRuntimeException $e) {
         if ($silent === FALSE) {
             throw $e;
         }
         return FALSE;
     }
 }
Esempio n. 2
0
 /**
  * Deletes the binding
  *
  * @param Tx_Amqp_Messaging_Binding $binding
  * @return void
  */
 public function deleteBinding(Tx_Amqp_Messaging_Binding $binding)
 {
     $this->service->execute(function (\PhpAmqpLib\Channel\AMQPChannel $channel) use($binding) {
         if ($binding->isDestinationQueue()) {
             $channel->queue_unbind($binding->getDestination(), $binding->getExchange(), $binding->getRoutingKey(), $binding->getArguments());
         } else {
             $channel->exchange_unbind($binding->getExchange(), $binding->getDestination(), $binding->getRoutingKey(), $binding->getArguments());
         }
     });
 }
 protected function getConnectionStatus()
 {
     try {
         $service = new Tx_Amqp_Messaging_AMQPService(Tx_Amqp_Util_ConfigurationHelper::getConnectionFactory());
         $admin = new Tx_Amqp_Messaging_AMQPAdmin($service);
         $queue = $admin->declareQueue();
         $messageBody = 'ping ' . microtime(TRUE);
         $service->send(new \PhpAmqpLib\Message\AMQPMessage($messageBody), Tx_Amqp_Messaging_Exchange::DEFAULT_EXCHANGE, $queue->getName());
         $receivedMessage = $service->receive($queue->getName());
         $admin->deleteQueue($queue->getName());
         if ($messageBody !== $receivedMessage->body) {
             return new tx_reports_reports_status_Status('AMQP', 'Warning', sprintf('Send and receive not identical. Message sent [%s] differs from received message [%s]', $messageBody, $receivedMessage->body), tx_reports_reports_status_Status::WARNING);
         }
         return new tx_reports_reports_status_Status('AMQP', 'OK', 'Connection successful. Send and receive OK.', tx_reports_reports_status_Status::OK);
     } catch (\Exception $e) {
         $statusMessage = $e->getMessage() . ' (' . get_class($e) . ')';
         return new tx_reports_reports_status_Status('AMQP', 'Error', 'Unable to send and receive messages. Reason: ' . $statusMessage, tx_reports_reports_status_Status::ERROR);
     }
 }
 /**
  * Tests the basic.return if a message cannot be delivered immediately.
  *
  * @test
  */
 public function routeToQueueWithNoListenersShouldBasicReturn()
 {
     $this->markTestSkipped('immediate=true has been removed since RabbitMQ 3.0.0');
     $queue = $this->admin->declareQueue();
     $this->service->setImmediate(TRUE);
     $self = $this;
     $messageBody = 'basic.return with no consumers listening';
     $this->service->send(new \PhpAmqpLib\Message\AMQPMessage($messageBody), Tx_Amqp_Messaging_AMQPService::DEFAULT_EXCHANGE, $queue->getName());
     $this->service->handleReturn(function (Tx_Amqp_Messaging_UndeliverableMessage $message) use($self, $messageBody) {
         $self->assertEquals($messageBody, $message->getMessage()->body);
         Tx_Amqp_Messaging_AMQPUtils::throwStopException();
     });
     $this->admin->deleteQueue($queue->getName());
 }