description |
---|
Ecotone support for Amazon SQS PHP |
composer require ecotone/sqs
Enqueue solid and powerful abstraction over asynchronous queues.
In order to use SQS Support
we need to add ConnectionFactory
to our Dependency Container.
{% tabs %} {% tab title="Symfony" %}
# config/services.yaml
Enqueue\Sqs\SqsConnectionFactory:
class: Enqueue\Sqs\SqsConnectionFactory
arguments:
- "sqs:?key=key&secret=secret®ion=us-east-1&version=latest"
{% endtab %}
{% tab title="Laravel" %}
use Enqueue\Sqs\SqsConnectionFactory;
public function register()
{
$this->app->singleton(SqsConnectionFactory::class, function () {
return new SqsConnectionFactory("sqs:?key=key&secret=secret®ion=us-east-1&version=latest");
});
}
{% endtab %}
{% tab title="Lite" %}
use Enqueue\Sqs\SqsConnectionFactory;
$application = EcotoneLiteApplication::boostrap(
[
SqsConnectionFactory::class => new SqsConnectionFactory("sqs:?key=key&secret=secret®ion=us-east-1&version=latest")
]
);
{% endtab %} {% endtabs %}
{% hint style="info" %}
We register our SqsConnectionFactory
under the class name Enqueue\Sqs\SqsConnectionFactory.
This will help Ecotone resolve it automatically, without any additional configuration.
{% endhint %}
To create Message Channel, we need to create Service Context.
use Ecotone\Sqs\SqsBackedMessageChannelBuilder;
class MessagingConfiguration
{
#[ServiceContext]
public function orderChannel()
{
return SqsBackedMessageChannelBuilder::create("orders");
}
}
Now orders
channel will be available in Messaging System.
SqsBackedMessageChannelBuilder::create("orders")
->withAutoDeclare(false) // do not auto declare queue
->withDefaultTimeToLive(1000) // limit TTL of messages
If you want to publish Message directly to Exchange, you may use of Publisher.
use Ecotone\Sqs\Configuration\SqsMessagePublisherConfiguration;
class PublisherConfiguration
{
#[ServiceContext]
public function registerPublisherConfig()
{
return
SqsMessagePublisherConfiguration::create(
MessagePublisher::class, // 1
"delivery", // 2
"application/json" // 3
);
}
}
Reference name
- Name under which it will be available in Dependency Container.Queue name
- Name of queue where Message should be publishedDefault Conversion [Optional]
- Default type, payload will be converted to.
SqsMessagePublisherConfiguration::create(queueName: $queueName)
->withAutoDeclareQueueOnSend(false) // 1
->withHeaderMapper("application.*") // 2
withAutoDeclareQueueOnSend
- should Ecotone try to declare queue before sending messagewithHeaderMapper
- On default headers are not send with message. You map provide mapping for headers that should be mapped toSQS Message
To connect consumer directly to a SQS Queue, we need to provide Ecotone
with information, how the Queue is configured.
use Ecotone\Sqs\Configuration\SqsMessageConsumerConfiguration;
class ConsumerConfiguration
{
#[ServiceContext]
public function registerConsumerConfig(): array
{
return [
SqsMessageConsumerConfiguration::create("orders_consumer", "orders")
];
}
}
- Provides Consumer that will be registered at given name
"orders_consumer"
and will be polling"orders"
queue
$consumerConfiguration = SqsMessageConsumerConfiguration::createDirectExchange()
->withDeclareOnStartup(false) // do not try to declare queue before consuming first message;