用AOP实现业务service的重新调用(二)

 承接 用AOP实现业务service的重新调用(一),我们继续......

 

service重试的落地实现

       方案A: web业务系统里面有很多action,很多service,如果直接从每个调用service的点入手的话,修改点会很多,而且代码会大量冗余,实现代码并不复杂

 

try{
    //service调用
} catch(UncategorizedSQLException e) { 
    if(retry) {
        //service调用
    }
}

 

 

       方案B: 因为我们使用了spring,所以自然会想到AOP,通过拦截器来拦截service方法的调用,一旦发生UncategorizedSQLException,就重试一次service方法的调用.直接上代码:

 

import java.lang.reflect.Method;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.framework.ReflectiveMethodInvocation;
import org.springframework.dao.DataIntegrityViolationException;

public class ServiceRetryAdvice implements MethodInterceptor {
	
	private static ThreadLocal<Integer> retryNum = new ThreadLocal<Integer>() {  
        public Integer initialValue() {  
            return 0;  
        }  
    };

	public Object invoke(MethodInvocation invocation) throws Throwable {
				
		Object returnObject = null;
		try{
			returnObject = invocation.proceed();
		} catch(DataIntegrityViolationException e) {
			
			if (retryNum.get() == 0){
				retryNum.set(1);

				ReflectiveMethodInvocation refInvocation = (ReflectiveMethodInvocation)invocation;
				Object proxy = refInvocation.getProxy();
				Method method = refInvocation.getMethod();
				Object[] args = refInvocation.getArguments();
				
				returnObject = method.invoke(proxy, args);
			} else {
				throw e;
			}
		} finally {
			retryNum.remove();
		}
		return returnObject;
	}
}

 

关于代码的详细说明以及各种需要考虑到的方方面面,请参照

 

用AOP实现业务service的重新调用(三)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
业务操作记录的实现可以使用 AOP(面向切面编程)来实现,具体实现步骤如下: 1. 定义切点:定义需要记录操作记录的方法或类的切点。可以使用注解或表达式等方式指定切点。 ```java @Pointcut("execution(* com.example.service.*.*(..))") public void servicePointcut() {} ``` 2. 定义记录操作记录的切面:定义操作记录的切面,并在切面中完成记录操作记录的逻辑。可以使用注解或编程方式定义切面。 ```java @Aspect @Component public class OperationLogAspect { @Autowired private OperationLogService operationLogService; @Around("servicePointcut()") public Object around(ProceedingJoinPoint point) throws Throwable { // 获取当前用户信息 User currentUser = getCurrentUser(); // 获取方法名 String methodName = point.getSignature().getName(); // 获取方法参数 Object[] args = point.getArgs(); // 执行目标方法 Object result = point.proceed(); // 记录操作记录 operationLogService.saveOperationLog(currentUser, methodName, args); return result; } private User getCurrentUser() { // 获取当前用户信息的逻辑 } } ``` 3. 配置 AOP:在 Spring 配置文件中配置 AOP,将切点和切面进行关联。 ```xml <aop:aspectj-autoproxy/> <bean id="operationLogAspect" class="com.example.aspect.OperationLogAspect"/> <aop:config> <aop:aspect ref="operationLogAspect"> <aop:around pointcut-ref="servicePointcut" method="around"/> </aop:aspect> </aop:config> ``` 通过以上步骤,就可以使用 AOP 实现业务操作记录。当调用指定方法时,会自动触发切面的逻辑,完成操作记录的记录。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值