简体   繁体   中英

AWS SQSConnection multiple queues

I have a use-case where my application needs to subscribe to multiple SQS queues and listen to the events. I'm following the official AWS Docs to use SQS.

However, the example in the docs only cover the case of subscribing to one queue. There the flow is basically the following.

// 1) create the connection
val connection = factory.createConnection()

// 2) create the session from the connection
val session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE)

// 3) create the queue object from the session
val queue = session.createQueue("my-q-1")

// 4) create a consumer for that queue
val consumer = session.createConsumer(queue)

// 5) start the connection
connection.start()

This is simple enough. However, what if I want to subscribe to two queues?

  1. Do I need 2 connections?
  2. Or do I need 1 connection but 2 sessions?
  3. Or do I need 1 connection, 1 session, but 2 consumers on the same session?

Any experience around this would really help. Maybe JMS is not the best choice either.

If you're going to use the simplest JMS pattern then you can use 1 connection, 1 session, and 2 consumers.

However, if you're going to use JMS's MessageListener and you want the best performance you'll want to use 1 connection, 2 sessions, 1 consumer on each session, and 1 MessageListener on each consumer. This is because JMS sessions are not thread-safe and each MessageListener will be invoked asynchronously so if you want the listeners to be able to receive messages concurrently you'll want a session for each.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM