PHP code example of h4kuna / queue

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

    

h4kuna / queue example snippets


use h4kuna\Queue;

/** @var Queue\QueueFactory $queue */
$queueFactory = new Queue\QueueFactory();

/** @var Queue\Queue $queue */
$queue = $queueFactory->create('my-queue');

$queue->producer()->send('Hello');

$queue->consumer()->receive()->message === 'Hello'

$queue->producer()->send('Hello', 2);
$queue->consumer()->tryReceive() === NULL // non blocking read
$queue->consumer()->receive(2)->message === 'Hello'

$queue->producer()->send('Hello', 2);
$queue->consumer()->receive(Queue\Config::TYPE_ALL)->message === 'Hello'

use h4kuna\Queue;

/** @var Queue\QueueFactory $queue */
$queueFactory = new Queue\QueueFactory();

/** @var Queue\Queue $queue */
$queue = $queueFactory->create('my-queue');

$queue->restore(); // restore from backup, after restart
do {
    try {
        $message = $queue->consumer()->receive();
        // ... 
    } catch (Queue\ReceiveException $e) {
        // log error
        // stop process if code is 22
    }
} while(true);