很是簡單的springMVC接口異常監控

1、在applicationContent.xml配置javaMailjava

<bean id="javaMailSenderImpl" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="${郵箱服務地址}"></property>
    <property name="port" value="25"></property>
    <property name="username" value="${發送方郵箱}"></property>

    <property name="password" value="${密碼}"></property>
    <property name="javaMailProperties">
      <props>
        <prop key="mail.smtp.auth">true</prop>
        <prop key="mail.smtp.timeout">3000</prop>
      </props>
    </property>
</bean>

2、編寫發送郵件的組建spring

public class EmailService {
    private static final Logger log = LoggerFactory.getLogger(EmailService.class);

    @Autowired
    private JavaMailSenderImpl javaMailSender;

    /**
     * 發送郵件
     * @param subject
     * @param content
     * @param to
     */
    public void sendMail(String subject, String content, String... to) {
        log.info("send email subject:{}, to:{}, subject:{}", subject, to, content);
        MimeMessage objMimeMessage = javaMailSender.createMimeMessage();

        try {
            MimeMessageHelper helper = new MimeMessageHelper(objMimeMessage, true, "utf-8");
            try {
                helper.setFrom(javaMailSender.getUsername(),"XX異常");
            } catch (UnsupportedEncodingException e) {
                helper.setFrom(javaMailSender.getUsername());
            }
            helper.setSubject(subject);
            helper.setTo(to);
            helper.setText(content,true);
        } catch (MessagingException e) {
            log.error("發送郵件出錯.",e);
        }

        javaMailSender.send(objMimeMessage);
    }
}

3、配置異常捕獲類apache

/**
 * 自定義的全局異常處理類
 */
public class CustomMappingExceptionResolver extends SimpleMappingExceptionResolver implements HandlerExceptionResolver{

    private static Logger logger = LoggerFactory.getLogger(CustomMappingExceptionResolver.class);

    @Autowired
    private ExceptionCacheService exceptionCacheService;

    /**
     * 這裏異常信息記錄到cache中, 而後啓動一個任務去查詢cache中是否有異常信息;有則發送郵件
     * 若是不放cache能夠直接發送郵件
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @return
     */
    @Override
    protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
                                              Exception ex) {
        exceptionCacheService.doExceptionStore(ex);
        if (ex.getClass().getName().equals("org.apache.catalina.connector.ClientAbortException")) {

            return super.doResolveException(request, response, handler, ex);
        }

        logger.error("請求悲劇了!!", ex);

        if (WebUtils.isAjaxRequest(request)) {

            ResponseEntity entity = ResponseEntity.failed();
            entity.setMsg("服務器異常");

            try {
                WebUtils.writeJsonResponse(response, entity);
            } catch (IOException e) {
                logger.error("輸出json竟然也異常了!!", e);
            }
            return null;
        } else {
            return super.doResolveException(request, response, handler, ex);
        }
    }

    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
                                         Exception ex) {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("500");
        exceptionCacheService.doExceptionStore(ex);
        return mav;
    }
}

固然要配置該類json

<bean class="com.xxx.handler.CustomMappingExceptionResolver" />
相關文章
相關標籤/搜索