Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 83b37e8

Browse files
committed
feature #36525 Improve SQS interoperability (jderusse)
This PR was squashed before being merged into the 5.1-dev branch (closes #36525). Discussion ---------- Improve SQS interoperability | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | NA | License | MIT | Doc PR | NA The Symfony Messenger component provides a SerializerInterface to encode/decode the `Envelope`, this can be used to improve the Interoperability (see [article from jolicode](https://jolicode.com/blog/symfony-messenger-et-linteroperabilite) (french)) Sadly, the current implementation of SQS adapter json_encode the elements of the `Envelope` (`string body` + `string[] headers`) and store everything in the SQS message `Body`. That partially defect the interoperability: 3rd party have to also wrap (unwrap) message form json_encoded Body. This PR leverage the AWS SQS `Body` and `MessageAttribute` properties to store message information: ```yaml # before SQS Message: Body: {"body": "hello world", "headers": {"foo": "bar"}} MessageAttributes: {} # after SQS Message: Body: hello world MessageAttributes: foor: bar ``` Commits ------- 00d84c1 Improve SQS interoperability
2 parents f2f82d1 + 00d84c1 commit 83b37e8

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

.travis.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ env:
2121
- SYMFONY_PROCESS_PHP_TEST_BINARY=~/.phpenv/shims/php
2222
- MESSENGER_AMQP_DSN=amqp://localhost/%2f/messages
2323
- MESSENGER_REDIS_DSN=redis://127.0.0.1:7006/messages
24-
- MESSENGER_SQS_DSN=sqs://localhost:9494/messages?sslmode=disable
25-
- MESSENGER_SQS_FIFO_QUEUE_DSN=sqs://localhost:9494/messages.fifo?sslmode=disable
24+
- MESSENGER_SQS_DSN="sqs://localhost:9494/messages?sslmode=disable&poll_timeout=0.01"
25+
- MESSENGER_SQS_FIFO_QUEUE_DSN="sqs://localhost:9494/messages.fifo?sslmode=disable&poll_timeout=0.01"
2626
- SYMFONY_PHPUNIT_DISABLE_RESULT_CACHE=1
2727

2828
matrix:
@@ -73,8 +73,8 @@ before_install:
7373
7474
- |
7575
# Start Sqs server
76-
docker pull feathj/fake-sqs
77-
docker run -d -p 9494:9494 --name sqs feathj/fake-sqs
76+
docker pull asyncaws/testing-sqs
77+
docker run -d -p 9494:9494 --name sqs asyncaws/testing-sqs
7878
7979
- |
8080
# Start Kafka and install an up-to-date librdkafka

src/Symfony/Component/Messenger/Bridge/AmazonSqs/Tests/Transport/ConnectionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public function testKeepGettingPendingMessages()
109109
$queueUrl = $this->handleGetQueueUrl(0, $httpClient);
110110

111111
$httpClient->expects($this->at(1))->method('request')
112-
->with('POST', $queueUrl, ['body' => ['Action' => 'ReceiveMessage', 'VisibilityTimeout' => null, 'MaxNumberOfMessages' => 9, 'WaitTimeSeconds' => 20]])
112+
->with('POST', $queueUrl, ['body' => ['Action' => 'ReceiveMessage', 'VisibilityTimeout' => null, 'MaxNumberOfMessages' => 9, 'WaitTimeSeconds' => 20, 'MessageAttributeName.1' => 'All']])
113113
->willReturn($response);
114114
$response->expects($this->once())->method('getContent')->willReturn('<ReceiveMessageResponse>
115115
<ReceiveMessageResult>

src/Symfony/Component/Messenger/Bridge/AmazonSqs/Transport/Connection.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ private function getNewMessages(): \Generator
176176
'Action' => 'ReceiveMessage',
177177
'VisibilityTimeout' => $this->configuration['visibility_timeout'],
178178
'MaxNumberOfMessages' => $this->configuration['buffer_size'],
179+
'MessageAttributeName.1' => 'All',
179180
'WaitTimeSeconds' => $this->configuration['wait_time'],
180181
]);
181182
}
@@ -186,9 +187,18 @@ private function getNewMessages(): \Generator
186187

187188
$xml = new \SimpleXMLElement($this->currentResponse->getContent());
188189
foreach ($xml->ReceiveMessageResult->Message as $xmlMessage) {
190+
$headers = [];
191+
foreach ($xmlMessage->MessageAttribute as $item) {
192+
if ('String' !== (string) $item->Value->DataType) {
193+
continue;
194+
}
195+
$headers[(string) $item->Name] = (string) $item->Value->StringValue;
196+
}
189197
$this->buffer[] = [
190198
'id' => (string) $xmlMessage->ReceiptHandle,
191-
] + json_decode($xmlMessage->Body, true);
199+
'body' => (string) $xmlMessage->Body,
200+
'headers' => $headers,
201+
];
192202
}
193203

194204
$this->currentResponse = null;
@@ -246,17 +256,23 @@ public function send(string $body, array $headers, int $delay = 0, ?string $mess
246256
$this->setup();
247257
}
248258

249-
$messageBody = json_encode(['body' => $body, 'headers' => $headers]);
250-
251259
$parameters = [
252260
'Action' => 'SendMessage',
253-
'MessageBody' => $messageBody,
261+
'MessageBody' => $body,
254262
'DelaySeconds' => $delay,
255263
];
256264

265+
$index = 0;
266+
foreach ($headers as $name => $value) {
267+
++$index;
268+
$parameters["MessageAttribute.$index.Name"] = $name;
269+
$parameters["MessageAttribute.$index.Value.DataType"] = 'String';
270+
$parameters["MessageAttribute.$index.Value.StringValue"] = $value;
271+
}
272+
257273
if ($this->isFifoQueue($this->configuration['queue_name'])) {
258274
$parameters['MessageGroupId'] = null !== $messageGroupId ? $messageGroupId : __METHOD__;
259-
$parameters['MessageDeduplicationId'] = null !== $messageDeduplicationId ? $messageDeduplicationId : sha1($messageBody);
275+
$parameters['MessageDeduplicationId'] = null !== $messageDeduplicationId ? $messageDeduplicationId : sha1(json_encode(['body' => $body, 'headers' => $headers]));
260276
}
261277

262278
$this->call($this->getQueueUrl(), $parameters);

0 commit comments

Comments
 (0)