Download the PHP package djereg/symfony-rabbitmq without Composer
On this page you can find all versions of the php package djereg/symfony-rabbitmq. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download djereg/symfony-rabbitmq
More information about djereg/symfony-rabbitmq
Files in djereg/symfony-rabbitmq
Package symfony-rabbitmq
Short Description Integrates php-amqplib with Symfony Messenger & RabbitMq.
License MIT
Informations about the package symfony-rabbitmq
Symfony RabbitMQ
THIS PACKAGE IS PRIMARILY INTENDED FOR INTERNAL/PRIVATE USE IN OWN PROJECTS. IF IT MEETS YOUR NEEDS, FEEL FREE TO USE IT, BUT IN CASE OF ANY MODIFICATION REQUESTS, I WILL CONSIDER MY OWN NEEDS FIRST.
It is still in a very early development phase, so I do not really recommend using it for now, because anything can change on it at any time and previous functions may break.
The package is part of the rabbitmq-multiverse.
Table of Contents
- Description
- Motivation
- Usage
- Installation
- Configuration
- Starting the consumer
- Events
- Dispatching events
- Listening to events
- Errors in listeners
- How to process an event asynchronously?
- Subscribing to events
- RPC
- Registering clients
- Calling remote procedures
- Registering remote procedures
- Symfony Messenger
- Lifecycle Events
- MessagePublishingEvent
- MessageReceivedEvent
- MessageProcessingEvent
- MessageProcessedEvent
- Known Issues
- License
Description
This package is an alternative for Symfony AMQP Messenger Component. Works very similarly, allows you to start a message consumer and send messages to RabbitMQ.
Extends the functionality of the original Symfony Messenger by adding the ability to send and receive events and RPC queries through RabbitMQ messages.
Motivation
Since the microservice architecture has become very popular, I needed a library that provides the possibility of communicating with services written in different programming languages or frameworks.
Symfony has a good messaging system, but it is a closed Symfony-only system. This package allows you to communicate through messages between Symfony and/or other non-Symfony microservices.
On the top of simple JSON messages, utilizes the Symfony Messenger system, which perfectly does the rest of the job.
Usage
Installation
You can install this package via composer using this command:
Configuration
First, you have to define the environment variables.
Then you have to add the configuration to the config/packages/messenger.yaml
file.
Starting the consumer
To start the consumer, you have to run the following command.
The consumer will start and listen to the queue for incoming messages.
Most of the options are the same as in the original Symfony Messenger consumer.
Start the consumer with the -h
option to see all available options.
Events
Provides an event based asynchronous communication between services.
Dispatching events
Create an event class that extends the MessagePublishEvent
class.
And after just dispatch the event like any other Symfony event.
Almost, just a little difference. Instead of the Symfony event dispatcher, you have to use the EventDispatcher
included in this package.
Since the Symfony event system does not support listening to interfaces on top of many events, the EventDispatcher
does the trick by calling the Symfony event dispatcher under the hood and pass the full name of MessagePublishEvent
and a listener
listening to this event will catch all events implementing this interface.
That's it, it's not so complicated.
Listening to events
Create an event listener class and add the AsMessageEventListener
attribute like in the example below.
You have to define the event name in the attribute. The event name must be the same as the event name defined in the event object.
The attribute behaves exactly like the Symfony event listener attribute, but adds one more tag to the service, what helps to collect the events listening to. The name differs from the Symfony attribute to avoid the confusion about which event system is used.
See more about the event listeners at Symfony docs. The only one thing you have to remember is to define the event name in the listener.
Errors in listeners
When an unhandled error occurs in a listener, the message will be requeued and the event will be dispatched again. This will happen until the message is successfully processed or the maximum number of attempts is reached. If multiple listeners are listening to the same event, the processing will stop at the first listener that throws an exception and the rest of the listeners will not be called.
Preventing this behavior there are two ways. The first one is to catch the exception in the listener and handle it. The second one is to listen to events and put messages to the queue and handle them separately and asynchronously. This way the failed message will not block the rest of the messages.
How to process an event asynchronously?
Oh, it's very simple! You need an intermediate listener that will put a message to the queue automatically and a message handler that will handle the message.
First create a message that extends EventMessage
. This message will be sent to the queue and will be processed by the message handler.
Then create an event listener that extends MessageEventListener
. This listener will listen to the event and put the message to the queue automatically.
Finally, create a message handler that will handle the message put to the queue.
It's pretty simple, right? I know, not really. But it works.
Subscribing to events
The consumer automatically creates the exchange and the queue if they do not exist and registers all listened events as bindings keys to the queue.
RPC
A synchronous-like communication between services.
Uses the JSON-RPC 2.0 protocol for communication.
Registering clients
To call remote procedures, you have to create an instance of the Client
class and register it in the service container.
Calling remote procedures
Create a service and inject the client into it.
Registering remote procedures
Create a service and add the AsRemoteProcedure
attribute.
Works very similarly to the event listeners described above. You can add the attribute to a class or to a method.
Or another example with the attribute added to a method.
When registering two or more procedures with the same name, an exception will be thrown at startup.
Symfony Messenger
The functionality of the original Symfony Messenger component is also available. Route the messages to the rabbitmq transport, and they will be sent to the queue and processed by the consumer.
Lifecycle Events
MessagePublishingEvent
Dispatched before the message is sent to the queue.
MessageReceivedEvent
Dispatched when the message is received from the queue.
MessageProcessingEvent
Dispatched when the message is being processed.
MessageProcessedEvent
Dispatched when the message is processed.
Known Issues
- NO TESTS! I know, I know. I will write them soon.
License
MIT licensed
All versions of symfony-rabbitmq with dependencies
ext-pcntl Version *
php-amqplib/php-amqplib Version ^3.0
datto/json-rpc Version ^6.1
illuminate/support Version ^11.5
ramsey/uuid Version ^4.7
symfony/messenger Version ^7.0