springcorn
springcorn

Reputation: 631

MethodInterceptor to retry HttpInvoker call

I am using the Spring Framework HttpInvokers on a rich client gui. Sometimes people have blips in their internet connection and the internet connection failure causes the application to crash. I would like to retry a failed method a couple times before giving up.

I tried writing a method interceptor to accomplish this but the second call of:

Object result = methodInvocation.proceed();

always bombs out with a RuntimeException wrapping a NullPointerException.

Can you not call this method methodInvocation.proceed() multiple times or is there some trick to it?

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;
            }

        }
    }

Upvotes: 1

Views: 808

Answers (1)

user5285915
user5285915

Reputation: 21

Use: ((ProxyMethodInvocation) invocation).invocableClone().proceed();

proceed can be called only once per MethodInvocation. See the "Invoking proceed() more than once" section of the "Professional Java Development with the Spring Framework" book.

Upvotes: 2

Related Questions