我的Java应用程序需要在远程调用失败时使用重试逻辑。 这些远程呼叫是:
此外,重试逻辑可能具有不同的重试间隔和不同的重试尝试。
我需要一个通用的retry()实现,它可以根据调用的位置进行适当的方法调用。下面是我正在寻找的简单代码示例。我知道我们可以尝试使用java反射来实现这一点,但是,是否有一个可以读取使用的框架或开源?
try {
ClassA objA = remoteServiceA.call(paramA1, paramA2, ...);
} catch (Exception e){
ClassA objA = (ClassA)retry(remoteService, listOfParams, ..); // generic method call
}
..
try {
ClassB objB = remoteServiceB.call(paramB1, paramB2, ...);
} catch (Exception e){
ClassA objB = (ClassB)retry(remoteService, listOfParams, ..); // generic method call
}
答案 0 :(得分:10)
如前所述,您应该使用AOP和Java注释。我会推荐jcabi-aspects(我是开发人员)的读取机制:
@RetryOnFailure(attempts = 3, delay = 5)
public String load(URL url) {
return url.openConnection().getContent();
}
另请阅读此博文:http://www.yegor256.com/2014/08/15/retry-java-method-on-exception.html
答案 1 :(得分:1)
这是一个书籍示例,其中可以使用aspectj(或一般aop),请参阅8.2.7 Example in Spring documentation和5 Reasons Java Developers Should Learn and Use AspectJ。
基本上,一个方面拦截对给定方法的所有调用(使用注释,命名约定,等等)和重试。
答案 2 :(得分:1)
enter link description here Spring有一个重试注释,用于实现目的
Step 1: Add following dependency to your POM
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
<version>1.1.5.RELEASE</version>
</dependency>
Step 2: Enabling Spring Retry
To enable Spring Retry in an application, we need to add the @EnableRetry annotation to our @Configuration class:
Ex:
@Configuration
@EnableRetry
public class AppConfig { ... }
Step 3: To add retry functionality to methods, @Retryable can be used:
Ex:
@Service
public interface MyService {
@Retryable(
value = { SQLException.class },
maxAttempts = 2,
backoff = @Backoff(delay = 5000))
void retryService(String sql) throws SQLException;
...
}
Step 4.The @Recover annotation is used to define a separate recovery method when a @Retryable method fails with a specified exception:
Ex:
@Service
public interface MyService {
...
@Recover
void recover(SQLException e, String sql);
}
See Url for more details : http://www.baeldung.com/spring-retry
答案 3 :(得分:0)
您从哪里获得服务?使用工厂代理从原始工厂获得的服务。然后,代理可以透明地实现重试。请参阅反射中的Java Proxy / ProxyGenerators。
答案 4 :(得分:0)
假设您有一个方法,需要每500毫秒和最多5次进行修复。 当前班级:
public class RemoteCaller{
Service serviceCaller;
public void remoteCall(String message) {
serviceCaller.updateDetails( this.message);
return null;
}
}
修改后的方法:
public class RetriableHelper<T> implements Callable<T> {
private Callable<T> task;
private int numberOfRetries;
private int numberOfTriesLeft;
private long timeToWait;
public RetriableHelper(int numberOfRetries, long timeToWait, Callable<T> task) {
this.numberOfRetries = numberOfRetries;
numberOfTriesLeft = numberOfRetries;
this.timeToWait = timeToWait;
this.task = task;
}
public T call() throws Exception {
while (true) {
try {
return task.call();
} catch (InterruptedException e) {
throw e;
} catch (CancellationException e) {
throw e;
} catch (Exception e) {
numberOfTriesLeft--;
if (numberOfTriesLeft == 0) {
throw e;
}
Thread.sleep(timeToWait);
}
}
}
}
Backend system/remote call class:
public class RemoteCaller{
Service serviceCaller;
public void remoteCall(String message) {
class RemoteCallable implements Callable<Void> {
String message;
public RemoteCallable( String message)
{
this.message = message;
}
public Void call() throws Exception{
serviceCaller.updateDetails( this.message);
return null;
}
}
RetriableHelper<Void> retriableHelper = new RetriableHelper<Void>(5, 500, new RemoteCallable( message));
try {
retriableHelper.call();
} catch (Exception e) {
throw e;
}
}
}
答案 5 :(得分:0)
如果您使用spring,那么最好使用Aspects。
否则,下面的示例解决方案可以工作:
public class Test
{
public static void main(String[] args) throws Exception
{
Test test = new Test();
test.toRunFirst("Hello! This is normal invocation");
runWithRetry(test, "toRunFirst", "Hello! This is First, called with retry");
runWithRetry(test, "toRunSecond", "Hello! This is Second, called with retry");
}
public void toRunFirst(String s) {
System.out.println(s);
}
public void toRunSecond(String s) {
System.out.println(s);
}
public static Object runWithRetry(Object obj, String methodName, Object... args) throws Exception
{
Class<?>[] paramClass = new Class<?>[args.length];
for(int i=0; i< args.length; i++) {
paramClass[i] = args[i].getClass();
}
Method method = obj.getClass().getDeclaredMethod(methodName, paramClass);
int retryCount = 2;
for(int i=0; i< retryCount; i++) {
try {
return method.invoke(obj, args);
}
catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
}
答案 6 :(得分:0)
我没有找到我需要的东西,所以有我的。 主要功能是它在达到 maxRetries 时抛出您需要的异常类型,以便您可以在调用中捕获它。
import org.apache.log4j.Logger;
public class TaskUtils {
public static <E extends Throwable> void retry(int maxRetries, Task<E> task) throws E {
retry(maxRetries, 0, null, task);
}
public static <E extends Throwable> void retry(int maxRetries, long waitTimeMs, Logger logger, Task<E> task) throws E {
while (maxRetries > 0) {
maxRetries--;
try {
task.run();
} catch (Exception e) {
if (maxRetries == 0) {
try {
throw e;
} catch (Exception ignored) { // can't happen but just in case we wrap it in
throw new RuntimeException(e);
}
}
if (logger != null)
logger.warn("Attempt " + maxRetries + " failed", e);
try {
Thread.sleep(waitTimeMs);
} catch (InterruptedException ignored) {
}
}
}
}
public interface Task<E extends Throwable> {
void run() throws E;
}
}
用法:
TaskUtils.retry(3, 500, LOGGER, () -> stmClickhouse.execute(
"ALTER TABLE `" + database + "`.`" + table.getName() + "` ON CLUSTER " + clusterName + allColumnsSql
));