首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >重试HttpInvoker调用的MethodInterceptor

重试HttpInvoker调用的MethodInterceptor
EN

Stack Overflow用户
提问于 2013-07-03 12:56:49
回答 1查看 663关注 0票数 1

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

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

代码语言:javascript
复制
Object result = methodInvocation.proceed();

总是用包装了NullPointerExceptionRuntimeException来轰炸。

你能不能不多次调用这个方法methodInvocation.proceed(),或者它有什么窍门?

代码语言:javascript
复制
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;
            }

        }
    }
EN

回答 1

Stack Overflow用户

发布于 2015-09-01 02:58:58

用法:((ProxyMethodInvocation) invocation).invocableClone().proceed();

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17439969

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档