Yes that should work.
For this task, you want to consider adding a start() and stop() method on the ClientConsumer class.
First you need to tell the server consumer endpoint to stop and obtain the lastDeliveryID (like how is currently done in closing()).
Then wait for the delivery to arrive and wait for onMessages to execute, much as is currently done in close().
I have got it working locally but I will need to add some more tests to make sure it's implemented correctly. I had to collect all those lastDeliveryIDs into a map on the server side and send them back to the client in a ConnectionStopResponse packet that I created.
Do we really have to wait for the delivery lastDeliveryID to come in this case? Wouldn't it be possible for ClientConsumer to still accept messages as usual when stopped but not deliver them to receive() and onMessage()? Then Connection.stop() could tell the server side to stop, wait until every ClientConsumer finishes any onMessage calls it has already started and return afterwards?
Yes you're right.
There's no need to wait for the deliveries to arrive, just need to wait for onmessages to complete and make sure no more messages are returned to receive or sent to onMessage until it is restarted.