I don't think the API is flawed necessarily. Obviously it could have been designed a different way, but I believe the solution to your alleged problem comes from the start
method on the Connection
object (inherited via Startable
). The documentation for Connection
states:
A CMS client typically creates a connection, one or more sessions, and a number of message producers and consumers. When a connection is created, it is in stopped mode. That means that no messages are being delivered.
It is typical to leave the connection in stopped mode until setup is complete (that is, until all message consumers have been created). At that point, the client calls the connection's start method, and messages begin arriving at the connection's consumers. This setup convention minimizes any client confusion that may result from asynchronous message delivery while the client is still in the process of setting itself up.
A connection can be started immediately, and the setup can be done afterwards. Clients that do this must be prepared to handle asynchronous message delivery while they are still in the process of setting up.
This is the same pattern that JMS follows.
In any case I don't think there's any risk of message loss regardless of when you invoke start()
. If the consumer is using an auto-acknowledge mode then messages should only be automatically acknowledged once they are delivered synchronously via one of the receive methods or asynchronously through the listener's onMessage
. To do otherwise would be a bug in my estimation. I've worked with JMS for the last 10 years on various implementations and I've never seen any kind of condition where messages were lost related to this.
If you want to add consumers after you've already invoked start()
you could certainly call stop()
first, but I don't see any problem with simply adding them on the fly.
SimpleAsyncConsumer
example in the activemq-cpp distro is an utter horror show, a classic of bad wannabe C++. It looks like the work of a Java programmer trying to write C++. – arayq2ActiveMQConnectionFactory::createConnection()
returning acms::Connection*
, gratuitously losing the subclass type that then would need adynamic_cast
to recover in client code... sigh – arayq2