almt-labs / pulsar-messenger-bundle
Symfony Messenger transport for Apache Pulsar
Package info
github.com/almt-labs/pulsar-messenger-bundle
Type:symfony-bundle
pkg:composer/almt-labs/pulsar-messenger-bundle
Requires
- php: >=8.2.35
- ext-bcmath: *
- ikilobyte/pulsar-client-php: 1.4.10
- psr/container: ^1.1|^2.0
- psr/event-dispatcher: ^1.0
- psr/log: ^1|^2|^3
- symfony/config: 6.4.*
- symfony/console: 6.4.*
- symfony/dependency-injection: 6.4.*
- symfony/event-dispatcher: 6.4.*
- symfony/http-kernel: 6.4.*
- symfony/messenger: 6.4.*
Requires (Dev)
- php-cs-fixer/shim: ^3.95
- phpstan/phpstan: ^2.3
- phpstan/phpstan-symfony: ^2.0
- phpunit/phpunit: ^11.5
- symfony/dotenv: 6.4.*
- symfony/framework-bundle: 6.4.*
- symfony/monolog-bundle: ^3.11
- symfony/property-access: 6.4.*
- symfony/runtime: 6.4.*
- symfony/serializer: 6.4.*
- symfony/yaml: 6.4.*
Suggests
- ext-zlib: Required to use the ZLIB compression_type
- ext-zstd: Required to use the ZSTD compression_type
- symfony/property-access: Required by the symfony/serializer ObjectNormalizer to (de)normalize the messages and the stamps
- symfony/serializer: To use messenger.transport.symfony_serializer (JSON) as the transport serializer
Provides
None
Conflicts
None
Replaces
None
README
A Symfony Messenger transport for
Apache Pulsar, built on the
ikilobyte/pulsar-client-php client.
- Send and consume through Messenger with a
pulsar://orpulsar+ssl://DSN. - A transport can be producer-only, consumer-only, or both.
- Failed messages are retried through a retry topic with Pulsar's delayed delivery, instead of a negative acknowledgement.
- Works as a Messenger failure transport (
messenger:failed:show,messenger:failed:retry). - Message keys and sequence IDs (deduplication) through stamps.
- JWT authentication, TLS and proxy connections.
Requirements
- PHP 8.2.35 or later
- Symfony 6.4 (Messenger, Config, Console)
ext-bcmath: the Pulsar client encodes its protocol commands withprotobuf-php/protobuf, which needs bcmath (or gmp) to encode negative integers. Without either extension, it throwsNegative integers are only supported with GMP or BC (64bit) intextensions.ext-zliborext-zstd, only to use theZLIBorZSTDcompression_type- Apache Pulsar. CI tests the bundle against Pulsar 3.3 and 4.0.
Installation
composer require almt-labs/pulsar-messenger-bundle
The bundle registers the transport factory and the retry listener by itself: there's nothing to add to
services.yaml.
Configuration
A transport uses the bundle when its DSN starts with pulsar:// or pulsar+ssl://. The DSN is the broker URL,
for example pulsar://localhost:6650. Everything else goes under the transport's options.
# .env MESSENGER_TRANSPORT_DSN=pulsar://localhost:6650 MESSENGER_TOKEN=<your JWT>
# config/packages/messenger.yaml framework: messenger: transports: orders: dsn: "%env(MESSENGER_TRANSPORT_DSN)%" options: topic: "persistent://my-tenant/my-namespace/orders" connection: jwt_token: "%env(MESSENGER_TOKEN)%" producer: ~ consumer: subscription: name: "my-app" type: "Shared" routing: 'App\Message\PlaceOrder': orders
produceris required to send, andconsumer(withsubscription.name) to consume. Each side only reads its own section, so a transport with only aproducersection is a valid send-only transport, and the other way round.- The topic can be set once at the root (
topic), for both sides, or inproducer.topicandconsumer.topic/consumer.topics, but not in both places. - Nothing connects to Pulsar when the container is built. Each side connects on its first use, so a broker that is
down or an invalid option fails inside
send()or the worker, as a MessengerTransportException, and is logged on themessengerMonolog channel, with the rest of Messenger's logs.
Options reference
The options are validated: an unknown key, a wrong type or a value outside the allowed ones fails with a message that names the option. To print every option with its default value and description:
php bin/console almt-labs:pulsar:config-reference
| Option | Default | Description |
|---|---|---|
topic |
Topic of both sides | |
connection.connect_timeout |
10 |
In seconds |
connection.jwt_token |
JWT used to authenticate | |
connection.proxy_url |
For example pulsar://proxy:6650 |
|
connection.tls.cert / key / cacert |
Paths to the client certificate, its key and the CA certificates. Requires a pulsar+ssl:// DSN, and the files must exist |
|
connection.tls.verify_peer / verify_host |
true |
|
producer.name |
Must be unique per topic: leave it unset when several processes send to the topic | |
producer.topic |
||
producer.compression_type |
NONE |
NONE, ZLIB or ZSTD |
producer.initial_subscription_name |
Creates this subscription with the producer, so no message is lost before a consumer subscribes | |
consumer.name |
||
consumer.topic / consumer.topics |
One topic, or a list | |
consumer.subscription.name |
Required | |
consumer.subscription.type |
Shared |
Exclusive, Shared, Failover or Key_Shared |
consumer.subscription.initial_position |
Latest |
Earliest or Latest |
consumer.receive_queue_size |
1000 |
|
consumer.reconnect_policy.status |
false |
Reconnect the consumer when the connection drops |
consumer.reconnect_policy.interval |
3 |
In seconds |
consumer.reconnect_policy.limit |
5 |
|
consumer.retry_topic_mode |
derived |
See Retries |
consumer.retry_producer.* |
name, compression_type and initial_subscription_name of the producers that send retries |
Sending
Messages are sent with Messenger as usual. The serializer's body becomes the Pulsar payload, and its headers
become the message properties. The returned envelope has a TransportMessageIdStamp with the Pulsar message ID.
Three stamps change how a message is sent:
use AlmtLabs\MessengerPulsarBundle\Transport\Stamp\MessageKeyStamp; use AlmtLabs\MessengerPulsarBundle\Transport\Stamp\MessageSequenceIdStamp; use Symfony\Component\Messenger\Stamp\DelayStamp; $bus->dispatch(new PlaceOrder($orderId), [ new MessageKeyStamp('order-'.$orderId), new MessageSequenceIdStamp($sequenceId), new DelayStamp(30_000), ]);
MessageKeyStampsets the message key. Pulsar uses it to route messages with the same key to the same partition, to deliver them to the same consumer on aKey_Sharedsubscription, and for topic compaction.MessageSequenceIdStampsets the sequence ID, which the broker uses to discard duplicates when deduplication is enabled on the namespace or topic. It is scoped to the producer, so give the producer a fixedname.DelayStampuses Pulsar's delayed delivery. The delay is rounded up to whole seconds, and Pulsar applies it only toSharedsubscriptions: other subscription types receive the message immediately.
Consuming
php bin/console messenger:consume orders
The worker acknowledges a message once it is handled. If the worker stops or loses its connection before that, the broker delivers the message again, so handlers should be idempotent (at-least-once delivery).
Retries
When a handler fails and Messenger's retry_strategy allows another attempt, the bundle doesn't send a negative
acknowledgement. It acknowledges the message and publishes a copy to a retry topic, with the delay from the retry
strategy as Pulsar's delayed delivery. consumer.retry_topic_mode chooses the retry topic:
derived(the default):<topic>-<subscription>-retry, so each subscription retries independently. The consumer subscribes to its topics and to their retry topics by itself. A message that fails on a retry topic stays on it.same: the original topic. Every subscription of the topic receives the retry, so use it only when the transport is the topic's only subscription, as with a failure transport.
Use a Shared subscription for retries with a delay: with the other types, Pulsar delivers the retry immediately.
The retry producers use the connection section and the optional consumer.retry_producer section, not
producer, so a transport that sends and consumes can configure each side separately. Leave retry_producer.name
unset when several workers consume the transport, since they all send to the same retry topic and Pulsar requires
producer names to be unique per topic.
Failure transport
A Pulsar transport can be Messenger's failure transport:
framework: messenger: failure_transport: failed transports: failed: dsn: "%env(MESSENGER_TRANSPORT_DSN)%" options: topic: "persistent://my-tenant/my-namespace/failed" connection: jwt_token: "%env(MESSENGER_TOKEN)%" producer: ~ consumer: retry_topic_mode: same subscription: name: "my-app" type: "Exclusive" initial_position: "Earliest"
It uses retry_topic_mode: same because the failure topic doesn't have the problem derived solves. derived is
for topics that several subscriptions read, typically several applications: each subscription retries its own
failed messages on its own retry topic, so a retry doesn't reach the other subscriptions, which already handled the
message. The failure topic belongs to one application and has a single Exclusive subscription, so no other
subscription can receive its retries.
messenger:failed:show, messenger:failed:retry and messenger:failed:remove work with it. To list the messages,
the transport receives every waiting message without acknowledging it, and the broker delivers the messages again
once the command ends. getMessageCount() works the same way, so it is slow on a large backlog.
Serializer
The transport works with any Messenger serializer. If you set the Symfony Serializer (JSON) as the transport serializer, install its packages too:
# config/packages/messenger.yaml framework: messenger: serializer: default_serializer: messenger.transport.symfony_serializer
composer require symfony/serializer symfony/property-access
symfony/serializerprovides theserializerservice used bymessenger.transport.symfony_serializer. Without it, the container fails withThe service "messenger.transport.symfony_serializer" has a dependency on a non-existent service "serializer".symfony/property-accessenables theObjectNormalizer, which (de)normalizes the messages and the stamps. Without it, sending fails withCould not normalize object of type "Symfony\Component\Messenger\Stamp\BusNameStamp", no supporting normalizer found.
Contributing
Issues and pull requests are welcome. CONTRIBUTING.md explains how to set up the project with Docker, run the tests against a local Pulsar broker, and try the transport in the test application.
License
MIT, see LICENSE. The bundle includes adapted code from Symfony Messenger and from the Pulsar PHP client: see THIRD_PARTY_NOTICES.md.