풍부한 클라이언트 GUI에서 Spring Framework HttpInvoker
을 사용하고 있습니다. 때로 사람들은 인터넷 연결이 끊어지며 인터넷 연결이 끊어지면 응용 프로그램이 중단됩니다. 포기하기 전에 실패한 방법을 두 번 다시 시도하고 싶습니다.MethodInterceptor가 HttpInvoker 호출을 다시 시도합니다.
Object result = methodInvocation.proceed();
폭탄 RuntimeException
가 NullPointerException
배치와 아웃 :
이 메서드를 methodInvocation.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;
}
}
}
귀하의 질문에 대한 답변이 아닙니다. 그러나 Spring [documentation] (http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/aop-api.html#aop-api-introduction)에서는 다음을 사용하여 AOP를 사용할 것을 권장합니다. @AspectJ 대신 Spring 자체의 AOP API를 사용합니다. 그것을 사용하는 것을 고려하십시오. – Vinay
나는 어쨌든 aspectj 인 2.5를 사용하고 있다고 확신한다. 내 문제에 대한 통찰력? – springcorn
"확실한"대신에 @AspectJ를 시도해보고 무슨 일이 일어나는 지 알 수 있습니다. IMO는 간단한 pointcut + advice를 작성하는 것이 자신의 인터셉터 클래스를 작성하는 것보다 훨씬 간단합니다. 프록시 기반 Spring AOP만으로는 충분하지 않다면, LTW를 통해 언제든지 AspectJ 전체에 의지 할 수 있습니다. – kriegaex