承接 graphaware/php-simplemq 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

graphaware/php-simplemq

最新稳定版本:1.0.4

Composer 安装命令:

composer require graphaware/php-simplemq

包简介

Github Events API Sync module

README 文档

README

GraphAware's PHP Simple Message Queue for RabbitMQ

RabbitMQ's Rapid Application Development.

This library makes possible to create channels, queues, bindings, producers, consumers, .. on the fly by only providing a simple YAML configuration file.

Build Status

Usage

Require the library dependency :

composer require graphaware/php-simplemq

Define the connections, exchanges, producers and consumers, eg:

simple_mq:
  connections:
    default:
      host: 192.168.59.103
      port: 5672
      user: admin
      password: error
      vhost: "/"

  exchanges:
    logs:
      connection: default
      type: fanout
      durable: true

    error-logs:
      connection: default
      type: direct
      durable: true

  producers:
    logs:
      exchange: logs

    errors:
      exchange: error-logs
      routing_key: error

  consumers:
    logs-printer:
      exchange: logs
      ack: true
      queue:
        name: my-app-all-logs
        durable: true
        qos:
          prefetch_count: 1

    error-logs-recorder:
      exchange: error-logs
      queue:
        name: my-app-error-logs
        durable: true
        qos:
          prefetch_size: 1
        bindings:
          -
            queue: my-app-error-logs
            routing_key: error

Bootstrap the library by providing your configuration file location :

require_once(__DIR__.'/vendor/autoload.php');

use GraphAware\SimpleMQ\SimpleMQ;

$smq = SimpleMQ::withYAMLConfigFile(__DIR__.'/path_to_your_config_file.yml');

Based on the example configuration, producers named logs and errors as well as consumers named logs-printer and error-logs-recorder are available through the library.

To retrieve and start consuming queues, you can get the consumer with the following method :

$consumer = $smq->getConsumer('logs-printer');

$callback = function($message) {
    print_r($message->body);
};

$consumer->consume($callback);

Getting a single message :

$message = $consumer->getMessage();
// Returns a AMQPMessage instance

Getting more than one message :

$messages = $consumer->getMessageBatch(10);
// Returns an array of AMQPMessage

Sometimes, there can be a latency between a message is sent and this message to be seen by the producers (for eg in CI suites).

You can define a maxAttempts to reach the batchSize before stopping reading queues :

$messages = $consumer->getMessageBatch(10, 20);

And to start sending messages to exchanges, it is pretty much the same :

$producer = $smq->getProducer('errors');
$message = json_encode(array('id' => 1234, 'text' => 'Hello world'));

$producer->sendMessage($message);

The producer and consumers knows exactly, based on the configuration, which routing key to use for direct and topic exchanges and also which binding keys to use for binding queues to exchanges.

License: MIT

Author: Christophe Willemsen

统计信息

  • 总下载量: 54
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 21
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2015-07-20