Last active
August 7, 2017 21:13
-
-
Save PrzemekMalak/6741a254fe9ba056e16e825aea1de548 to your computer and use it in GitHub Desktop.
Kod do wpisu na blog: https://devdiaries.pl/amazon-web-services-kolejki-fifo/
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
import boto3 | |
import hashlib | |
sqs = boto3.resource('sqs', region_name='eu-west-1') | |
queue_name = 'FifoQueue.fifo' | |
# tworzymy kolejkę wiadomości FIFO | |
queue_create = sqs.create_queue(QueueName=queue_name, Attributes={'FifoQueue':'true', 'ContentBasedDeduplication': 'true'}) | |
queue = sqs.get_queue_by_name(QueueName=queue_name) | |
# wysyłamy wiadomości do kolejki - w dwóch grupach | |
''' | |
for i in range(1,5): | |
message_content = 'G1_{0}'.format(i) | |
queue.send_message(MessageBody=message_content, MessageGroupId='Group1') | |
for i in range(1,3): | |
message_content = 'G2_{0}'.format(i) | |
queue.send_message(MessageBody=message_content, MessageGroupId='Group2') | |
''' | |
# pobieramy wiadomości z kolejki - 3 requesty | |
''' | |
print('First request') | |
for message in queue.receive_messages(MaxNumberOfMessages=2): | |
print('{0}'.format(message.body)) | |
print('Second request') | |
for message in queue.receive_messages(MaxNumberOfMessages=2): | |
print('{0}'.format(message.body)) | |
print('Third request') | |
for message in queue.receive_messages(MaxNumberOfMessages=2): | |
print('{0}'.format(message.body)) | |
''' | |
# pobieramy 1 wiadomość z kolejki i kasujemy ją | |
''' | |
for message in queue.receive_messages(MaxNumberOfMessages=1): | |
print('{0}'.format(message.body)) | |
print('Deleting message') | |
message.delete() | |
''' | |
''' | |
# pobieramy wiadomości z kolejki po obsłużeniu jednej wiadomości- 2 requesty | |
print('First request') | |
for message in queue.receive_messages(): | |
print('{0}'.format(message.body)) | |
print('Second request') | |
for message in queue.receive_messages(): | |
print('{0}'.format(message.body)) | |
''' | |
# próbujemy wstawić wiadomości o takiej samej treści do SQS FIFO | |
''' | |
for i in range(1,5): | |
message_content = '1_{0}'.format(i) | |
queue.send_message(MessageBody=message_content, MessageGroupId='Group1') | |
for i in range(1,5): | |
message_content = '1_{0}'.format(i) | |
queue.send_message(MessageBody=message_content, MessageGroupId='Group2') | |
''' | |
# próbujemy stawić wiadomości o takiej samej treści | |
''' | |
message_content = 'Duplication test' | |
response = queue.send_message(MessageBody=message_content, MessageGroupId='Group1') | |
print('First request. Message Content: {0}, HTTP Status Code: {1}'.format(message_content, response['ResponseMetadata']['HTTPStatusCode'])) | |
response = queue.send_message(MessageBody=message_content, MessageGroupId='Group2') | |
print('Second request. Message Content: {0}, HTTP Status Code: {1}'.format(message_content, response['ResponseMetadata']['HTTPStatusCode'])) | |
''' | |
''' | |
queue_name = 'FifoQueue_duplicates.fifo' | |
# tworzymy kolejkę wiadomości FIFO bez deduplikacji | |
queue_create = sqs.create_queue(QueueName=queue_name, Attributes={'FifoQueue':'true', 'ContentBasedDeduplication': 'false'}) | |
queue = sqs.get_queue_by_name(QueueName=queue_name) | |
''' | |
# wysyłamy wiadomoścu do kolejki i dbamy o brak duplikatów | |
''' | |
message_content = 'Duplication test' | |
message_group_id = 'Group1' | |
message_deduplication_id = '{0}{1}'.format(message_group_id, message_content) | |
response = queue.send_message(MessageBody=message_content, MessageGroupId='Group1', MessageDeduplicationId=hashlib.md5(message_deduplication_id.encode('utf-8')).hexdigest()) | |
print('First request. Message Content: {0}, HTTP Status Code: {1}'.format(message_content, response['ResponseMetadata']['HTTPStatusCode'])) | |
message_group_id = 'Group2' | |
message_deduplication_id = '{0}{1}'.format(message_group_id, message_content) | |
response = queue.send_message(MessageBody=message_content, MessageGroupId='Group2', MessageDeduplicationId=hashlib.md5(message_deduplication_id.encode('utf-8')).hexdigest()) | |
print('Second request. Message Content: {0}, HTTP Status Code: {1}'.format(message_content, response['ResponseMetadata']['HTTPStatusCode'])) | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment