facile-it/mongodb-messenger-transport

基于 MongoDB 的 Symfony Messenger 传输,建立在 facile-it/mongodb-bundle 之上

1.4.0 2024-03-15 21:02 UTC

This package is auto-updated.

Last update: 2024-09-15 22:08:15 UTC


README

基于 MongoDB 的 Symfony Messenger 传输,建立在 facile-it/mongodb-bundle 之上

Latest Stable Version Total Downloads Latest Unstable Version License

CI Static analysis codecov

安装

  • 要安装此包,请使用 Composer
composer require facile-it/mongodb-messenger-transport

此包将在 Symfony 中注册为 bundle;如果您之前没有启用它,您需要启用它和 FacileMongoDbBundle。为此,您可以选择以下任一操作:

  • 如果正在使用 Flex,则让其自动启用
# config/bundles.php

# ...
    Facile\MongoDbBundle\FacileMongoDbBundle::class => ['all' => true],
+     Facile\MongoDbMessenger\FacileMongoDbMessengerBundle::class => ['all' => true],
];
  • 在自己的内核中手动启用
<?php

class Kernel extends BaseKernel
{
    public function registerBundles(): array
    {
        return [
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
            # ...
            new Facile\MongoDbBundle\FacileMongoDbBundle(),
+            new Facile\MongoDbMessenger\FacileMongoDbMessengerBundle(),
        ];
    }
}

配置

  1. 如果您还没有配置 MongoDB 连接,请按照 facile-it/mongodb-bundle 中的说明进行配置
  2. 使用该配置中的连接名称(例如,Flex 脚本中的 default),按如下方式配置 Messenger 的新传输
# config/packages/messenger.yaml
framework:
  messenger:
    transports:
      new_transport: 'facile-it-mongodb://default'

注意:在首次使用或调用 messenger:setup-transports 控制台命令时,此传输会创建一个具有索引的集合,以优化性能,因为它针对用于检索消息的属性进行了定制。

建议

建议将此传输用于失败的消息,如 Doctrine 传输;如果您想这样做,可以这样做:

framework:
  messenger:
    failure_transport: new_transport
    transports:
      new_transport: 'facile-it-mongodb://default'

如果您配置此传输多次,请记住使用 queue_name 和/或 collection_name 选项(见下文)来区分消息。

完整配置参考

此传输,与其他默认传输一样,提供了一系列选项,这些选项可以作为 DSN 中的查询字符串或以下数组传递,如下例所示(所有值都是提供的默认值)

framework:
  messenger:
    transports:
      new_transport: 
        dsn: 'facile-it-mongodb://default'
        options:
          collection_name: 'messenger_messages'
          queue_name: 'default'
          redeliver_timeout: 3600
          document_enhancers: [] 

redeliver_timeout 选项

redeliver_timeoutDoctrineTransport 的工作方式相同:当消息被投递但未 ackreject(可能是由于致命崩溃或网络故障)时,它作为超时工作,并在超时后使消息可重新投递。

document_enhancers 选项

document_enhancers 选项是此传输的扩展点;它接受一个字符串数组,每个字符串都是一个完整的类名或带有 @ 前缀的服务名。

它允许最终用户向将持久化每个消息的文档添加字段。每个增强器必须实现 Facile\MongoDbMessenger\Extension\DocumentEnhancer 接口,该接口要求实现一个 enhance(BSONDocument $document, Envelope $envelope): void 方法。BSONDocument 将随后持久化,并且可以通过添加额外的属性来丰富它,这对于使用例如 MongoDbTransport::find 搜索和索引特定信息非常有用。

您可以查看提供的 \Facile\MongoDbMessenger\Extension\DocumentEnhancer\LastErrorMessageEnhancer 作为示例,或像这样使用它:

framework:
  messenger:
    transports:
      new_transport: 
        dsn: 'facile-it-mongodb://default'
        options:
          document_enhancers:
          - 'Facile\MongoDbMessenger\Extension\DocumentEnhancer\LastErrorMessageEnhancer'
          # or
          - '@my_document_enhancer'

services:
  my_document_enhancer:
    class: App\My\Class # which implements the DocumentEnhancer interface
    arguments:
    - '...'