I have a Spring service named EmailService
like this:
@Service
public class EmailService {
public void send (String recipient, String content) {
// send out an email
}
}
There are several other Spring services using EmailService
, e.g. PaymentService
uses EmailService
to send emails to customers about their payments and RetailerReportService
uses EmailService
to send emails to retailers some selling reports.
I would like to know the amount of usage for EmailService
and who are using it. Specifically, I want a bar chart that says "PaymentService
called EmailService
for 300 times a day" and "RetailerReportService
called EmailService
for 20 times a day".
Therefore, I want to add some code inside EmailService
to monitor who are calling it. The first thing came to my mind is to retrieve the call stack with Thread.currentThread().getStackTrace()
. However, since my application is using Spring framework, the call stack is full of proxy classes like org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint()
or sun.reflect.NativeMethodAccessorImpl.invoke0()
. Still, I can traverse the call stack and filter out unwanted classes by package names, but it would look ugly.
I would like to know if Spring provides a way to retrieve the caller Spring bean's name, so that I can use it inside EmailService
to get names like paymentService
or retailerReportService
. Thank you.