Skip to content

Commit da622af

Browse files
committed
Some clean up in the IntegrationMBeanExporter
1 parent be8c1e2 commit da622af

File tree

1 file changed

+39
-40
lines changed

1 file changed

+39
-40
lines changed

spring-integration-jmx/src/main/java/org/springframework/integration/monitor/IntegrationMBeanExporter.java

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -243,29 +243,28 @@ public void afterSingletonsInstantiated() {
243243

244244
this.singletonsInstantiated = true;
245245
}
246-
catch (RuntimeException e) {
246+
catch (RuntimeException ex) {
247247
unregisterBeans();
248-
throw e;
248+
throw ex;
249249
}
250250
}
251251

252252
private void populateMessageHandlers() {
253253
Map<String, MessageHandler> messageHandlers = this.applicationContext.getBeansOfType(MessageHandler.class);
254254
for (Entry<String, MessageHandler> entry : messageHandlers.entrySet()) {
255-
256255
String beanName = entry.getKey();
257256
MessageHandler bean = entry.getValue();
258-
if (this.handlerInAnonymousWrapper(bean) != null) {
257+
if (handlerInAnonymousWrapper(bean) != null) {
259258
if (logger.isDebugEnabled()) {
260259
logger.debug("Skipping " + beanName + " because it wraps another handler");
261260
}
262261
continue;
263262
}
264263
// If the handler is proxied, we have to extract the target to expose as an MBean.
265264
// The MetadataMBeanInfoAssembler does not support JDK dynamic proxies.
266-
MessageHandler monitor = (MessageHandler) extractTarget(bean);
267-
if (monitor instanceof IntegrationManagement) {
268-
this.handlers.put(beanName, (IntegrationManagement) monitor);
265+
MessageHandler monitor = extractTarget(bean);
266+
if (monitor instanceof IntegrationManagement management) {
267+
this.handlers.put(beanName, management);
269268
}
270269
}
271270
}
@@ -316,15 +315,15 @@ public Object postProcessAfterInitialization(Object bean, String beanName) throw
316315
if (this.singletonsInstantiated) {
317316
try {
318317
if (bean instanceof MessageChannel) {
319-
MessageChannel monitor = (MessageChannel) extractTarget(bean);
320-
if (monitor instanceof IntegrationManagement) {
321-
this.channels.put(beanName, (IntegrationManagement) monitor);
322-
registerChannel((IntegrationManagement) monitor);
318+
MessageChannel monitor = extractTarget(bean);
319+
if (monitor instanceof IntegrationManagement management) {
320+
this.channels.put(beanName, management);
321+
registerChannel(management);
323322
this.runtimeBeans.add(bean);
324323
}
325324
}
326-
else if (bean instanceof MessageProducer && bean instanceof Lifecycle) {
327-
registerProducer((MessageProducer) bean);
325+
else if (bean instanceof MessageProducer producer && bean instanceof Lifecycle) {
326+
registerProducer(producer);
328327
this.runtimeBeans.add(bean);
329328
}
330329
else if (bean instanceof AbstractEndpoint) {
@@ -341,19 +340,18 @@ else if (bean instanceof AbstractEndpoint) {
341340
private void postProcessAbstractEndpoint(Object bean) {
342341
if (bean instanceof IntegrationConsumer integrationConsumer) {
343342
MessageHandler handler = integrationConsumer.getHandler();
344-
MessageHandler monitor = (MessageHandler) extractTarget(handler);
345-
if (monitor instanceof IntegrationManagement) {
346-
registerHandler((IntegrationManagement) monitor, integrationConsumer);
347-
this.handlers.put(((IntegrationManagement) monitor).getComponentName(),
348-
(IntegrationManagement) monitor);
343+
MessageHandler monitor = extractTarget(handler);
344+
if (monitor instanceof IntegrationManagement management) {
345+
registerHandler(management, integrationConsumer);
346+
this.handlers.put(management.getComponentName(), management);
349347
this.runtimeBeans.add(monitor);
350348
}
351349
return;
352350
}
353351
else if (bean instanceof SourcePollingChannelAdapter pollingChannelAdapter) {
354352
MessageSource<?> messageSource = pollingChannelAdapter.getMessageSource();
355353
if (messageSource instanceof IntegrationInboundManagement) {
356-
IntegrationInboundManagement monitor = (IntegrationInboundManagement) extractTarget(messageSource);
354+
IntegrationInboundManagement monitor = extractTarget(messageSource);
357355
registerSource(monitor);
358356
this.sourceLifecycles.put(monitor, pollingChannelAdapter);
359357
this.runtimeBeans.add(monitor);
@@ -366,7 +364,7 @@ else if (bean instanceof SourcePollingChannelAdapter pollingChannelAdapter) {
366364
}
367365

368366
private void registerProducer(MessageProducer messageProducer) {
369-
Lifecycle target = (Lifecycle) extractTarget(messageProducer);
367+
Lifecycle target = extractTarget(messageProducer);
370368
if (!(target instanceof AbstractMessageProducingHandler)) {
371369
this.inboundLifecycleMessageProducers.add(target);
372370
}
@@ -387,22 +385,22 @@ public void postProcessBeforeDestruction(Object bean, String beanName) throws Be
387385
ObjectName objectName = this.objectNames.remove(bean);
388386
if (objectName != null) {
389387
doUnregister(objectName);
390-
if (bean instanceof AbstractEndpoint) {
391-
this.endpointNames.remove(((AbstractEndpoint) bean).getComponentName());
388+
if (bean instanceof AbstractEndpoint endpoint) {
389+
this.endpointNames.remove(endpoint.getComponentName());
392390
}
393391
else {
394392

395393
this.endpointsByMonitor.remove(bean);
396-
if (bean instanceof IntegrationManagement && bean instanceof MessageChannel) {
397-
this.channels.remove(((NamedComponent) bean).getComponentName());
394+
if (bean instanceof IntegrationManagement management && management instanceof MessageChannel) {
395+
this.channels.remove(management.getComponentName());
398396
}
399-
else if (bean instanceof IntegrationManagement && bean instanceof MessageHandler) {
400-
this.handlers.remove(((NamedComponent) bean).getComponentName());
401-
this.endpointNames.remove(((NamedComponent) bean).getComponentName());
397+
else if (bean instanceof IntegrationManagement management && management instanceof MessageHandler) {
398+
this.handlers.remove(management.getComponentName());
399+
this.endpointNames.remove(management.getComponentName());
402400
}
403-
else if (bean instanceof IntegrationInboundManagement && bean instanceof MessageSource) {
404-
this.sources.remove(((NamedComponent) bean).getComponentName());
405-
this.endpointNames.remove(((NamedComponent) bean).getComponentName());
401+
else if (bean instanceof IntegrationInboundManagement management && bean instanceof MessageSource) {
402+
this.sources.remove(management.getComponentName());
403+
this.endpointNames.remove(management.getComponentName());
406404
}
407405
}
408406
}
@@ -553,7 +551,7 @@ public void stopInboundMessageProducers() {
553551

554552
@ManagedOperation
555553
public void stopActiveChannels() {
556-
// Stop any "active" channels (JMS etc).
554+
// Stop any "active" channels (JMS etc.)
557555
for (IntegrationManagement metrics : this.channels.values()) {
558556
if (metrics instanceof Lifecycle) {
559557
if (logger.isInfoEnabled()) {
@@ -759,15 +757,16 @@ private boolean matches(String[] patterns, String name) {
759757
return match != null && match;
760758
}
761759

762-
private Object extractTarget(Object bean) {
760+
@SuppressWarnings("unchecked")
761+
private <T> T extractTarget(Object bean) {
763762
if (!(bean instanceof Advised advised)) {
764-
return bean;
763+
return (T) bean;
765764
}
766765
try {
767766
return extractTarget(advised.getTargetSource().getTarget());
768767
}
769-
catch (Exception e) {
770-
logger.error("Could not extract target", e);
768+
catch (Exception ex) {
769+
logger.error("Could not extract target", ex);
771770
return null;
772771
}
773772
}
@@ -979,11 +978,11 @@ private IntegrationInboundManagement buildMessageSourceMetricsIfAny(
979978
}
980979

981980
MessageChannel outputChannel = null;
982-
if (target instanceof MessagingGatewaySupport) {
983-
outputChannel = ((MessagingGatewaySupport) target).getRequestChannel();
981+
if (target instanceof MessagingGatewaySupport gatewaySupport) {
982+
outputChannel = gatewaySupport.getRequestChannel();
984983
}
985-
else if (target instanceof SourcePollingChannelAdapter) {
986-
outputChannel = ((SourcePollingChannelAdapter) target).getOutputChannel();
984+
else if (target instanceof SourcePollingChannelAdapter pollingChannelAdapter) {
985+
outputChannel = pollingChannelAdapter.getOutputChannel();
987986
}
988987

989988
if (outputChannel != null) {

0 commit comments

Comments
 (0)