tricae / queue
该软件包已被弃用,不再维护。没有建议替代软件包。
处理队列的PHP库(RabbitMQ、内存队列等)
v0.2.1
2015-11-28 00:15 UTC
Requires
- php: >=5.4.0
- videlalvaro/php-amqplib: 2.4.*
Requires (Dev)
- phpmd/phpmd: @stable
- phpunit/phpunit: 4.2.*
- squizlabs/php_codesniffer: @stable
This package is not auto-updated.
Last update: 2018-12-27 17:57:36 UTC
README
这是一个用于处理队列的PHP库,旨在保持发布和消费消息的简单性。
设置
$ git clone git@github.com:tricae-br/queue.git
$ cd queue/
$ composer install
测试
您必须安装RabbitMQ才能运行集成测试。
$ phpunit -c phpunit.xml.dist
准备环境
// bootstrap.php require_once __DIR__ . '/vendor/autoload.php'; $configuration = new \Queue\Configuration(\Queue\Driver::AMQP, '127.0.0.1', 5672, 'guest', 'guest'); $connection = \Queue\DriverManager::getConnection($configuration); $queue = $connection->getDriver()->createQueue('logs.error'); $exchange = $connection->getDriver()->createExchange('logs.error', \Queue\Resources\Exchange::TYPE_DIRECT); $exchange->addBinding($queue->getName(), 'error'); $connection->createQueue($queue); $connection->createExchange($exchange); $connection->bind($queue, $exchange, 'error');
如何发布消息
// publisher.php require_once __DIR__ . '/bootstrap.php'; class DummyProducer extends \Queue\Producer { } $producer = new DummyProducer($connection, $exchange); if (empty($argv[1])) { throw new InvalidArgumentException('message not found to publish'); } $message = $producer->prepare($argv[1]); $producer->publish($message, 'error'); $connection->close();
如何消费消息
// consumer.php require_once __DIR__ . '/bootstrap.php'; class EchoConsumer extends \Queue\Consumer { public function process(\Queue\Resources\MessageInterface $message) { echo $message->getBody() . PHP_EOL; $message->setAck(); } } $consumer = new EchoConsumer($connection, $queue); $consumer->consume(); $connection->close();