예제 #1
0
        /**
         * Convenience utility: creates a "session fail" retry loop calling the given proc
         *
         * @param client Zookeeper
         * @param mode how to handle session failures
         * @param proc procedure to call with retry
         * @param <T> return type
         * @return procedure result
         * @throws Exception any non-retriable errors
         */
        public static T callWithRetry <T>(CuratorZookeeperClient client, Mode mode, ICallable <T> proc)
        {
            T result = default(T);
            SessionFailRetryLoop retryLoop = client.newSessionFailRetryLoop(mode);

            retryLoop.start();
            try
            {
                while (retryLoop.shouldContinue())
                {
                    try
                    {
                        result = proc.call();
                    }
                    catch (Exception e)
                    {
                        ThreadUtils.checkInterrupted(e);
                        retryLoop.takeException(e);
                    }
                }
            }
            finally
            {
                retryLoop.Dispose();
            }
            return(result);
        }
예제 #2
0
 public void run()
 {
     if (state != NEW)
     {
         return;
     }
     try
     {
         ICallable <T> c = callable;
         if (c != null && state == NEW)
         {
             T    result;
             bool ran;
             try
             {
                 result = c.call();
                 ran    = true;
             }
             catch (Exception ex)
             {
                 result = default(T);
                 ran    = false;
                 setException(ex);
             }
             if (ran)
             {
                 set(result);
             }
         }
     }
     finally
     {
         // read volatile field into local var to prevent two volatile reads
         int s = state;
         if (s >= INTERRUPTING)
         {
             handlePossibleCancellationInterrupt(s);
         }
     }
 }
예제 #3
0
        /**
         * Convenience utility: creates a retry loop calling the given proc and retrying if needed
         *
         * @param client Zookeeper
         * @param proc procedure to call with retry
         * @param <T> return type
         * @return procedure result
         * @throws Exception any non-retriable errors
         */
        public static T callWithRetry <T>(CuratorZookeeperClient client,
                                          ICallable <T> proc)
        {
            T         result    = default(T);
            RetryLoop retryLoop = client.newRetryLoop();

            while (retryLoop.shouldContinue())
            {
                try
                {
                    client.internalBlockUntilConnectedOrTimedOut();

                    result = proc.call();
                    retryLoop.markComplete();
                }
                catch (Exception e)
                {
                    ThreadUtils.checkInterrupted(e);
                    retryLoop.takeException(e);
                }
            }
            return(result);
        }
예제 #4
0
        /**
         * Executes the computation without setting its result, and then
         * resets this future to initial state, failing to do so if the
         * computation encounters an exception or is cancelled.  This is
         * designed for use with tasks that intrinsically execute more
         * than once.
         *
         * @return {@code true} if successfully run and reset
         */
        protected bool runAndReset()
        {
            if (state != NEW)
            {
                return(false);
            }
            bool ran = false;
            int  s   = state;

            try
            {
                ICallable <T> c = callable;
                if (c != null && s == NEW)
                {
                    try
                    {
                        c.call(); // don't set result
                        ran = true;
                    }
                    catch (Exception ex)
                    {
                        setException(ex);
                    }
                }
            }
            finally
            {
                // read volatile field into local var to prevent two volatile reads
                s = state;
                if (s >= INTERRUPTING)
                {
                    handlePossibleCancellationInterrupt(s);
                }
            }
            return(ran && s == NEW);
        }