gpt4 book ai didi

java - MethodInterceptor 重试 HttpInvoker 调用

转载 作者:行者123 更新时间:2023-11-30 04:19:30 25 4
gpt4 key购买 nike

我在富客户端 GUI 上使用 Spring Framework HttpInvoker。有时人们的互联网连接出现故障,互联网连接故障会导致应用程序崩溃。我想在放弃之前重试失败的方法几次。

我尝试编写一个方法拦截器来完成此任务,但第二次调用:

Object result = methodInvocation.proceed();

总是用包装 NullPointerExceptionRuntimeException 来轰炸。

你不能多次调用这个方法methodInitation.proceed()吗?或者有什么技巧吗?

public class RetryConnectionTool implements MethodInterceptor
{
private static int FailureCount = 0;
static Logger logger = Logger.getLogger(RetryConnectionTool.class);
/**
* 4 seconds of sleep
*/
private int SleepTime = 4000;

public RetryConnectionTool()
{
}

public Object invoke(MethodInvocation methodInvocation) throws Throwable
{
return tryInvoke(methodInvocation, new Integer(0));
}

private Object tryInvoke(MethodInvocation methodInvocation, Integer tryCount) throws Throwable
{
try
{
//if we have failed 10 times in the past or retried 3 times to no success shut it down
if (FailureCount >= 10 || (tryCount != null && tryCount >= 3))
{
logger.error("internet issue failure " + methodInvocation.getMethod().toGenericString());
System.exit(-1);
return null;

}
if (tryCount != null && tryCount >= 1)
{
if (tryCount == 0) //increment the failure count on every first retry
FailureCount++;
tryCount++;
Thread.sleep(SleepTime);
}


Object result = methodInvocation.proceed();

//if we have tried more than once and there is already a record of a failure we try again
if (tryCount != null && tryCount > 1 && FailureCount > 1)
{
String messagePassed = "There seems to be a problem with your internet connection. It the problem persists Iridium Suite will be forced to close.\n" +
"Please evaluate your internet connectivity.";
JOptionPane.showMessageDialog(null, messagePassed, "WARNING", JOptionPane.WARNING_MESSAGE);
}
return result;

}
catch (org.springframework.remoting.RemoteConnectFailureException x)
{
logger.error("internet issue " + methodInvocation.getMethod().toGenericString(), x);
//retry on failure
return tryInvoke(methodInvocation, tryCount);
}
catch (RemoteLookupFailureException x)
{
logger.error("internet issue " + methodInvocation.getMethod().toGenericString(), x);
//retry on failure
return tryInvoke(methodInvocation, tryCount);
}
catch (java.net.ConnectException x)
{
logger.error("internet issue " + methodInvocation.getMethod().toGenericString(), x);
//retry on failure
return tryInvoke(methodInvocation, tryCount);
}
catch (RuntimeException x)
{
throw x;
}
catch (Exception x)
{
throw x;
}
catch (Throwable x)
{
throw x;
}

}
}

最佳答案

使用:((ProxyMethodInitation)调用).invocableClone().proceed();

每个方法调用只能调用一次proceed。请参阅the "Invoking proceed() more than once" section of the "Professional Java Development with the Spring Framework" book.

关于java - MethodInterceptor 重试 HttpInvoker 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17439969/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com