psyllium/amqp 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

psyllium/amqp

最新稳定版本:7.0

Composer 安装命令:

composer require psyllium/amqp

包简介

Symfony psyllium adapter for amqp

README 文档

README

Getting Started

This package is a part of Psyllium library. It provides a simple way to use RabbitMQ in your application. Allows you to define listeners and message producers in the queue system.

Install

To install the package you will need to be using Composer in your project. To install it please see the docs.

composer require psyllium/amqp

Enable the Bundle

Enable it by adding it to the list of registered bundles in the Kernel.php file of your project:

config/bundles.php

return [
    Psyllium\Amqp\PsylliumAmqp::class => ['all' => true]
];

config/packages/psyllium_amqp.yaml

psyllium:
  rabbitmq:
    hostname: '%hostname%'
    port: '%port%'
    user: '%user%'
    pass: '%password%'

Configure

In this way, you can create producers and listeners who will use the connection to the queuing system generated by the bundle.

config/services.yaml

psyllium.rabbitmq.listener:
  class: 'Psyllium\Amqp\Infrastructure\RabbitMQ\Listener'
  factory: ['@Psyllium\Amqp\Infrastructure\RabbitMQ\ListenerFactory', 'create']
  arguments:
    - '%queue_name%'

config/services.yaml

psyllium.rabbitmq.producer:
  class: 'Psyllium\Amqp\Infrastructure\RabbitMQ\Producer'
  factory: [ '@Psyllium\Amqp\Infrastructure\RabbitMQ\ProducerFactory', 'create' ]
  arguments:
    - '%exchange_name%'

Both configurations ( producers and consumers ) allow the definition of middlewares that can be used to transform messages as sequential steps performed in the order defined before the final delivery / publication of the message. Example:

psyllium.rabbitmq.listener:
  class: 'Psyllium\Amqp\Infrastructure\RabbitMQ\Listener'
  factory: ['@Psyllium\Amqp\Infrastructure\RabbitMQ\ListenerFactory', 'create']
  arguments:
    - '%queue_name%'
    - ['@Psyllium\Amqp\Domain\Middleware\GzUncompress', '@Psyllium\Amqp\Domain\Middleware\Timestamp']

How to use in code?

Listener:

use Psyllium\Amqp\Domain\ReadModel\ReadInterface;

$this->listener->listen(
    function (ReadInterface $message): void {
        $message->getBody()
        // do something with message
        $message->ack();
    }
);

Producer:

Definde class that implements EventInterface

use Psyllium\Amqp\Domain\EventModel\EventInterface;

class Example implements EventInterface

Send message to queue

$this->producer->produce(
    new Example()
);

Middlewares

Definde class that implements MiddlewareInterface

use Psyllium\Amqp\Domain\Middleware\MiddlewareInterface;

class Example implements MiddlewareInterface

The system also has 3 pre-defined middlewares to compress, decompress and timestamp messages.

@Psyllium\Amqp\Domain\Middleware\GzUncompress

Decompression of messages using the gzuncompress function.

@Psyllium\Amqp\Domain\Middleware\GzCompress

Compression of messages using the gzcompress function. Allows the specification of the compression level ( from -1 to 9, default: 9 ). Example:

psyllium.rabbitmq.middleware.gzcompress:
  class: 'Psyllium\Amqp\Domain\Middleware\GzCompress'
  arguments:
    $compressionLevel: 9
@Psyllium\Amqp\Domain\Middleware\Timestamp

Add a timestamp in the message header. Requires to define the name of the parameter. Example:

psyllium.rabbitmq.middleware.timestamp:
  class: 'Psyllium\Amqp\Domain\Middleware\Timestamp'
  arguments:
    $key: 'default-name'

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2024-03-18