示例#1
0
<?php

include __DIR__ . './../vendor/autoload.php';
use Kemer\Amqp;
$broker = new Amqp\Broker("rabbit.docker", 5672, 'guest', 'guest');
$dispatcher = new Amqp\Dispatcher();
$dispatcher->addSubscriber(new Amqp\Facade\PostponeSubscriber($broker));
$dispatcher->addSubscriber(new Amqp\Facade\DeadLetterSubscriber($broker));
$dispatcher->addListener("#", new Amqp\Publisher\QueuePublisher($broker), 1000);
$dispatcher->addListener("#", new Amqp\Publisher\ExchangePublisher($broker), 1001);
// Add event listeners
$dispatcher->addListener('kernel.message', [new App\Listener(), "onMessage"]);
$dispatcher->addListener("kernel.*", [new App\Listener(), "onKernel"]);
$dispatcher->addListener('kernel.#', [new App\Listener(), "onAll"]);
// Add event subscriber
$dispatcher->addSubscriber(new App\Subscriber());
// Send discover request
$dispatcher->dispatch("some-kernel", new Amqp\PublishEvent());
$consumer = new Amqp\Consumer($dispatcher);
// Listen on named queue
$consumer->listen($broker->declareQueue(AMQP_DURABLE, "error-queue"));
// Listen on existing shared queue (load balanced)
$consumer->listen($broker->declareQueue(AMQP_PASSIVE, "existing-queue"));
示例#2
0
<?php

include __DIR__ . './../vendor/autoload.php';
use Kemer\Amqp;
use Kemer\Amqp\Addons as AmqpAddons;
use Symfony\Component\EventDispatcher\GenericEvent;
$broker = new Amqp\Broker(getenv("AMQP_HOST") ?: "rabbit.docker", getenv("AMQP_PORT") ?: 5672, getenv("AMQP_LOGIN") ?: 'guest', getenv("AMQP_PASSWORD") ?: 'guest');
$dispatcher = new Amqp\Dispatcher();
$dispatcher->addSubscriber(new AmqpAddons\PostponeSubscriber($broker));
$dispatcher->addSubscriber(new AmqpAddons\DeadLetterSubscriber($broker));
// Add event listeners
$dispatcher->addListener('kernel.waits', [new App\Listener(), "onMessage"]);
$dispatcher->addListener("kernel.*", [new App\Listener(), "onKernel"]);
$dispatcher->addListener('#', [new App\Listener(), "onAll"]);
// Add event subscriber
$dispatcher->addSubscriber(new App\Subscriber());
// dispatch local messages
// $dispatcher->dispatch("some.kernel", new Amqp\PublishEvent());
$consumer = new Amqp\Consumer($dispatcher);
/**
 * Listen on shared queue (load balanced)
 *
 * @throws AMQPQueueException if queue does not exists
 */
//$queue = $broker->declareQueue(AMQP_PASSIVE, "some-queue");
$queue = $broker->queue(AMQP_DURABLE, "some-queue");
$queue->setArgument("x-dead-letter-exchange", "some-exchange-dlx");
$queue->declareQueue();
// $consumer->listen($queue);
// Listen on temporary queue - and bind it to exchange
//$queue = $broker->declareQueue(AMQP_AUTODELETE);