测试
通过一个用户相关的业务方法来测试上面的代码
接口定义:
package org.java.base.springretry;
public interface UserService {
void add() throws Exception;
void query() throws Exception;
}
复制代码
接口实现:
package org.java.base.springretry;
public class UserServiceImpl implements UserService {
@Override
public void add() throws Exception {
System.out.println("添加用户。。。");
throw new RuntimeException();
}
@Override
@Retryable(maxAttemps = 3)
public void query() {
System.out.println("查询用户。。。");
throw new RuntimeException();
}
}复制代码
测试:
package org.java.base.springretry;
public class TestRetry {
public static void main(String[] args) throws Exception{
UserServiceImpl user = new UserServiceImpl();
//SpringRetry代理测试
SpringRetryProxy springRetryProxy = new SpringRetryProxy();
UserService u = (UserService)springRetryProxy.newProxyInstance(user);
//u.add();//失败不重试
u.query();//失败重试
}
}
复制代码
add方法不添加重试注解,程序异常结束,query方法添加重试注解,设置重试3次,运行效果如下 |