PHP code example of ssigwart / aws-high-availability-sqs

1. Go to this page and download the library: Download ssigwart/aws-high-availability-sqs library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ssigwart / aws-high-availability-sqs example snippets


// Set up SQS queues
$primaryQueue = new SqsAvailableQueue('us-east-1', 'https://sqs.us-east-1.amazonaws.com/123456789012/queue_in_us_east_1');
$backupQueue = new SqsAvailableQueue('us-east-2', 'https://sqs.us-east-2.amazonaws.com/123456789012/queue_in_us_east_2');
$availableQueues = new SqsAvailableQueues($primaryQueue);
$availableQueues->addAvailableQueue($backupQueue);

// Set up S3 locations
$primaryLocation = new S3FileBucketAndKey('us-east-1', 'phpunit-test-us-east-1', 'us-east-1/path/to/dir/');
$backupLocation = new S3FileBucketAndKey('us-east-2', 'phpunit-test-us-east-2', 'us-east-2/path/to/dir/');
$s3Locations = new S3AvailableUploadFileBucketAndKeyLocations($primaryLocation);
$s3Locations->addAlternativeLocation($backupLocation);

// Send messages
$sqsSender = new AwsHighAvailabilitySqsSender($awsSdk);
$result = $sqsSender->sendMessageWithS3LargeMessageBacking($availableQueues, $s3Locations, $queueMsgBody, null);
print 'Selected Queue: ' . $result->getSelectedQueue()->getQueueUrl() . PHP_EOL;
print 'Message ID: ' . $result->getSqsMessageId() . PHP_EOL;

$primaryQueue = new SqsAvailableQueue('us-east-1', 'https://sqs.us-east-1.amazonaws.com/123456789012/queue_in_us_east_1');

// Set up metadata
$metadata = new SqsMessageReceivingMetadata();
$metadata->setMaxNumMessages(7);
$metadata->setVisibilityTimeout(300);
$metadata->setWaitTime(15);

$sqsReceiver = new AwsHighAvailabilitySqsReceiver($awsSdk);
$sqsMessagesResult = $sqsReceiver->receivedMessagesWithS3LargeMessageBacking($primaryQueue, $metadata);
print 'Number of Messages: ' . $sqsMessagesResult->getNumMessages() . PHP_EOL;
foreach ($sqsMessagesResult->getSqsMessages() as $sqsMessage)
{
	print 'Messages ID: ' . $sqsMessage->getMessageId() . PHP_EOL;
	print 'Receipt Handle: ' . $sqsMessage->getReceiptHandle() . PHP_EOL;
	print 'Message Body: ' . $sqsMessage->getMessage() . PHP_EOL;
}

$primaryQueue = new SqsAvailableQueue('us-east-1', 'https://sqs.us-east-1.amazonaws.com/123456789012/queue_in_us_east_1');
$sqsReceiver = new AwsHighAvailabilitySqsReceiver($awsSdk);
try {
	$sqsReceiver->deleteMessage($primaryQueue, $sqsMessage);
} catch (AwsHighAvailabilitySqsDeleteS3FileDeletionFailureException $e) {
	error_log($e->getMessage());
} catch (AwsHighAvailabilitySqsDeleteException $e) {
	// Handle error. Possibly try to delete it again or add application code to prevent the message from being processed again later.
}