-
Notifications
You must be signed in to change notification settings - Fork 444
DBAL Transport #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
DBAL Transport #54
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
192019a
dbal transport
ASKozienko 674edd4
dbal transport
ASKozienko c840b68
dbal transport
ASKozienko d1b3c13
dbal transport
ASKozienko b836dc8
Merge remote-tracking branch 'origin/master' into dbal-transport
ASKozienko 334820c
dbal transport
ASKozienko eb8bf6a
dbal transport
ASKozienko ec51705
dbal transport
ASKozienko 783a956
[dbal] Add documentation
makasim 9bb6e13
dbal transport
ASKozienko fd72fdb
Merge branch 'dbal-transport' of github.com:php-enqueue/enqueue-dev i…
ASKozienko 75b8bbe
dbal transport
ASKozienko abcf56c
dbal transport
ASKozienko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,110 @@ | ||
# Doctrine DBAL transport | ||
|
||
The transport uses [Doctrine DBAL](http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/) library and SQL like server as a broker. | ||
It creates a table there. Pushes and pops messages to\from that table. | ||
|
||
**Limitations** It works only in auto ack mode hence If consumer crashes the message is lost. | ||
|
||
* [Installation](#installation) | ||
* [Init database](#init-database) | ||
* [Create context](#create-context) | ||
* [Send message to topic](#send-message-to-topic) | ||
* [Send message to queue](#send-message-to-queue) | ||
* [Consume message](#consume-message) | ||
|
||
## Installation | ||
|
||
```bash | ||
$ composer require enqueue/dbal | ||
``` | ||
|
||
## Create context | ||
|
||
* With config (a connection is created internally): | ||
|
||
```php | ||
<?php | ||
use Enqueue\Dbal\DbalConnectionFactory; | ||
|
||
$factory = new DbalConnectionFactory([ | ||
'connection' => [ | ||
'dbname' => 'mqdev', | ||
'user' => 'user', | ||
'password' => 'pass', | ||
'host' => 'localhost', | ||
'port' => 3306, | ||
'driver' => 'pdo_mysql', | ||
], | ||
'table_name' => 'enqueue', | ||
]); | ||
|
||
$psrContext = $factory->createContext(); | ||
``` | ||
|
||
* With existing connection: | ||
|
||
```php | ||
<?php | ||
use Enqueue\Dbal\ManagerRegistryConnectionFactory; | ||
use Doctrine\Common\Persistence\ManagerRegistry; | ||
|
||
/** @var ManagerRegistry $registry */ | ||
|
||
$factory = new ManagerRegistryConnectionFactory($registry, [ | ||
'connection_name' => 'default', | ||
]); | ||
|
||
$psrContext = $factory->createContext(); | ||
``` | ||
|
||
## Init database | ||
|
||
At first time you have to create a table where your message will live. There is a handy methods for this `createDataBaseTable` on the context. | ||
Please pay attention to that the database has to be created manually. | ||
|
||
```php | ||
<?php | ||
/** @var \Enqueue\Dbal\DbalContext $psrContext */ | ||
|
||
$psrContext->createDataBaseTable(); | ||
``` | ||
|
||
## Send message to topic | ||
|
||
```php | ||
<?php | ||
/** @var \Enqueue\Dbal\DbalContext $psrContext */ | ||
|
||
$fooTopic = $psrContext->createTopic('aTopic'); | ||
$message = $psrContext->createMessage('Hello world!'); | ||
|
||
$psrContext->createProducer()->send($fooTopic, $message); | ||
``` | ||
|
||
## Send message to queue | ||
|
||
```php | ||
<?php | ||
/** @var \Enqueue\Dbal\DbalContext $psrContext */ | ||
|
||
$fooQueue = $psrContext->createQueue('aQueue'); | ||
$message = $psrContext->createMessage('Hello world!'); | ||
|
||
$psrContext->createProducer()->send($fooQueue, $message); | ||
``` | ||
|
||
## Consume message: | ||
|
||
```php | ||
<?php | ||
/** @var \Enqueue\Dbal\DbalContext $psrContext */ | ||
|
||
$fooQueue = $psrContext->createQueue('aQueue'); | ||
$consumer = $psrContext->createConsumer($fooQueue); | ||
|
||
$message = $consumer->receive(); | ||
|
||
// process a message | ||
``` | ||
|
||
[back to index](index.md) |
This file contains hidden or 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 hidden or 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,6 @@ | ||
*~ | ||
/composer.lock | ||
/composer.phar | ||
/phpunit.xml | ||
/vendor/ | ||
/.idea/ |
This file contains hidden or 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,21 @@ | ||
sudo: false | ||
|
||
git: | ||
depth: 1 | ||
|
||
language: php | ||
|
||
php: | ||
- '5.6' | ||
- '7.0' | ||
|
||
cache: | ||
directories: | ||
- $HOME/.composer/cache | ||
|
||
install: | ||
- composer self-update | ||
- composer install --prefer-source | ||
|
||
script: | ||
- vendor/bin/phpunit --exclude-group=functional |
This file contains hidden or 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,148 @@ | ||
<?php | ||
namespace Enqueue\Dbal\Client; | ||
|
||
use Enqueue\Client\Config; | ||
use Enqueue\Client\DriverInterface; | ||
use Enqueue\Client\Message; | ||
use Enqueue\Client\MessagePriority; | ||
use Enqueue\Dbal\DbalContext; | ||
use Enqueue\Dbal\DbalMessage; | ||
use Enqueue\Psr\PsrMessage; | ||
use Psr\Log\LoggerInterface; | ||
use Psr\Log\NullLogger; | ||
|
||
class DbalDriver implements DriverInterface | ||
{ | ||
/** | ||
* @var DbalContext | ||
*/ | ||
private $context; | ||
|
||
/** | ||
* @var Config | ||
*/ | ||
private $config; | ||
|
||
/** | ||
* @param DbalContext $context | ||
* @param Config $config | ||
*/ | ||
public function __construct(DbalContext $context, Config $config) | ||
{ | ||
$this->context = $context; | ||
$this->config = $config; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @return DbalMessage | ||
*/ | ||
public function createTransportMessage(Message $message) | ||
{ | ||
$properties = $message->getProperties(); | ||
|
||
$headers = $message->getHeaders(); | ||
$headers['content_type'] = $message->getContentType(); | ||
|
||
$transportMessage = $this->context->createMessage(); | ||
$transportMessage->setBody($message->getBody()); | ||
$transportMessage->setHeaders($headers); | ||
$transportMessage->setProperties($properties); | ||
$transportMessage->setMessageId($message->getMessageId()); | ||
$transportMessage->setTimestamp($message->getTimestamp()); | ||
$transportMessage->setDelay($message->getDelay()); | ||
$transportMessage->setReplyTo($message->getReplyTo()); | ||
$transportMessage->setCorrelationId($message->getCorrelationId()); | ||
|
||
return $transportMessage; | ||
} | ||
|
||
/** | ||
* @param DbalMessage $message | ||
* | ||
* {@inheritdoc} | ||
*/ | ||
public function createClientMessage(PsrMessage $message) | ||
{ | ||
$clientMessage = new Message(); | ||
|
||
$clientMessage->setBody($message->getBody()); | ||
$clientMessage->setHeaders($message->getHeaders()); | ||
$clientMessage->setProperties($message->getProperties()); | ||
|
||
$clientMessage->setContentType($message->getHeader('content_type')); | ||
$clientMessage->setMessageId($message->getMessageId()); | ||
$clientMessage->setTimestamp($message->getTimestamp()); | ||
$clientMessage->setPriority(MessagePriority::NORMAL); | ||
$clientMessage->setDelay($message->getDelay()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. setReplyTo |
||
$clientMessage->setReplyTo($message->getReplyTo()); | ||
$clientMessage->setCorrelationId($message->getCorrelationId()); | ||
|
||
return $clientMessage; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function sendToRouter(Message $message) | ||
{ | ||
if (false == $message->getProperty(Config::PARAMETER_TOPIC_NAME)) { | ||
throw new \LogicException('Topic name parameter is required but is not set'); | ||
} | ||
|
||
$queue = $this->createQueue($this->config->getRouterQueueName()); | ||
$transportMessage = $this->createTransportMessage($message); | ||
|
||
$this->context->createProducer()->send($queue, $transportMessage); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function sendToProcessor(Message $message) | ||
{ | ||
if (false == $message->getProperty(Config::PARAMETER_PROCESSOR_NAME)) { | ||
throw new \LogicException('Processor name parameter is required but is not set'); | ||
} | ||
|
||
if (false == $queueName = $message->getProperty(Config::PARAMETER_PROCESSOR_QUEUE_NAME)) { | ||
throw new \LogicException('Queue name parameter is required but is not set'); | ||
} | ||
|
||
$transportMessage = $this->createTransportMessage($message); | ||
$destination = $this->createQueue($queueName); | ||
|
||
$this->context->createProducer()->send($destination, $transportMessage); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function createQueue($queueName) | ||
{ | ||
return $this->context->createQueue($this->config->createTransportQueueName($queueName)); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setupBroker(LoggerInterface $logger = null) | ||
{ | ||
$logger = $logger ?: new NullLogger(); | ||
$log = function ($text, ...$args) use ($logger) { | ||
$logger->debug(sprintf('[DbalDriver] '.$text, ...$args)); | ||
}; | ||
|
||
$log('Creating database table: "%s"', $this->context->getTableName()); | ||
$this->context->createDataBaseTable(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getConfig() | ||
{ | ||
return $this->config; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
replyTo,correlationId