11/*
2- * Copyright 2002-2016 the original author or authors.
2+ * Copyright 2002-2018 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.
1616
1717package org .springframework .integration .jmx .config ;
1818
19- import java .util .ArrayList ;
2019import java .util .Collection ;
21- import java .util .HashSet ;
22- import java .util .List ;
20+ import java .util .Queue ;
2321import java .util .Set ;
22+ import java .util .concurrent .ConcurrentHashMap ;
23+ import java .util .concurrent .ConcurrentLinkedQueue ;
24+ import java .util .function .Consumer ;
2425
2526import org .springframework .aop .support .AopUtils ;
2627import org .springframework .beans .BeansException ;
2728import org .springframework .beans .DirectFieldAccessor ;
28- import org .springframework .beans .factory .config .BeanPostProcessor ;
29+ import org .springframework .beans .factory .config .DestructionAwareBeanPostProcessor ;
2930import org .springframework .core .Ordered ;
3031import org .springframework .core .annotation .AnnotatedElementUtils ;
3132import org .springframework .integration .monitor .IntegrationMBeanExporter ;
4041 *
4142 * @author Oleg Zhurakousky
4243 * @author Artem Bilan
44+ *
4345 * @since 2.1
4446 *
4547 */
46- class MBeanExporterHelper implements BeanPostProcessor , Ordered {
48+ class MBeanExporterHelper implements DestructionAwareBeanPostProcessor , Ordered {
4749
48- private final List <MBeanExporter > mBeanExportersForExcludes = new ArrayList < MBeanExporter >();
50+ private final Queue <MBeanExporter > mBeanExportersForExcludes = new ConcurrentLinkedQueue < >();
4951
50- private final Set <String > siBeanNames = new HashSet < String > ();
52+ private final Set <String > siBeanNames = ConcurrentHashMap . newKeySet ();
5153
5254 @ Override
5355 public Object postProcessBeforeInitialization (Object bean , String beanName ) throws BeansException {
5456 if ("$autoCreateChannelCandidates" .equals (beanName )) {
5557 @ SuppressWarnings ("unchecked" )
56- Collection <String > autoCreateChannelCandidatesNames = ( Collection < String >) new DirectFieldAccessor ( bean )
57- .getPropertyValue ("channelNames" );
58+ Collection <String > autoCreateChannelCandidatesNames =
59+ ( Collection < String >) new DirectFieldAccessor ( bean ) .getPropertyValue ("channelNames" );
5860 this .siBeanNames .addAll (autoCreateChannelCandidatesNames );
5961 if (!this .mBeanExportersForExcludes .isEmpty ()) {
60- for ( String autoCreateChannelCandidatesName : autoCreateChannelCandidatesNames ) {
61- for ( MBeanExporter mBeanExporter : this . mBeanExportersForExcludes ) {
62- mBeanExporter . addExcludedBean ( autoCreateChannelCandidatesName );
63- }
64- }
62+ autoCreateChannelCandidatesNames
63+ . stream ().
64+ < Consumer <? super MBeanExporter >> map ( candidateName ->
65+ mBeanExporter -> mBeanExporter . addExcludedBean ( candidateName ))
66+ . forEach ( this . mBeanExportersForExcludes :: forEach );
6567 }
6668 }
6769
@@ -72,25 +74,37 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) thro
7274 public Object postProcessAfterInitialization (Object bean , String beanName ) throws BeansException {
7375 if (AnnotatedElementUtils .isAnnotated (AopUtils .getTargetClass (bean ),
7476 IntegrationManagedResource .class .getName ())) {
77+
7578 this .siBeanNames .add (beanName );
76- if (!this .mBeanExportersForExcludes .isEmpty ()) {
77- for (MBeanExporter mBeanExporter : this .mBeanExportersForExcludes ) {
78- mBeanExporter .addExcludedBean (beanName );
79- }
80- }
79+ this .mBeanExportersForExcludes .forEach (mBeanExporter -> mBeanExporter .addExcludedBean (beanName ));
8180 }
8281
8382 if (bean instanceof MBeanExporter && !(bean instanceof IntegrationMBeanExporter )) {
8483 MBeanExporter mBeanExporter = (MBeanExporter ) bean ;
8584 this .mBeanExportersForExcludes .add (mBeanExporter );
86- for (String siBeanName : this .siBeanNames ) {
87- mBeanExporter .addExcludedBean (siBeanName );
88- }
85+ this .siBeanNames .forEach (mBeanExporter ::addExcludedBean );
8986 }
9087
9188 return bean ;
9289 }
9390
91+ @ Override
92+ public boolean requiresDestruction (Object bean ) {
93+ return (bean instanceof MBeanExporter && !(bean instanceof IntegrationMBeanExporter )) ||
94+ AnnotatedElementUtils .isAnnotated (AopUtils .getTargetClass (bean ),
95+ IntegrationManagedResource .class .getName ());
96+ }
97+
98+ @ Override
99+ public void postProcessBeforeDestruction (Object bean , String beanName ) throws BeansException {
100+ if (bean instanceof MBeanExporter ) {
101+ this .mBeanExportersForExcludes .remove (bean );
102+ }
103+ else {
104+ this .siBeanNames .remove (beanName );
105+ }
106+ }
107+
94108 @ Override
95109 public int getOrder () {
96110 return Ordered .HIGHEST_PRECEDENCE ;
0 commit comments