Example #1
0
function peekNextMessageSample($queueClient)
{
    // OPTIONAL: Set peek message options.
    $message_options = new PeekMessagesOptions();
    $message_options->setNumberOfMessages(1);
    // Default value is 1.
    try {
        $peekMessagesResult = $queueClient->peekMessages("myqueue", $message_options);
    } catch (ServiceException $e) {
        $code = $e->getCode();
        $error_message = $e->getMessage();
        echo $code . ": " . $error_message . PHP_EOL;
    }
    $messages = $peekMessagesResult->getQueueMessages();
    // View messages.
    $messageCount = count($messages);
    if ($messageCount <= 0) {
        echo "There are no messages." . PHP_EOL;
    } else {
        foreach ($messages as $message) {
            echo "Peeked message:" . PHP_EOL;
            echo "Message Id: " . $message->getMessageId() . PHP_EOL;
            echo "Date: " . date_format($message->getInsertionDate(), 'Y-m-d') . PHP_EOL;
            $msg = $message->getMessageText();
            // optional: $msg = base64_decode($msg);
            echo "Message text: " . $msg . PHP_EOL . PHP_EOL;
        }
    }
}
 /**
  * @covers MicrosoftAzure\Storage\Queue\Models\PeekMessagesOptions::setNumberOfMessages
  */
 public function testSetNumberOfMessages()
 {
     // Setup
     $peekMessagesOptions = new PeekMessagesOptions();
     $expected = 10;
     // Test
     $peekMessagesOptions->setNumberOfMessages($expected);
     // Assert
     $actual = $peekMessagesOptions->getNumberOfMessages();
     $this->assertEquals($expected, $actual);
 }
 public function testCheckPeekMessagesOptions()
 {
     $options = new PeekMessagesOptions();
     $this->assertNull($options->getTimeout(), 'Default PeekMessagesOptions->getTimeout');
     $this->assertNull($options->getNumberOfMessages(), 'Default PeekMessagesOptions->getNumberOfMessages');
     $options->setTimeout(self::INT_MAX_VALUE);
     $options->setNumberOfMessages(0);
     $this->assertEquals(self::INT_MAX_VALUE, $options->getTimeout(), 'Set PeekMessagesOptions->getTimeout');
     $this->assertEquals(0, $options->getNumberOfMessages(), 'Set PeekMessagesOptions->getNumberOfMessages');
 }
 /**
  * @covers MicrosoftAzure\Storage\ServiceBus\ServiceBusRestProxy::clearMessages
  * @covers MicrosoftAzure\Storage\ServiceBus\ServiceBusRestProxy::createMessage
  * @covers MicrosoftAzure\Storage\ServiceBus\ServiceBusRestProxy::listMessages
  * @covers MicrosoftAzure\Storage\ServiceBus\ServiceBusRestProxy::peekMessages
  */
 private function peekMessagesWorker($options)
 {
     self::println('Trying $options: ' . self::tmptostring($options));
     $queue = QueueServiceFunctionalTestData::$testQueueNames;
     $queue = $queue[0];
     $this->restProxy->clearMessages($queue);
     // Put three messages into the queue.
     $this->restProxy->createMessage($queue, QueueServiceFunctionalTestData::getSimpleMessageText());
     $this->restProxy->createMessage($queue, QueueServiceFunctionalTestData::getSimpleMessageText());
     $this->restProxy->createMessage($queue, QueueServiceFunctionalTestData::getSimpleMessageText());
     // Default is 1 message
     $effectiveNumOfMessages = is_null($options) || is_null($options->getNumberOfMessages()) ? 1 : $options->getNumberOfMessages();
     $effectiveNumOfMessages = $effectiveNumOfMessages < 0 ? 0 : $effectiveNumOfMessages;
     $expectedNumMessagesFirst = $effectiveNumOfMessages > 3 ? 3 : $effectiveNumOfMessages;
     try {
         $res = is_null($options) ? $this->restProxy->peekMessages($queue) : $this->restProxy->peekMessages($queue, $options);
         if (is_null($options)) {
             $options = new PeekMessagesOptions();
         }
         if (!is_null($options->getNumberOfMessages()) && ($options->getNumberOfMessages() < 1 || $options->getNumberOfMessages() > 32)) {
             $this->assertTrue(false, 'Expect  getNumberOfMessages < 1 or 32 < numMessages in $options to throw');
         } else {
             if (!is_null($options->getTimeout()) && $options->getTimeout() < 1) {
                 $this->assertTrue(false, 'Expect negative timeouts in $options to throw');
             }
         }
         $this->assertEquals($expectedNumMessagesFirst, count($res->getQueueMessages()), 'getQueueMessages() count');
         $opts = new PeekMessagesOptions();
         $opts->setNumberOfMessages(32);
         $res2 = $this->restProxy->peekMessages($queue, $opts);
         $this->assertEquals(3, count($res2->getQueueMessages()), 'getQueueMessages() count');
         $this->restProxy->listMessages($queue);
         $opts = new PeekMessagesOptions();
         $opts->setNumberOfMessages(32);
         $res3 = $this->restProxy->peekMessages($queue, $opts);
         $this->assertEquals(2, count($res3->getQueueMessages()), 'getQueueMessages() count');
     } catch (ServiceException $e) {
         if (is_null($options)) {
             $options = new PeekMessagesOptions();
         }
         if (!is_null($options->getTimeout()) && $options->getTimeout() < 1) {
             $this->assertEquals(TestResources::STATUS_INTERNAL_SERVER_ERROR, $e->getCode(), 'getCode');
         } else {
             if (!is_null($options->getNumberOfMessages()) && ($options->getNumberOfMessages() < 1 || $options->getNumberOfMessages() > 32)) {
                 $this->assertEquals(TestResources::STATUS_BAD_REQUEST, $e->getCode(), 'getCode');
             } else {
                 throw $e;
             }
         }
     }
     $this->restProxy->clearMessages($queue);
 }
 /**
  * @covers MicrosoftAzure\Storage\Queue\QueueRestProxy::createMessage
  * @covers MicrosoftAzure\Storage\Queue\QueueRestProxy::peekMessages
  */
 public function testPeekMessagesWithOptionsWorks()
 {
     // Arrange
     $year2010 = new \DateTime();
     $year2010->setDate(2010, 1, 1);
     // Act
     $this->restProxy->createMessage(self::$testQueueForMessages5, 'message1');
     $this->restProxy->createMessage(self::$testQueueForMessages5, 'message2');
     $this->restProxy->createMessage(self::$testQueueForMessages5, 'message3');
     $this->restProxy->createMessage(self::$testQueueForMessages5, 'message4');
     $opts = new PeekMessagesOptions();
     $opts->setNumberOfMessages(4);
     $result = $this->restProxy->peekMessages(self::$testQueueForMessages5, $opts);
     // Assert
     $this->assertNotNull($result, '$result');
     $this->assertEquals(4, count($result->getQueueMessages()), 'count($result->getQueueMessages())');
     for ($i = 0; $i < 4; $i++) {
         $entry = $result->getQueueMessages();
         $entry = $entry[$i];
         $this->assertNotNull($entry->getMessageId(), '$entry->getMessageId()');
         $this->assertNotNull($entry->getMessageText(), '$entry->getMessageText()');
         $this->assertEquals(0, $entry->getDequeueCount(), '$entry->getDequeueCount()');
         $this->assertNotNull($entry->getExpirationDate(), '$entry->getExpirationDate()');
         $this->assertTrue($year2010 < $entry->getExpirationDate(), '$year2010 < $entry->getExpirationDate()');
         $this->assertNotNull($entry->getInsertionDate(), '$entry->getInsertionDate()');
         $this->assertTrue($year2010 < $entry->getInsertionDate(), '$year2010 < $entry->getInsertionDate()');
     }
 }
 /**
  * Retrieves a message from the front of the queue, without changing 
  * the message visibility.
  * 
  * @param string              $queueName The queue name.
  * @param PeekMessagesOptions $options   The optional parameters.
  * 
  * @return MicrosoftAzure\Storage\Common\Models\PeekMessagesResult
  */
 public function peekMessages($queueName, $options = null)
 {
     Validate::isString($queueName, 'queueName');
     Validate::notNullOrEmpty($queueName, 'queueName');
     $method = Resources::HTTP_GET;
     $headers = array();
     $queryParams = array();
     $postParams = array();
     $path = $queueName . '/messages';
     $statusCode = Resources::STATUS_OK;
     if (is_null($options)) {
         $options = new PeekMessagesOptions();
     }
     $messagesCount = $options->getNumberOfMessages();
     $timeout = $options->getTimeout();
     $this->addOptionalQueryParam($queryParams, Resources::QP_PEEK_ONLY, 'true');
     $this->addOptionalQueryParam($queryParams, Resources::QP_TIMEOUT, $timeout);
     $this->addOptionalQueryParam($queryParams, Resources::QP_NUM_OF_MESSAGES, $messagesCount);
     $response = $this->send($method, $headers, $queryParams, $postParams, $path, $statusCode);
     $parsed = $this->dataSerializer->unserialize($response->getBody());
     return PeekMessagesResult::create($parsed);
 }
 /**
  * @covers MicrosoftAzure\Storage\Queue\QueueRestProxy::peekMessages
  */
 public function testPeekMessagesMultiplesMessages()
 {
     // Setup
     $name = 'peekmessagesmultiplesmessages';
     $this->createQueue($name);
     $expected1 = 'Message #1 Text';
     $expected2 = 'Message #2 Text';
     $expected3 = 'Message #3 Text';
     $this->restProxy->createMessage($name, $expected1);
     $this->restProxy->createMessage($name, $expected2);
     $this->restProxy->createMessage($name, $expected3);
     $options = new PeekMessagesOptions();
     $options->setNumberOfMessages(10);
     // Test
     $result = $this->restProxy->peekMessages($name, $options);
     // Assert
     $actual = $result->getQueueMessages();
     $this->assertCount(3, $actual);
     $this->assertEquals($expected1, $actual[0]->getMessageText());
     $this->assertEquals($expected2, $actual[1]->getMessageText());
     $this->assertEquals($expected3, $actual[2]->getMessageText());
 }