标签:bsp 返回 dubbo eric str style else lis point
1 @Activate(group = Constants.PROVIDER) 2 public class ExceptionFilter implements Filter { 3 private static final Logger logger = LogManager.getLogger(ExceptionFilter.class); 4 5 public Result invoke(Invoker<?> invoker, Invocation invocation) { 6 Result result = null; 7 try { 8 result = invoker.invoke(invocation); 9 if (result.hasException() && GenericService.class != invoker.getInterface()) { 10 Throwable exception = result.getException(); 11 String data = String.format("\r\n[level]:Error,[createTime]:%s,[platform]:%s,[serviceName]:%s,[methodName]:%s,[inputParam]:%s", DateUtil.formatDateTime(new Date()), PlatformNameEnum.PAY, invoker.getInterface().getName(), invocation.getMethodName(), JSON.toJSONString(invocation.getArguments())); 12 logger.error(data, exception); 13 ResultVo resultVo = new ResultVo(false); 14 resultVo.setResultCode(PayCenterErrorCodeEnum.PAY_ERR_100000.getCode()); 15 resultVo.setResultMessage(PayCenterErrorCodeEnum.PAY_ERR_100000.getMsg()); 16 //出现异常,打印日志后返回错误码 17 return new RpcResult(resultVo); 18 } 19 } catch (RuntimeException e) { 20 String data = String.format("\r\n[level]:Error,[createTime]:%s,[platform]:%s,[serviceName]:%s,[methodName]:%s,[inputParam]:%s", DateUtil.formatDateTime(new Date()), PlatformNameEnum.PAY, invoker.getInterface().getName(), invocation.getMethodName(), JSON.toJSONString(invocation.getArguments())); 21 logger.error(data, e); 22 } 23 return result; 24 } 25 }
1 public class ExceptionAop { 2 private static final Logger logger = LogManager.getLogger(ExceptionAop.class); 3 4 public Object handlerControllerMethod(ProceedingJoinPoint pjp) { 5 long startTime = System.currentTimeMillis(); 6 7 ResultVo result; 8 9 try { 10 result = (ResultVo) pjp.proceed(); 11 logger.info(pjp.getSignature() + "use time:" + (System.currentTimeMillis() - startTime)); 12 } catch (Throwable e) { 13 result = handlerException(pjp, e); 14 } 15 16 return result; 17 } 18 19 private ResultVo handlerException(ProceedingJoinPoint pjp, Throwable e) { 20 ResultVo result = new ResultVo(false); 21 pjp.getArgs(); 22 23 // 已知异常 24 if (e instanceof BusinessException) { 25 result.setResultCode(Integer.parseInt(((BusinessException) e).getExceptionCode())); 26 result.setResultMessage(((BusinessException) e).getExceptionMsg()); 27 } else { 28 logger.error(pjp.getSignature() + " error ", e); 29 30 //TODO 未知的异常,应该格外注意,可以发送邮件通知等 31 result.setResultCode(PayCenterErrorCodeEnum.PAY_ERR_100000.getCode()); 32 result.setResultMessage(PayCenterErrorCodeEnum.PAY_ERR_100000.getMsg()); 33 } 34 35 return result; 36 } 37 }
1 <bean id="exceptionAop" class="com.zcz.pay.aop.ExceptionAop"/> 2 <aop:config> 3 <aop:pointcut id="target" 4 expression="execution(* com.zcz.pay.api..*.*(..))"/> 5 <aop:aspect id="myAop" ref="exceptionAop"> 6 <!-- 配置环绕通知 --> 7 <aop:around method="handlerControllerMethod" pointcut-ref="target"/> 8 </aop:aspect> 9 </aop:config>
标签:bsp 返回 dubbo eric str style else lis point
原文地址:http://www.cnblogs.com/zcz527/p/7655235.html