I am trying to listen to RabbitMQ queue by polling it. But somehow due to network issues if once connection to queue is lost then thread silently dies off and connection and everything is closed. But this is a background task and we wont know untill queue really grows huge and start send out notification.
Can some one please help me with graceful shutdown of thread (which I guess I am already doing by dealing carefully with exception in catch clause). But I don’t know how to re-start a stopped thread.
Is there a way through which I can restart a new instance of stopped thread.
PS: I am instantiating the thread using @postconstruct and calling init thread soon after container loads all beans.
This sounds to me that you are not doing proper exception handling. You say “thread silently dies off” but that doesn’t happen in Java. I would audit your exceptions and watch for the following problems:
throws Exceptionon a method. This hides all sorts of evils. A method should usually enumerate the exceptions that it throws.try { ... } catch (Exception e) { ... }. That, again, hides evils.e.printStackTrace()may work but providing more information about the problem is usually in order.You do not re-start a stopped thread, you start another one. If the thread should not be shutting down at all then it needs to re-open a socket or maybe re-start its
RabbitMQconnection. Again, it’s about proper exception handling. I don’t knowRabbitMQbut something like the following pseudo code might help:Best of luck.