Do I need to close my JCSMP consumer/session on application shutdown?

petegehrman Member Posts: 43 ✭✭
edited October 2021 in General Discussions #1

I generally like to close resources gracefully when shutting down an application, and ran into a scenario where my queue consumer still had an outstanding message that it tried to ack() after the consumer had been closed, so I got the message "Attempted an operation on a closed message consumer." If upon shutdown I instead just stop() the consumer so that it doesn't consume anymore, and let anything outstanding get acked, is there any harm in not explicitly closing the consumer & session? They will obviously be forcefully closed when my application is terminated.

Otherwise, I feel like I'd need to track the count of unacked messages, and upon acking always check if it's the last one and then close the connection...



  • TomF
    TomF Member, Employee Posts: 408 Solace Employee

    Hi @petegehrman, a great question. You're correct in that there's no harm in not explicitly calling close() on shutdown, provided you've dealt with acknowledgements correctly. Like you though, I feel it's always better to explicitly clean things up.

    It's important to make sure that if you have received a message, you call ack() before you close the flow. Otherwise, the message will be redelivered when your consumer restarts. In a way, you need to ensure all message processing has finished before closing the flow. If you call stop() then close() quickly afterwards, you could still end up trying to ack messages belonging to a closed flow.

    Your idea of calling stop() first, waiting until messages are acked (so yes, keeping a track of them), and then calling close is probably a best practice.