Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #26632 [Messenger] Add AMQP adapter (sroze)
This PR was squashed before being merged into the 4.1-dev branch (closes #26632). Discussion ---------- [Messenger] Add AMQP adapter | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | ø | License | MIT - [x] Depends on the Messenger component #24411 - [x] Add tests once we are all happy about the structure --- In order to give a great DX for simple needs such as sending messages through an AMQP broker such as RabbitMq, we should ship an AMQP adapter for the Messenger component within Symfony Core. It should be as simple as this proposal. We don't need to handle more specific use-cases nor brokers as other adapters such as the [enqueue adapter](https://github.com/sroze/enqueue-bridge) can also be used. Configuring the adapter is as simple as the following configuration: ```yaml # config/packages/messenger_adapters.yaml framework: messenger: adapter: "%env(MESSENGER_DSN)%" ``` With the given `.env` for example: ``` MESSENGER_DSN=amqp://guest:guest@localhost:5672/%2f/messages ``` Keep in mind that after having configured the adapter, developers have to route their messages to the given adapter. ```yaml # config/packages/messenger_routes.yaml framework: messenger: routing: producer). 'App\Message\Command\CreateNumber': messenger.default_sender ``` --- Additionally, multiple adapters can be created and messages routed to these ones. ```yaml # config/packages/messenger_routes.yaml framework: messenger: adapters: commands: "amqp://guest:guest@localhost:5672/%2f/commands" maintenance: "amqp://guest:guest@localhost:5672/%2f/maintenance" routing: producer). 'App\Message\Command\CreateNumber': messenger.commands_sender 'App\Message\Command\MaintenanceSpecificCommand': messenger.maintenance_sender ``` Commits ------- 798c230 [Messenger] Add AMQP adapter
- Loading branch information
Showing
33 changed files
with
1,322 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
...mfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/messenger_adapter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
$container->loadFromExtension('framework', array( | ||
'messenger' => array( | ||
'adapters' => array( | ||
'default' => 'amqp://localhost/%2f/messages', | ||
'customised' => array( | ||
'dsn' => 'amqp://localhost/%2f/messages?exchange_name=exchange_name', | ||
'options' => array('queue_name' => 'Queue'), | ||
), | ||
), | ||
), | ||
)); |
16 changes: 16 additions & 0 deletions
16
...mfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/messenger_adapter.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:framework="http://symfony.com/schema/dic/symfony" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd | ||
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd"> | ||
|
||
<framework:config> | ||
<framework:messenger> | ||
<framework:adapter name="default" dsn="amqp://localhost/%2f/messages" /> | ||
<framework:adapter name="customised" dsn="amqp://localhost/%2f/messages?exchange_name=exchange_name"> | ||
<framework:option name="queue_name" value="Queue" /> | ||
</framework:adapter> | ||
</framework:messenger> | ||
</framework:config> | ||
</container> |
8 changes: 8 additions & 0 deletions
8
...mfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/messenger_adapter.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
framework: | ||
messenger: | ||
adapters: | ||
default: 'amqp://localhost/%2f/messages' | ||
customised: | ||
dsn: 'amqp://localhost/%2f/messages?exchange_name=exchange_name' | ||
options: | ||
queue_name: Queue |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/Symfony/Component/Messenger/Adapter/AmqpExt/AmqpAdapterFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger\Adapter\AmqpExt; | ||
|
||
use Symfony\Component\Messenger\Adapter\Factory\AdapterFactoryInterface; | ||
use Symfony\Component\Messenger\Transport\ReceiverInterface; | ||
use Symfony\Component\Messenger\Transport\SenderInterface; | ||
use Symfony\Component\Messenger\Transport\Serialization\DecoderInterface; | ||
use Symfony\Component\Messenger\Transport\Serialization\EncoderInterface; | ||
|
||
/** | ||
* @author Samuel Roze <samuel.roze@gmail.com> | ||
*/ | ||
class AmqpAdapterFactory implements AdapterFactoryInterface | ||
{ | ||
private $encoder; | ||
private $decoder; | ||
private $debug; | ||
|
||
public function __construct(EncoderInterface $encoder, DecoderInterface $decoder, bool $debug) | ||
{ | ||
$this->encoder = $encoder; | ||
$this->decoder = $decoder; | ||
$this->debug = $debug; | ||
} | ||
|
||
public function createReceiver(string $dsn, array $options): ReceiverInterface | ||
{ | ||
return new AmqpReceiver($this->decoder, Connection::fromDsn($dsn, $options, $this->debug)); | ||
} | ||
|
||
public function createSender(string $dsn, array $options): SenderInterface | ||
{ | ||
return new AmqpSender($this->encoder, Connection::fromDsn($dsn, $options, $this->debug)); | ||
} | ||
|
||
public function supports(string $dsn, array $options): bool | ||
{ | ||
return 0 === strpos($dsn, 'amqp://'); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/Symfony/Component/Messenger/Adapter/AmqpExt/AmqpFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger\Adapter\AmqpExt; | ||
|
||
class AmqpFactory | ||
{ | ||
public function createConnection(array $credentials): \AMQPConnection | ||
{ | ||
return new \AMQPConnection($credentials); | ||
} | ||
|
||
public function createChannel(\AMQPConnection $connection): \AMQPChannel | ||
{ | ||
return new \AMQPChannel($connection); | ||
} | ||
|
||
public function createQueue(\AMQPChannel $channel): \AMQPQueue | ||
{ | ||
return new \AMQPQueue($channel); | ||
} | ||
|
||
public function createExchange(\AMQPChannel $channel): \AMQPExchange | ||
{ | ||
return new \AMQPExchange($channel); | ||
} | ||
} |
Oops, something went wrong.