Skip to content

Commit 7b502c6

Browse files
authored
AMQ-9843 Handle InstanceNotFoundException during MBean unregistration to avoid errors on shutdown (#1635)
1 parent 35e8b48 commit 7b502c6

1 file changed

Lines changed: 18 additions & 6 deletions

File tree

activemq-broker/src/main/java/org/apache/activemq/broker/jmx/ManagementContext.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,16 @@ public void stop() throws Exception {
203203
// unregister the mbeans we have registered
204204
if (mbeanServer != null) {
205205
for (Map.Entry<ObjectName, ObjectName> entry : registeredMBeanNames.entrySet()) {
206-
ObjectName actualName = entry.getValue();
206+
final ObjectName actualName = entry.getValue();
207207
if (actualName != null && beanServer.isRegistered(actualName)) {
208-
LOG.debug("Unregistering MBean {}", actualName);
209-
mbeanServer.unregisterMBean(actualName);
208+
try {
209+
LOG.debug("Unregistering MBean {}", actualName);
210+
mbeanServer.unregisterMBean(actualName);
211+
} catch (javax.management.InstanceNotFoundException e) {
212+
// Ignore - the MBean was already unregistered (likely by advisory cleanup)
213+
// This is a benign race condition during shutdown
214+
LOG.trace("MBean already unregistered: {}", actualName);
215+
}
210216
}
211217
}
212218
}
@@ -449,10 +455,16 @@ public ObjectInstance getObjectInstance(ObjectName name) throws InstanceNotFound
449455
* Unregister an MBean
450456
*/
451457
public void unregisterMBean(ObjectName name) throws JMException {
452-
ObjectName actualName = this.registeredMBeanNames.get(name);
458+
final ObjectName actualName = this.registeredMBeanNames.get(name);
453459
if (beanServer != null && actualName != null && beanServer.isRegistered(actualName) && this.registeredMBeanNames.remove(name) != null) {
454-
LOG.debug("Unregistering MBean {}", actualName);
455-
beanServer.unregisterMBean(actualName);
460+
try {
461+
LOG.debug("Unregistering MBean {}", actualName);
462+
beanServer.unregisterMBean(actualName);
463+
} catch (javax.management.InstanceNotFoundException e) {
464+
// Ignore - the MBean was already unregistered (race condition during concurrent cleanup)
465+
// This is benign since the MBean is already gone, which is what we wanted
466+
LOG.trace("MBean already unregistered: {}", actualName);
467+
}
456468
}
457469
}
458470

0 commit comments

Comments
 (0)