Links

Amazon SQS

Ecotone support for Amazon SQS PHP

Installation

composer require ecotone/sqs

Module Powered By

Enqueue solid and powerful abstraction over asynchronous queues.

Configuration

In order to use AMQP Support we need to add ConnectionFactory to our Dependency Container.
Symfony
Laravel
Lite
# config/services.yaml
Enqueue\Sqs\SqsConnectionFactory:
class: Enqueue\Sqs\SqsConnectionFactory
arguments:
- "sqs:?key=key&secret=secret&region=us-east-1&version=latest"
use Enqueue\Sqs\SqsConnectionFactory;
public function register()
{
$this->app->singleton(SqsConnectionFactory::class, function () {
return new SqsConnectionFactory("sqs:?key=key&secret=secret&region=us-east-1&version=latest");
});
}
use Enqueue\Sqs\SqsConnectionFactory;
$application = EcotoneLiteApplication::boostrap(
[
SqsConnectionFactory::class => new SqsConnectionFactory("sqs:?key=key&secret=secret&region=us-east-1&version=latest")
]
);
We register our SqsConnectionFactory under the class name Enqueue\Sqs\SqsConnectionFactory. This will help Ecotone resolve it automatically, without any additional configuration.

Message Channel

To create Message Channel, we need to create Service Context.
class MessagingConfiguration
{
#[ServiceContext]
public function orderChannel()
{
return SqsBackedMessageChannelBuilder::create("orders");
}
}
Now orders channel will be available in Messaging System.

Message Channel Configuration

SqsBackedMessageChannelBuilder::create("orders")
->withAutoDeclare(false) // do not auto declare queue
->withDefaultTimeToLive(1000) // limit TTL of messages

Message Publisher

If you want to publish Message directly to Exchange, you may use of Publisher.
class PublisherConfiguration
{
#[ServiceContext]
public function registerPublisherConfig()
{
return
SqsMessagePublisherConfiguration::create(
MessagePublisher::class, // 1
"delivery", // 2
"application/json" // 3
);
}
}
  1. 1.
    Reference name - Name under which it will be available in Dependency Container.
  2. 2.
    Queue name - Name of queue where Message should be published
  3. 3.
    Default Conversion [Optional] - Default type, payload will be converted to.

Publisher Configuration

SqsMessagePublisherConfiguration::create(queueName: $queueName)
->withAutoDeclareQueueOnSend(false) // 1
->withHeaderMapper("application.*") // 2
  1. 1.
    withAutoDeclareQueueOnSend - should Ecotone try to declare queue before sending message
  2. 2.
    withHeaderMapper - On default headers are not send with AMQP message. You map provide mapping for headers that should be mapped to AMQP Message

Message Consumer

To connect consumer directly to a AMQP Queue, we need to provide Ecotone with information, how the Queue is configured.
class ConsumerConfiguration
{
#[ServiceContext]
public function registerConsumerConfig(): array
{
return [
SqsMessageConsumerConfiguration::create("orders_consumer", "orders")
];
}
}
  1. 1.
    Provides Consumer that will be registered at given name "orders_consumer" and will be polling "orders" queue

Consumer Configuration

$consumerConfiguration = SqsMessageConsumerConfiguration::createDirectExchange
->withDeclareOnStartup(false) // do not try to declare queue before consuming first message;