PHP code example of mucy / webman-rabbitmq

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

    

mucy / webman-rabbitmq example snippets



return [
    'enable' => true,

    'host'               => '127.0.0.1',
    'vhost'              => '/',
    'port'               => 5672,
    'username'           => 'guest',
    'password'           => 'guest',
    'mechanism'          => 'AMQPLAIN', # 阿里云等云服务使用 PLAIN
    'timeout'            => 10,
    'heartbeat'          => 50,
    'heartbeat_callback' => function(){ # 心跳回调
    },
    'error_callback'     => function(Throwable $throwable){ # 异常回调
    }
];


declare(strict_types=1);

namespace process\workbunny\rabbitmq;

use Bunny\Channel as BunnyChannel;
use Bunny\Async\Client as BunnyClient;
use Bunny\Message as BunnyMessage;
use Workbunny\WebmanRabbitMQ\Constants;
use Workbunny\WebmanRabbitMQ\FastBuilder;

class TestBuilder extends FastBuilder
{
   	// QOS 大小
   	protected int $prefetch_size = 0;
   	// QOS 数量
   	protected int $prefetch_count = 0;
   	// QOS 是否全局
   	protected bool $is_global = false;
   	// 是否延迟队列
   	protected bool $delayed = false;
   	// 消费回调
   	public function handler(BunnyMessage $message, BunnyChannel $channel, BunnyClient $client): string
   	{
       	// TODO 消费需要的回调逻辑
       	var_dump('请重写 TestBuilderDelayed::handler() ');
       	return Constants::ACK;
       	# Constants::NACK
       	# Constants::REQUEUE
   	}
}

use function Workbunny\WebmanRabbitMQ\sync_publish;
use process\workbunny\rabbitmq\TestBuilder;

sync_publish(TestBuilder::instance(), 'abc'); # return bool

use function Workbunny\WebmanRabbitMQ\sync_publish;
use process\workbunny\rabbitmq\TestBuilder;

sync_publish(TestBuilder::instance(), 'abc', [
	'x-delay' => 10000, # 延迟10秒
]); # return bool

use function Workbunny\WebmanRabbitMQ\async_publish;
use process\workbunny\rabbitmq\TestBuilder;

async_publish(TestBuilder::instance(), 'abc'); # return PromiseInterface|bool

use function Workbunny\WebmanRabbitMQ\async_publish;
use process\workbunny\rabbitmq\TestBuilder;

async_publish(TestBuilder::instance(), 'abc', [
	'x-delay' => 10000, # 延迟10秒
]); # return PromiseInterface|bool
shell
# 在 process/workbunny/rabbitmq/project 目录下创建 TestBuilder.php
./webman workbunny:rabbitmq-builder project/test 1

# 延迟同理