PHP code example of graze / queue

1. Go to this page and download the library: Download graze/queue 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/ */

    

graze / queue example snippets


use Aws\Sqs\SqsClient;
use Graze\Queue\Adapter\SqsAdapter;
use Graze\Queue\Client;
use Graze\Queue\Message\MessageInterface;

$client = new Client(new SqsAdapter(new SqsClient([
    'region'  => 'us-east-1',
    'version' => '2012-11-05',
    'credentials' => [
        'key'    => 'ive_got_the_key',
        'secret' => 'ive_got_the_secret'
    ],
]), 'queue_name'));

// Producer
$client->send([
    $client->create('foo'),
]);

// Consumer
$client->receive(function (MessageInterface $msg) {
    var_dump($msg->getBody());
    var_dump($msg->getMetadata()->getAll());
});

use Graze\Queue\Client;
use Graze\Queue\Adapter\ArrayAdapter;
use Graze\Queue\Handler\BatchAcknowledgementHandler;
use Graze\Queue\Message\MessageInterface;

// Create client with the Batch Acknowledgement Handler.
$client = new Client(new ArrayAdapter(), [
    'handler' => new BatchAcknowledgementHandler(),
]);

// Receive a maximum of 10 messages.
$client->receive(function (MessageInterface $message) {
    // Do some work.
}, 10);

use Graze\Queue\Client;
use Graze\Queue\Adapter\ArrayAdapter;
use Graze\Queue\Handler\BatchAcknowledgementHandler;
use Graze\Queue\Message\MessageInterface;

// Create client with the Batch Acknowledgement Handler.
$client = new Client(new ArrayAdapter(), [
    'handler' => new BatchAcknowledgeHandler(100), // Acknowledge after 100 messages.
]);

// Poll until `$done()` is called.
$client->receive(function (MessageInterface $message, Closure $done) {
    // Do some work.

    // You should always define a break condition (i.e. timeout, expired session, etc).
    if ($breakCondition) $done();
}, null);