Messaging Gateway
Message Gateway PHP
Last updated
Message Gateway PHP
Last updated
The Messaging Gateway encapsulates messaging-specific code (The code required to send or receive a Message) and separates it from the rest of the application code.
It takes Application specific data and convert it Message which then is sent via Message channel. This hide Messaging specific code, from user's code.
Command/Query/Event Buses are implementations of Messaging Gateways.
Ecotone aims for eliminating Framework related code from Business related code, that's why Gateway can defined as interface in user's code base. Ecotone is responsible for generating implementation for any interface.
To implement custom Gateway, we will be using clear interfaces and message channel.
By default gateway will be available under interface name in DI, in that case Product\ProductGateway.
If you want to register it under different name for example "productGateway", then pass it to annotation #[ClassReference("productGateway")]
If you are using Symfony Integration, then it will be auto registered in Dependency Container with possibility to auto-wire the gateway.
As Command/Query/Event buses are Gateways, you can auto-wire them. They can be injected into Controller and called directly.
2. Gateway
enables method to be used as Messaging Gateway. You may have multiple Gateways defined within interface. The "buyProduct"
is a channel name that we will be requesting.
Using combination of Messaging Gateway and Router we can build our own Buses. Messaging Gateway will be responsible for building Message and sending to the Router. And Router will be then based on need route it by payload / headers or whatever is needed.
In some cases you may want to invoke Query/Command Handler directly, not via Bus. In that case you may define Message Gateway that routes to given Handler.
Service Command Handler:
Gateway:
This also works for Query Handlers. This way we can build an interface, which provides API for given set of behaviours e.g. OrderApi
.
You may expose Aggregate's Command and Query Handlers through interface.
Aggregate:
Gateway:
Gateway may return values, but as you probably remember, everything is connected via Message Channels. So how does we get the reply?
During Message building, gateway adds header replyChannel
which contains automatically created Channel. During Endpoint's method invocation, if any value was returned it will be sent via reply Channel.
This way gateway may receive the reply and return it.
If you have registered Converter for specific Media Type, then you can tell Ecotone
to convert result of any Gateway to specific format. This is especially useful, when we are dealing with QueryBus
, when we want to return the result to the caller of the request.
In order to do this, we need to make use of Metadata
and replyContentType
header.
Repository implemented via interfaces are just higher level abstraction of Message Gateway. You enable them by marking attribute #[Repository].
Read more about Repositories in Command Handling section.
In order to build Message, Parameter Converters are introduced. You may configure them manually or let Ecotone make use of default parameter converters.
Parameter converter types:
This will convert string passed under $content
parameter to message payload
This convert $content
to message's payload and will add to headers under "receiverEmail" key value of $toEmail