Shows how to subscribe queues to a FIFO topic.
()
| 174 | |
| 175 | # snippet-start:[python.example_code.sns.Scenario_SubscribeFifoTopic] |
| 176 | def usage_demo(): |
| 177 | """Shows how to subscribe queues to a FIFO topic.""" |
| 178 | print("-" * 88) |
| 179 | print("Welcome to the `Subscribe queues to a FIFO topic` demo!") |
| 180 | print("-" * 88) |
| 181 | |
| 182 | sns = boto3.resource("sns") |
| 183 | sqs = boto3.resource("sqs") |
| 184 | fifo_topic_wrapper = FifoTopicWrapper(sns) |
| 185 | sns_wrapper = SnsWrapper(sns) |
| 186 | |
| 187 | prefix = "sqs-subscribe-demo-" |
| 188 | queues = set() |
| 189 | subscriptions = set() |
| 190 | |
| 191 | wholesale_queue = sqs.create_queue( |
| 192 | QueueName=prefix + "wholesale.fifo", |
| 193 | Attributes={ |
| 194 | "MaximumMessageSize": str(4096), |
| 195 | "ReceiveMessageWaitTimeSeconds": str(10), |
| 196 | "VisibilityTimeout": str(300), |
| 197 | "FifoQueue": str(True), |
| 198 | "ContentBasedDeduplication": str(True), |
| 199 | }, |
| 200 | ) |
| 201 | queues.add(wholesale_queue) |
| 202 | print(f"Created FIFO queue with URL: {wholesale_queue.url}.") |
| 203 | |
| 204 | retail_queue = sqs.create_queue( |
| 205 | QueueName=prefix + "retail.fifo", |
| 206 | Attributes={ |
| 207 | "MaximumMessageSize": str(4096), |
| 208 | "ReceiveMessageWaitTimeSeconds": str(10), |
| 209 | "VisibilityTimeout": str(300), |
| 210 | "FifoQueue": str(True), |
| 211 | "ContentBasedDeduplication": str(True), |
| 212 | }, |
| 213 | ) |
| 214 | queues.add(retail_queue) |
| 215 | print(f"Created FIFO queue with URL: {retail_queue.url}.") |
| 216 | |
| 217 | analytics_queue = sqs.create_queue(QueueName=prefix + "analytics", Attributes={}) |
| 218 | queues.add(analytics_queue) |
| 219 | print(f"Created standard queue with URL: {analytics_queue.url}.") |
| 220 | |
| 221 | topic = fifo_topic_wrapper.create_fifo_topic("price-updates-topic.fifo") |
| 222 | print(f"Created FIFO topic: {topic.attributes['TopicArn']}.") |
| 223 | |
| 224 | for q in queues: |
| 225 | fifo_topic_wrapper.add_access_policy(q, topic.attributes["TopicArn"]) |
| 226 | |
| 227 | print(f"Added access policies for topic: {topic.attributes['TopicArn']}.") |
| 228 | |
| 229 | for q in queues: |
| 230 | sub = fifo_topic_wrapper.subscribe_queue_to_topic( |
| 231 | topic, q.attributes["QueueArn"] |
| 232 | ) |
| 233 | subscriptions.add(sub) |
no test coverage detected