/** * @param array $data * @throws \PHPQueue\Exception\BackendException * @return boolean Status of saving */ public function add($data = array()) { $this->beforeAdd(); $this->checkQueue(); try { $message = new BrokeredMessage(); $message->setBody(json_encode($data)); $this->getConnection()->sendQueueMessage($this->queue_name, $message); } catch (ServiceException $ex) { throw new BackendException($ex->getMessage(), $ex->getCode()); } return true; }
/** * Receives a message. * * Queues: * * @link http://msdn.microsoft.com/en-us/library/windowsazure/hh780735 * @link http://msdn.microsoft.com/en-us/library/windowsazure/hh780756 * * Topic Subscriptions: * @link http://msdn.microsoft.com/en-us/library/windowsazure/hh780722 * @link http://msdn.microsoft.com/en-us/library/windowsazure/hh780770 * * @param string $path The path of the * message. * @param ReceivedMessageOptions $receiveMessageOptions The options to * receive the message. * * @return BrokeredMessage */ public function receiveMessage($path, $receiveMessageOptions = null) { if (is_null($receiveMessageOptions)) { $receiveMessageOptions = new ReceiveMessageOptions(); } $httpCallContext = new HttpCallContext(); $httpCallContext->setPath($path); $httpCallContext->addStatusCode(Resources::STATUS_CREATED); $httpCallContext->addStatusCode(Resources::STATUS_NO_CONTENT); $httpCallContext->addStatusCode(Resources::STATUS_OK); $timeout = $receiveMessageOptions->getTimeout(); if (!is_null($timeout)) { $httpCallContext->addQueryParameter('timeout', $timeout); } if ($receiveMessageOptions->getIsReceiveAndDelete()) { $httpCallContext->setMethod(Resources::HTTP_DELETE); } elseif ($receiveMessageOptions->getIsPeekLock()) { $httpCallContext->setMethod(Resources::HTTP_POST); } else { throw new \InvalidArgumentException(Resources::INVALID_RECEIVE_MODE_MSG); } $response = $this->sendContext($httpCallContext); if ($response->getStatus() === Resources::STATUS_NO_CONTENT) { $brokeredMessage = null; } else { $responseHeaders = $response->getHeader(); $brokerProperties = new BrokerProperties(); if (array_key_exists('brokerproperties', $responseHeaders)) { $brokerProperties = BrokerProperties::create($responseHeaders['brokerproperties']); } if (array_key_exists('location', $responseHeaders)) { $brokerProperties->setLockLocation($responseHeaders['location']); } $brokeredMessage = new BrokeredMessage(); $brokeredMessage->setBrokerProperties($brokerProperties); if (array_key_exists(Resources::CONTENT_TYPE, $responseHeaders)) { $brokeredMessage->setContentType($responseHeaders[Resources::CONTENT_TYPE]); } if (array_key_exists('Date', $responseHeaders)) { $brokeredMessage->setDate($responseHeaders['Date']); } $brokeredMessage->setBody($response->getBody()); foreach (array_keys($responseHeaders) as $headerKey) { $value = $responseHeaders[$headerKey]; $decodedValue = json_decode($value); if (is_scalar($decodedValue)) { $brokeredMessage->setProperty($headerKey, $decodedValue); } } } return $brokeredMessage; }
/** * @covers WindowsAzure\ServiceBus\Models\BrokeredMessage::getBody * @covers WindowsAzure\ServiceBus\Models\BrokeredMessage::setBody */ public function testGetSetBody() { // Setup $expected = 'testBody'; $brokeredMessage = new BrokeredMessage(); // Test $brokeredMessage->setBody($expected); $actual = $brokeredMessage->getBody(); // Assert $this->assertEquals($expected, $actual); }
/** * @covers WindowsAzure\ServiceBus\ServiceBusRestProxy::sendTopicMessage * @covers WindowsAzure\ServiceBus\ServiceBusRestProxy::receiveMessage * @covers WindowsAzure\ServiceBus\ServiceBusRestProxy::receiveSubscriptionMessage * @covers WindowsAzure\ServiceBus\ServiceBusRestProxy::getSubscription */ public function testReceiveMessageWillNotDeleteSubscription() { // Setup $topicName = 'testReceiveMessageWillNotDeleteSubscription'; $subscriptionName = 'sub'; $messageBody = '<p>testReceiveMessageWillNotDeleteSubscription</p>'; $topicInfo = new TopicInfo($topicName); $subscriptionInfo = new SubscriptionInfo($subscriptionName); $brokeredMessage = new BrokeredMessage(); $brokeredMessage->setBody($messageBody); $brokeredMessage->setContentType('text/html'); $createTopicResult = $this->createTopic($topicInfo); $createSubscriptionResult = $this->createSubscription($topicName, $subscriptionInfo); $receiveMessageOptions = new ReceiveMessageOptions(); $receiveMessageOptions->setTimeout(5); $receiveMessageOptions->setReceiveAndDelete(); // Test $this->restProxy->sendTopicMessage($topicName, $brokeredMessage); $receivedMessage = $this->restProxy->receiveSubscriptionMessage($topicName, $subscriptionName, $receiveMessageOptions); $subscriptionInfo = $this->restProxy->getSubscription($topicName, $subscriptionName); // Assert $this->assertNotNull($subscriptionInfo); $this->assertEquals($subscriptionName, $subscriptionInfo->getTitle()); }