That should be quiet easy to accomplish. Following is an example demonstrating the usage. This example works as long as you have a naming convention, where your service beans in the configuration are ending with "Services". The email adress is retrieved from a JNDI service in websphere. You can easily just subtitute that with a hardcoded value in the configuration.
spring configuration
Code:
<!-- This bean can be activated for AOP based RuntimException interceptor -->
<bean name="runtimeExceptionInterceptor"
class="ch.xxx.xnet.fwk.common.interceptor.RuntimeExceptionInterceptor">
<property name="mailer">
<ref bean="commonMailer" />
</property>
<property name="toEmail">
<bean class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>FWK_EXCEPTION_EMAIL</value>
</property>
</bean>
</property>
</bean>
<bean
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<value>*Service</value>
</property>
<property name="interceptorNames">
<list>
<value>runtimeExceptionInterceptor</value>
</list>
</property>
</bean>
and the RuntimeExceptionInterceptor class looks like:
Code:
package ch.generali.xnet.fwk.common.interceptor;
import java.io.PrintWriter;
import java.io.StringWriter;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import ch.generali.xxxx.fwk.common.logging.ILogger;
import ch.generali.xxx.fwk.common.logging.LoggerImpl;
import ch.generali.xxx.fwk.exception.DataBaseBusinessException;
import ch.generali.xxx.fwk.mail.service.ICommonMailer;
public class RuntimeExceptionInterceptor implements MethodInterceptor {
private static final ILogger LOG = new LoggerImpl(RuntimeExceptionInterceptor.class);
private ICommonMailer mailer;
private String toEmail;
public void setToEmail(String toEmail) {
this.toEmail = toEmail;
}
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
try {
result = methodInvocation.proceed();
} catch (RuntimeException e) {
// send an email
}
return result;
}
public void setMailer(ICommonMailer mailer) {
this.mailer = mailer;
}
}