-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from tomaj/sqs
Sqs driver
- Loading branch information
Showing
5 changed files
with
180 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
use Tomaj\Hermes\Driver\AmazonSqsDriver; | ||
use Tomaj\Hermes\Dispatcher; | ||
use Tomaj\Hermes\Message; | ||
use Aws\Sqs\SqsClient; | ||
|
||
require_once __DIR__ . '/../../vendor/autoload.php'; | ||
|
||
$client = SqsClient::factory([ | ||
'version' => 'latest', | ||
'region' => 'eu-west-1', | ||
'key' => '*key*', | ||
'secret' => '*secret*', | ||
]); | ||
|
||
$driver = new AmazonSqsDriver($client, '*queueName*'); | ||
$dispatcher = new Dispatcher($driver); | ||
$counter = 1; | ||
while (true) { | ||
$dispatcher->emit(new Message('type1', ['message' => $counter])); | ||
echo "Emited message $counter\n"; | ||
$counter++; | ||
sleep(1); | ||
} |
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,22 @@ | ||
<?php | ||
|
||
use Tomaj\Hermes\Driver\AmazonSqsDriver; | ||
use Tomaj\Hermes\Dispatcher; | ||
use Tomaj\Hermes\Handler\EchoHandler; | ||
use Aws\Sqs\SqsClient; | ||
|
||
require_once __DIR__ . '/../../vendor/autoload.php'; | ||
|
||
$client = SqsClient::factory([ | ||
'version' => 'latest', | ||
'region' => 'eu-west-1', | ||
'key' => '*key*', | ||
'secret' => '*secret*', | ||
]); | ||
|
||
$driver = new AmazonSqsDriver($client, '*queueName*'); | ||
$dispatcher = new Dispatcher($driver); | ||
|
||
$dispatcher->registerHandler('type1', new EchoHandler()); | ||
|
||
$dispatcher->handle(); |
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,124 @@ | ||
<?php | ||
|
||
namespace Tomaj\Hermes\Driver; | ||
|
||
use Closure; | ||
use Exception; | ||
use Tomaj\Hermes\MessageInterface; | ||
use Tomaj\Hermes\MessageSerializer; | ||
use Aws\Sqs\SqsClient; | ||
|
||
class AmazonSqsDriver implements DriverInterface | ||
{ | ||
use SerializerAwareTrait; | ||
|
||
/** | ||
* @var SqsClient | ||
*/ | ||
private $client; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $queueName; | ||
|
||
/** | ||
* string | ||
*/ | ||
private $queueUrl; | ||
|
||
/** | ||
* integer | ||
*/ | ||
private $sleepInterval = 0; | ||
|
||
/** | ||
* Create new Amazon SQS driver. | ||
* | ||
* You have to create aws client instnace and provide it to this driver. | ||
* You can use service builder or factory method. | ||
* | ||
* <code> | ||
* use Aws\Sqs\SqsClient; | ||
* | ||
* $client = SqsClient::factory(array( | ||
* 'profile' => '<profile in your aws credentials file>', | ||
* 'region' => '<region name>' | ||
* )); | ||
* </code> | ||
* | ||
* or | ||
* | ||
* <code> | ||
* use Aws\Common\Aws; | ||
* | ||
* // Create a service builder using a configuration file | ||
* $aws = Aws::factory('/path/to/my_config.json'); | ||
* | ||
* // Get the client from the builder by namespace | ||
* $client = $aws->get('Sqs'); | ||
* </code> | ||
* | ||
* More examples see: https://docs.aws.amazon.com/aws-sdk-php/v2/guide/service-sqs.html | ||
* | ||
* | ||
* @see examples/sqs folder | ||
* | ||
* @param AMQPChannel $client | ||
* @param string $queueName | ||
* @param array $queueAttributes | ||
*/ | ||
public function __construct(SqsClient $client, $queueName, $queueAttributes = []) | ||
{ | ||
$this->client = $client; | ||
$this->queueName = $queueName; | ||
$this->serializer = new MessageSerializer(); | ||
|
||
$result = $client->createQueue([ | ||
'QueueName' => $queueName, | ||
'Attributes' => $queueAttributes, | ||
]); | ||
$this->queueUrl = $result->get('QueueUrl'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function send(MessageInterface $message) | ||
{ | ||
$this->client->sendMessage([ | ||
'QueueUrl' => $this->queueUrl, | ||
'MessageBody' => $this->serializer->serialize($message), | ||
]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function wait(Closure $callback) | ||
{ | ||
while (true) { | ||
$result = $this->client->receiveMessage(array( | ||
'QueueUrl' => $this->queueUrl, | ||
'WaitTimeSeconds' => 20, | ||
)); | ||
|
||
$messages = $result['Messages']; | ||
|
||
if ($messages) { | ||
foreach ($messages as $message) { | ||
$hermesMessage = $this->serializer->unserialize($message['Body']); | ||
$callback($hermesMessage); | ||
$result = $this->client->deleteMessage(array( | ||
'QueueUrl' => $this->queueUrl, | ||
'ReceiptHandle' => $message['ReceiptHandle'], | ||
)); | ||
} | ||
} | ||
|
||
if ($this->sleepInterval) { | ||
sleep($this->sleepInterval); | ||
} | ||
} | ||
} | ||
} |