What I want is to stop this from another thread
Personal advice: don't do it.
From the information gathered in the comments, it appears that you are separating the message receival from its treatment. My suggestion is to merge both; and use an ExecutorService
to wrap them both.
When you need to shut the whole thing down, call .shutDown()
on the ExecutorService
: it means that no other task can be submitted to that execution.
As the receival/treatment process is now merged, it means you can never have a state in which a message is received but cannot be treated anymore.