We have a Spring application responsible for doing some integration work that is expected to be up and running 24 x 7.
One of the integration patterns we use is JMS messaging using a bunch of clustered IBM MQ queue managers. With clustered IBM MQ queue managers if one of the queue manager goes down you need to reinstantiate the Queue Connection Factory beans in order for the connectivity to work with the remaining queue managers that are still up.
We put in place JMS health checks and the status of the IBM MQ queue managers changes we programatically restart the spring context and as a result the Queue Connection Factory beans get recreated and everything works just fine.
However this is not ideal for us because other parts of our application not related to IBM MQ works just fine and actually can be in the middle of processing stuff. Because of this we would prefer to only recreate the JMS context, pretty much recreating the Queue Connection Factory beans and a few other beans that depend on those (message listener containers, JmsTemplats, etc) rather than restarting the whole application context.
I found this post how-to-reinitialize-a-spring-bean and tried to implement solution 2)Delete & Register bean in registry but I could not get it to work.
// If I call below:
beanFactory.destroySingleton(qcfName);
var qcf = jmsConfig.connectionFactory();
beanFactory.registerSingleton(qcfName, qcf);
// I get this exception:
Could not register object [org.springframework.cloud.sleuth.instrument.messaging.LazyConnectionFactory@5a0fb814] under bean name 'connectionFactory':
there is already object [org.springframework.cloud.sleuth.instrument.messaging.LazyConnectionFactory@5a0fb814] bound
// If I call below:
beanFactory.destroySingleton(qcfName);
beanFactory.removeBeanDefinition(qcfName);
var qcf = jmsConfig.connectionFactory();
beanFactory.registerSingleton(qcfName, qcf);
// I am getting this exception:
No bean named 'connectionFactory' available
I tried some other permutations such as destroyBean
but I could not get it to work.
Any idea what I am doing wrong and how should I fix it? Thank you in advance for your inputs.