1
1
/*
2
- * Copyright 2002-2016 the original author or authors.
2
+ * Copyright 2002-2018 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .integration .jmx .config ;
18
18
19
- import java .util .ArrayList ;
20
19
import java .util .Collection ;
21
- import java .util .HashSet ;
22
- import java .util .List ;
20
+ import java .util .Queue ;
23
21
import java .util .Set ;
22
+ import java .util .concurrent .ConcurrentHashMap ;
23
+ import java .util .concurrent .ConcurrentLinkedQueue ;
24
+ import java .util .function .Consumer ;
24
25
25
26
import org .springframework .aop .support .AopUtils ;
26
27
import org .springframework .beans .BeansException ;
27
28
import org .springframework .beans .DirectFieldAccessor ;
28
- import org .springframework .beans .factory .config .BeanPostProcessor ;
29
+ import org .springframework .beans .factory .config .DestructionAwareBeanPostProcessor ;
29
30
import org .springframework .core .Ordered ;
30
31
import org .springframework .core .annotation .AnnotatedElementUtils ;
31
32
import org .springframework .integration .monitor .IntegrationMBeanExporter ;
40
41
*
41
42
* @author Oleg Zhurakousky
42
43
* @author Artem Bilan
44
+ *
43
45
* @since 2.1
44
46
*
45
47
*/
46
- class MBeanExporterHelper implements BeanPostProcessor , Ordered {
48
+ class MBeanExporterHelper implements DestructionAwareBeanPostProcessor , Ordered {
47
49
48
- private final List <MBeanExporter > mBeanExportersForExcludes = new ArrayList < MBeanExporter >();
50
+ private final Queue <MBeanExporter > mBeanExportersForExcludes = new ConcurrentLinkedQueue < >();
49
51
50
- private final Set <String > siBeanNames = new HashSet < String > ();
52
+ private final Set <String > siBeanNames = ConcurrentHashMap . newKeySet ();
51
53
52
54
@ Override
53
55
public Object postProcessBeforeInitialization (Object bean , String beanName ) throws BeansException {
54
56
if ("$autoCreateChannelCandidates" .equals (beanName )) {
55
57
@ 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" );
58
60
this .siBeanNames .addAll (autoCreateChannelCandidatesNames );
59
61
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 );
65
67
}
66
68
}
67
69
@@ -72,25 +74,37 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) thro
72
74
public Object postProcessAfterInitialization (Object bean , String beanName ) throws BeansException {
73
75
if (AnnotatedElementUtils .isAnnotated (AopUtils .getTargetClass (bean ),
74
76
IntegrationManagedResource .class .getName ())) {
77
+
75
78
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 ));
81
80
}
82
81
83
82
if (bean instanceof MBeanExporter && !(bean instanceof IntegrationMBeanExporter )) {
84
83
MBeanExporter mBeanExporter = (MBeanExporter ) bean ;
85
84
this .mBeanExportersForExcludes .add (mBeanExporter );
86
- for (String siBeanName : this .siBeanNames ) {
87
- mBeanExporter .addExcludedBean (siBeanName );
88
- }
85
+ this .siBeanNames .forEach (mBeanExporter ::addExcludedBean );
89
86
}
90
87
91
88
return bean ;
92
89
}
93
90
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
+
94
108
@ Override
95
109
public int getOrder () {
96
110
return Ordered .HIGHEST_PRECEDENCE ;
0 commit comments