Messaging Gateway

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 does build from domain specific objects a Message that is send via Message channel. To not have dependency on the Ecotone API — including the gateway class, Ecotone provides the Gateway as interface. Framework generates a proxy for any interface and internally invokes the gateway methods. By using dependency injection, you can then expose the interface to your business methods.

Implementing own Gateway

namespace Product;
use Ecotone\Messaging\Annotation\Gateway;
use Ecotone\Messaging\Annotation\MessageEndpoint;
/**
* @MessageEndpoint() // 1
*/
interface ProductGateway
{
/**
* @Gateway(requestChannel="buyProduct") // 2
*/
public function order(string $productName) : void;
}
  1. @MessageEndpoint marks interface so it can be find by Ecotone. By default gateway will be available under interface name, in that case Product\ProductGateway. If you want to register it under different name for example "productGateway", then pass it to annotation @MessageEndpoint(referenceName="productGateway")

Symfony
Lite Configuraiton
Symfony

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.

Lite Configuraiton

In Lite Configuration you can retrieve it using messaging system configuration.

$productGateway = $messagingSystem->getGatewayByName(ProductGateway::class);

2. @Gateway enables method to be used as Messaging Gateway. You may have multiple Gateways defined within interface.

Gateway reply

/**
* @Gateway(requestChannel="getPrice")
*/
public function getPrice(string $productName) : int;

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.

Parameters to Message Conversion

In order to build Message, Parameter Converters are introduced. You may configure them manually or let Ecotone make use of default parameter converters.

/**
* @Gateway(
* requestChannel="orders",
* parameterConverters={
* @Payload(parameterName="order"),
* @Header(parameterName="executorId", headerName="executor")
* }
* )
*/
public function placeOrder(Order $order, string $executorId);

Parameter converter types:

  • This will convert string passed under $content parameter to message payload

* parameterConverters={
* @Payload(parameterName="content")
* }
public function sendMail(string $content) : void;
  • This convert $content to message's payload and will add to headers under "receiverEmail" key value of $toEmail

* parameterConverters={
* @Payload(parameterName="content"),
* @Header(parameterName="toEmail", headerName="receiverEmail")
* }
public function sendMail(string $content, string $toEmail) : void;