dspacelabs/queue
最新稳定版本:v0.2.0
Composer 安装命令:
composer require dspacelabs/queue
包简介
General abstract queue library for PHP, has ability to support various different queue systems
关键字:
README 文档
README
General queue library for PHP, ability to support various different queue systems.
For more documentation, see the wiki.
Installation
composer require dspacelabs/queue
Usage
<?php use Dspacelabs\Component\Queue\Message; // Publishing messages to a queue $message = new Message($body); $queue->publish($message); /** * This will publish a message to the queue you created, the $body can be * anything you want. */ // Receive messages $message = $queue->receive(); $body = $message->getBody(); // ... Process Data ... /** * $message will be the message that was published. `->receive()` can be put * into a foreach loop if you want to continue to process the queue until * all the messages are processed, use a for loop in you only want to process * a small number of the messages */ /** * Once you are done processing a message, it needs to be deleted from the queue */ $queue->delete($message);
Messages, Queues, Broker
Messages are published to queues. When you receive a message from a queue, you will be interacting with this class.
Queues are where you publish your messages to. For example, a Queue could be an AWS SQS, RabbitMQ, or any other queue you can think of.
The Broker helps you keep track of queues. So instead of having 100 different queue objects all over, you just add all those to the Broker and let the Broker sort them out. You just get the ones you need.
Using the FileQueue
The FileQueue will store messages on disk and is good to use for local development.
Messages are stored on disk in the file naming format "name.timestamp.message" so you can have multiple file queues share the same directory.
<?php use Dspacelabs\Component\Queue\FileQueue; use Dspacelabs\Component\Queue\Message; $queue = new FileQueue('queue.name', '/tmp/'); $queue->publish(new Message('Hello World!')); // ... $message = $queue->receive(); $body = $message->getBody(); // $body === "Hello World!" $queue->delete($message);
Using the SqsQueue
Requires Amazon PHP SDK.
php composer.phar require aws/aws-sdk-php
<?php use Aws\Credentials\Credentials; use Aws\Sqs\SqsClient; use Dspacelabs\Component\Queue\SqsQueue; $credentials = new Credentials($accessKey, $secretKey); $client = new SqsClient([ 'version' => 'latest', 'region' => 'us-east-1', 'credentials' => $credentials, ]); $queue = new SqsQueue($client, $queueUrl, $name);
Using the StandardQueue
The standard queue is mainly used for testing. Once this is setup you can quickly test your workflow. Keep in mind that this has some drawbacks mainly that the messages are not persisted.
<?php // First you need to setup the Queue $queue = new \Dspacelabs\Component\Queue\StandardQueue('queue.name'); // Create a message that will be sent to the queue $message = new \Dspacelabs\Component\Queue\Message('Hello World A'); // Publish the message $queue->publish($message); // Consume all messages /** @var Message $msg **/ while ($msg = $queue->receive()) { // process message // ... // Delete the Message from the queu $queue->delete($msg); }
NOTE: When using the StandardQueue, you do not need to delete the message like
in this example $queue->delete($msg); HOWEVER there are some queues out there
that support this.
Using the RedisQueue
To use the RedisQueue you need to install Predis
composer require predis/predis
Once you have done that, you can begin to use the Redis as one of the possible Queues.
<?php use Predis\Client; use Dspacelabs\Component\Queue\RedisQueue; $client = new Client(); $queue = new RedisQueue($client, 'queue.name');
See https://github.com/nrk/predis for Predis documentation.
Using the Broker
If you have multiple queues, you can use the Broker which will just help you manage the various queues you have. For example, you could be using multiple SQS queues and want a single point to access those at. The Broker will help you with this.
It's also important to point out that the broker supports all queue types in this library. So you can use the SQS Queue, Standard Queue, or a custom queue that you made.
<?php use Dspacelabs\Component\Queue\Broker; $broker = new Broker(); // I assume you already have a queue $broker->addQueue($queue); // `queue.name` is the name given to the queue you created // I assume you already have a `$message` created $broker->get('queue.name')->publish($message); $broker->get('queue.other')->publish($messageOther);
Change Log
See CHANGELOG.md.
License
Copyright (c) 2015-2017 dSpace Labs LLC
See LICENSE for full license.
dspacelabs/queue 适用场景与选型建议
dspacelabs/queue 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10.44k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2015 年 06 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「message queue」 「aws sqs」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dspacelabs/queue 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dspacelabs/queue 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dspacelabs/queue 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP AMQP Binding Library
This package includes a script and fail2ban configuration that allows you to use fail2ban when utilizing AWS elastic load balancer (ELB) and an apache webserver.
A Laravel package to monitor queue jobs.
Elastic Driver for Laravel Scout
simple api library.
A replica of the AWS Elastic Beanstalk worker SQS daemon (sqsd) in PHP
统计信息
- 总下载量: 10.44k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-06-23