Results 1 to 3 of 3

Thread: Sending an email when an exception occurs.

  1. #1
    Join Date
    Apr 2009
    Posts
    1

    Default Sending an email when an exception occurs.

    Hi,
    I have a scenario that any exception thrown by the bean configured in the application context should be caught and an email should be send containing the stack trace.

  2. #2

    Default

    You can intercept the exception with AOP .

  3. #3

    Default

    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;
        }
    
        
        
    }

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •