@@ -944,11 +944,16 @@ private MessageHandlerMetrics enhanceHandlerMonitor(MessageHandlerMetrics monito
944
944
endpoint = null ;
945
945
}
946
946
}
947
- return buildMessageHandlerMetrics (monitor , name , endpointName , source , endpoint );
947
+
948
+ MessageHandlerMetrics messageHandlerMetrics = buildMessageHandlerMetrics (monitor , name , source , endpoint );
949
+ if (endpointName != null ) {
950
+ this .endpointsByMonitor .put (messageHandlerMetrics , endpointName );
951
+ }
952
+ return messageHandlerMetrics ;
948
953
}
949
954
950
955
private MessageHandlerMetrics buildMessageHandlerMetrics (MessageHandlerMetrics monitor ,
951
- String name , String endpointName , String source , IntegrationConsumer endpoint ) {
956
+ String name , String source , IntegrationConsumer endpoint ) {
952
957
953
958
MessageHandlerMetrics result = monitor ;
954
959
String managedType = source ;
@@ -961,20 +966,7 @@ private MessageHandlerMetrics buildMessageHandlerMetrics(MessageHandlerMetrics m
961
966
if (managedName != null && name .startsWith (SI_PACKAGE )) {
962
967
MessageChannel inputChannel = endpoint .getInputChannel ();
963
968
if (inputChannel != null ) {
964
- if (!this .anonymousHandlerCounters .containsKey (inputChannel )) {
965
- this .anonymousHandlerCounters .put (inputChannel , new AtomicLong ());
966
- }
967
- AtomicLong count = this .anonymousHandlerCounters .get (inputChannel );
968
- long total = count .incrementAndGet ();
969
- String suffix = "" ;
970
- /*
971
- * Short hack to makes sure object names are unique if more than one endpoint has the same input
972
- * channel
973
- */
974
- if (total > 1 ) {
975
- suffix = "#" + total ;
976
- }
977
- managedName = inputChannel + suffix ;
969
+ managedName = buildAnonymousManagedName (this .anonymousHandlerCounters , inputChannel );
978
970
managedType = "anonymous" ;
979
971
}
980
972
}
@@ -993,15 +985,21 @@ private MessageHandlerMetrics buildMessageHandlerMetrics(MessageHandlerMetrics m
993
985
managedType = "handler" ;
994
986
}
995
987
996
- if (endpointName != null ) {
997
- this .endpointsByMonitor .put (monitor , endpointName );
998
- }
999
-
1000
988
result .setManagedType (managedType );
1001
989
result .setManagedName (managedName );
1002
990
return result ;
1003
991
}
1004
992
993
+ private String buildAnonymousManagedName (Map <Object , AtomicLong > anonymousCache , MessageChannel messageChannel ) {
994
+ AtomicLong count = anonymousCache .computeIfAbsent (messageChannel , (key ) -> new AtomicLong ());
995
+ long total = count .incrementAndGet ();
996
+ /*
997
+ * Short hack to makes sure object names are unique if more than one endpoint has the same input
998
+ * channel
999
+ */
1000
+ return messageChannel + (total > 1 ? "#" + total : "" );
1001
+ }
1002
+
1005
1003
/**
1006
1004
* Wrap the monitor in a lifecycle so it exposes the start/stop operations
1007
1005
*/
@@ -1076,26 +1074,34 @@ private MessageSourceMetrics enhanceSourceMonitor(MessageSourceMetrics monitor)
1076
1074
name = getInternalComponentName (name );
1077
1075
source = "internal" ;
1078
1076
}
1079
- return buildMessageSourceMetricsIfAny (monitor , name , endpointName , source , endpoint );
1077
+
1078
+ MessageSourceMetrics messageSourceMetrics = buildMessageSourceMetricsIfAny (monitor , name , source , endpoint );
1079
+ if (endpointName != null ) {
1080
+ this .endpointsByMonitor .put (messageSourceMetrics , endpointName );
1081
+ }
1082
+ return messageSourceMetrics ;
1080
1083
}
1081
1084
1082
1085
private MessageSourceMetrics buildMessageSourceMetricsIfAny (MessageSourceMetrics monitor , String name ,
1083
- String endpointName , String source , Object endpoint ) {
1086
+ String source , Object endpoint ) {
1084
1087
1085
1088
MessageSourceMetrics result = monitor ;
1086
- if (name != null && name .startsWith (SI_PACKAGE )) {
1089
+ String managedType = source ;
1090
+ String managedName = name ;
1091
+
1092
+ if (managedName != null && managedName .startsWith (SI_PACKAGE )) {
1087
1093
Object target = endpoint ;
1088
1094
if (endpoint instanceof Advised ) {
1089
1095
TargetSource targetSource = ((Advised ) endpoint ).getTargetSource ();
1090
1096
try {
1091
1097
target = targetSource .getTarget ();
1092
1098
}
1093
1099
catch (Exception e ) {
1094
- logger .error ("Could not get handler from bean = " + name );
1100
+ logger .error ("Could not get handler from bean = " + managedName );
1095
1101
}
1096
1102
}
1097
1103
1098
- Object outputChannel = null ;
1104
+ MessageChannel outputChannel = null ;
1099
1105
if (target instanceof MessagingGatewaySupport ) {
1100
1106
outputChannel = ((MessagingGatewaySupport ) target ).getRequestChannel ();
1101
1107
}
@@ -1104,39 +1110,22 @@ else if (target instanceof SourcePollingChannelAdapter) {
1104
1110
}
1105
1111
1106
1112
if (outputChannel != null ) {
1107
- if (!this .anonymousSourceCounters .containsKey (outputChannel )) {
1108
- this .anonymousSourceCounters .put (outputChannel , new AtomicLong ());
1109
- }
1110
- AtomicLong count = this .anonymousSourceCounters .get (outputChannel );
1111
- long total = count .incrementAndGet ();
1112
- String suffix = "" ;
1113
- /*
1114
- * Short hack to makes sure object names are unique if more than one endpoint has the same input
1115
- * channel
1116
- */
1117
- if (total > 1 ) {
1118
- suffix = "#" + total ;
1119
- }
1120
- name = outputChannel + suffix ;
1121
- source = "anonymous" ;
1113
+ managedName = buildAnonymousManagedName (this .anonymousSourceCounters , outputChannel );
1114
+ managedType = "anonymous" ;
1122
1115
}
1123
1116
}
1124
1117
1125
1118
if (endpoint instanceof Lifecycle ) {
1126
1119
result = wrapMessageSourceInLifecycleMetrics (result , endpoint );
1127
1120
}
1128
1121
1129
- if (name == null ) {
1130
- name = result .toString ();
1131
- source = "source" ;
1132
- }
1133
-
1134
- if (endpointName != null ) {
1135
- this .endpointsByMonitor .put (result , endpointName );
1122
+ if (managedName == null ) {
1123
+ managedName = result .toString ();
1124
+ managedType = "source" ;
1136
1125
}
1137
1126
1138
- result .setManagedType (source );
1139
- result .setManagedName (name );
1127
+ result .setManagedType (managedType );
1128
+ result .setManagedName (managedName );
1140
1129
return result ;
1141
1130
}
1142
1131
0 commit comments