20
20
import io .micrometer .observation .Observation .Event ;
21
21
import io .micrometer .observation .ObservationRegistry ;
22
22
23
+ import java .util .Map ;
24
+ import java .util .Optional ;
25
+ import java .util .concurrent .ConcurrentHashMap ;
23
26
import java .util .function .Supplier ;
24
27
25
28
import org .springframework .context .ApplicationEvent ;
26
29
import org .springframework .context .ApplicationListener ;
27
30
import org .springframework .context .PayloadApplicationEvent ;
31
+ import org .springframework .modulith .core .ApplicationModule ;
28
32
import org .springframework .modulith .observability .support .ModulithObservations .Events ;
29
33
import org .springframework .modulith .runtime .ApplicationModulesRuntime ;
30
34
import org .springframework .util .Assert ;
@@ -39,6 +43,8 @@ public class ModuleEventListener implements ApplicationListener<ApplicationEvent
39
43
private final Supplier <MeterRegistry > meterRegistry ;
40
44
private final CrossModuleEventCounterFactory factory ;
41
45
46
+ private final Map <Class <?>, Optional <ApplicationModule >> modulesByType ;
47
+
42
48
/**
43
49
* Creates a new {@link ModuleEventListener} for the given {@link ApplicationModulesRuntime} and
44
50
* {@link ObservationRegistry} and {@link MeterRegistry}.
@@ -61,6 +67,8 @@ public ModuleEventListener(ApplicationModulesRuntime runtime,
61
67
this .observationRegistry = observationRegistrySupplier ;
62
68
this .meterRegistry = meterRegistrySupplier ;
63
69
this .factory = counterFactory ;
70
+
71
+ this .modulesByType = new ConcurrentHashMap <>();
64
72
}
65
73
66
74
/*
@@ -81,8 +89,7 @@ public void onApplicationEvent(ApplicationEvent event) {
81
89
return ;
82
90
}
83
91
84
- var moduleByType = runtime .get ()
85
- .getModuleByType (payloadType .getSimpleName ())
92
+ var moduleByType = modulesByType .computeIfAbsent (payloadType , it -> runtime .get ().getModuleByType (it ))
86
93
.orElse (null );
87
94
88
95
if (moduleByType == null ) {
0 commit comments