Message Consumer - By annotation with Attribute we tell Ecotone to activate this method to be used as Message Consumer. sqs_consumer will be the endpoint it, so we will be running our consumer by this name.
As Message Consumer can be connected to any external broker, we also need to know that it's supposed to be used with SQS.
To do this, we will introduce Extension Object, that will tell us how to configure this Message Consumer.
Module Annotation - This tell Ecotone to use this class as Module
Creating Module - In here we may look for all classes containing given attribute, if there is a need.
Preparing Module - In here we can adjust configuration using our Module. In our scenario we are using Message Consumer Configuration to register SQS Inbound Channel Adapter in Messaging Configuration.
We tell Ecotone what types of Extension Objects this Module is looking for.
You want to be sure that your ModulePackageList::SQS_PACKAGE is available in ModulePackageList::allPackages, and ModulePackageList::getModuleClassesForPackage, so it can be correctly resolved.
Before we will test this, we need a way to send Message to SQS Queue first. In order to do so, we need to set up Message Publisher.
We have register Inbound Channel Adapter, however we have not connected it anyhow SqsConsumerExample.
This is the same for all types of Message Consumers, so there is a separate module for that MessageConsumerModule.
This module connect this class to channel named by endpoint id.
Our inbound Channel Adapter (3rd parameter), we are using endpoint id, as request channel after fetching message from the queue. Thanks to that it's all connected.
Message Publisher
Message Publisher is Gateway implementation that allow us to create abstraction that will send messages to SQS Queue via simple interface.
First let's create Sqs Publisher Configuration:
namespaceEcotone\SqsDemo\Configuration;finalclassSqsMessagePublisherConfiguration{privatebool $autoDeclareOnSend =true;privatestring $headerMapper =''; private function __construct(private string $connectionReference, private string $queueName, private ?string $outputDefaultConversionMediaType, private string $referenceName)
{ } public static function create(string $publisherReferenceName = MessagePublisher::class, string $queueName = '', ?string $outputDefaultConversionMediaType = null, string $connectionReference = SqsConnectionFactory::class): self
{returnnewself($connectionReference, $queueName, $outputDefaultConversionMediaType, $publisherReferenceName); }publicfunctiongetConnectionReference():string {return$this->connectionReference; }publicfunctionwithAutoDeclareQueueOnSend(bool $autoDeclareQueueOnSend):self {$this->autoDeclareOnSend = $autoDeclareQueueOnSend;return$this; }/** * @paramstring $headerMapper comma separated list of headers to be mapped. * (e.g. "\*" or "thing1*, thing2" or "*thing1") */publicfunctionwithHeaderMapper(string $headerMapper):self {$this->headerMapper = $headerMapper;return$this; }publicfunctionisAutoDeclareOnSend():bool {return$this->autoDeclareOnSend; }publicfunctiongetHeaderMapper():string {return$this->headerMapper; }publicfunctiongetOutputDefaultConversionMediaType():?string {return$this->outputDefaultConversionMediaType; }publicfunctiongetQueueName():string {return$this->queueName; }publicfunctiongetReferenceName():string {return$this->referenceName; }}
and then we can create Module, that will use of this config in order to register our Sqs Outbound Channel Adapter.
Extension Object Resolver - This is a helper class that allows use to fetch from extension object, concrete class or classes of given type.
Registering Messaging Gateways - Messaging Gateways are entrypoints to messaging system as Message Publisher is an interface that will be called by end-user, this means it's a Messaging Gateway. We register all method that this interface has together with parameter converters
Registering Outbound Channel Adapter - Using our Message Publisher Configuration we register our Sqs Outbound Channel Adapter.
The input channel name is the same as request channel name of Gateway. This way, when interface's method will be called under the hood we will call our Outbound Channel Adapter.
We are using Message Publisher to send the message and then we are consuming it using our Message Consumer.
Summary
We have introduced Message Channel and Message Consumer and Publisher.
However the the hood we had chance to get known with Messaging Gateways, Inbound and Outbound Channel Adapters and Message Handlers.
Using few patterns you may actually build really customized Messaging Configurations. This is power of decoupled system, when you get familiar with them, they open much more possibilities.