Hey @Sathishkumar , looking at your code, the most obvious thing is that your sendMessage()
method exits before waiting to receive the ACK from the broker. You’ve defined all the connection info, the Session, etc. inside this method… when the method returns after you send, the Session variable loses scope and is torn down… so the ACKs or NACKs will never arrive. Remember that the send()
call is asynchronous… the method returns once the message has been written to the socket, not once it has be confirmed by the broker. You have to stick around to wait for the broker response. You could implement a mechanism using concurrency objects like a CyclicBarrier
to coordinate between the thread that is publishing and the ACK/NACK callbacks.
Typically most apps will connect when they start up, and leave the connection up for the lifetime of the application, and send/receive many messages on that Session. Creating a brand new Session for every single messages would be very very slow performance wise due to the complexity of establishing a new TCP session, TLS, then SMF protocol. So: move all your connection out of this method, keep the Session and Producer as instance variables (or static class variables?) and have the sendMessage()
method just reference them.