All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.tinkerpop.blueprints.util.TransactionRetryHelper Maven / Gradle / Ivy

The newest version!
package com.tinkerpop.blueprints.util;

import com.tinkerpop.blueprints.TransactionalGraph;

import java.util.Set;

/**
 * Creates a TransactionGraph "holder" which allows execution of a TransactionWork instance inside of a
 * TransactionRetryStrategy implementation.
 *
 * @param  The return value of the work.
 */
public class TransactionRetryHelper {
    private final TransactionalGraph graph;
    private final TransactionWork work;

    private TransactionRetryHelper(final Builder builder) {
        this.graph = builder.getGraph();
        this.work = builder.getWork();
    }

    /**
     * Executes the work committing if possible and rolling back on failure.  On failure, not exception is reported.
     */
    public T fireAndForget() {
        return use(new TransactionRetryStrategy.FireAndForget());
    }

    /**
     * Executes the work committing if possible and rolling back on failure.  On failure an exception is reported.
     */
    public T oneAndDone() {
        return use(new TransactionRetryStrategy.OneAndDone());
    }

    /**
     * Executes the work with a default number of retries with a default number of milliseconds delay between
     * each try.
     */
    public T retry() {
        return use(new TransactionRetryStrategy.DelayedRetry());
    }

    /**
     * Executes the work with a specified number of retries with a default number of milliseconds delay between
     * each try.
     */
    public T retry(final int retries) {
        return use(new TransactionRetryStrategy.DelayedRetry(retries, TransactionRetryStrategy.DelayedRetry.DEFAULT_DELAY_MS));
    }

    /**
     * Executes the work with a specified number of retries with a specified number of milliseconds delay between
     * each try.
     */
    public T retry(final int retries, final long delayBetweenRetries) {
        return use(new TransactionRetryStrategy.DelayedRetry(retries, delayBetweenRetries));
    }

    /**
     * Executes the work with a specified number of retries with a specified number of milliseconds delay between
     * each try.
     *
     * @param exceptionsToRetryOn For a retry to happen, it must be in this set of accepted exceptions.  If an
     *                            exception raises that is not in the set, then an error is immediately raised
     *                            with no retry.
     */
    public T retry(final int retries, final long delayBetweenRetries, final Set exceptionsToRetryOn ) {
        return use(new TransactionRetryStrategy.DelayedRetry(retries, delayBetweenRetries, exceptionsToRetryOn));
    }

    /**
     * Executes the work with a default number of retries with a exponentially increasing number of milliseconds
     * between each retry.
     */
    public T exponentialBackoff() {
        return use(new TransactionRetryStrategy.ExponentialBackoff());
    }

    /**
     * Executes the work with a specified number of retries with a exponentially increasing number of milliseconds
     * between each retry.
     */
    public T exponentialBackoff(final int retries) {
        return use(new TransactionRetryStrategy.ExponentialBackoff(
                retries, TransactionRetryStrategy.ExponentialBackoff.DEFAULT_DELAY_MS));
    }

    /**
     * Executes the work with a specified number of retries with a exponentially increasing number of milliseconds
     * between each retry.
     */
    public T exponentialBackoff(final int retries, final long initialDelay) {
        return use(new TransactionRetryStrategy.ExponentialBackoff(
                retries, initialDelay));
    }

    /**
     * Executes the work with a specified number of retries with a exponentially increasing number of milliseconds
     * between each retry.
     *
     * @param exceptionsToRetryOn For a retry to happen, it must be in this set of accepted exceptions.  If an
     *                            exception raises that is not in the set, then an error is immediately raised
     *                            with no retry.
     */
    public T exponentialBackoff(final int retries, final long initialDelay, final Set exceptionsToRetryOn) {
        return use(new TransactionRetryStrategy.ExponentialBackoff(
                retries, initialDelay, exceptionsToRetryOn));
    }

    /**
     * Executes the work with a specified TransactionRetryStrategy.
     */
    public T use(final TransactionRetryStrategy strategy) {
        return strategy.execute(this.graph, this.work);
    }

    /**
     * Constructs a TransactionRetryStrategy.
     */
    public static class Builder {
        private final TransactionalGraph graph;
        private TransactionWork work;

        public Builder(final TransactionalGraph graph) {
            this.graph = graph;
        }

        public Builder perform(final TransactionWork work) {
            this.work = work;
            return this;
        }

        public TransactionRetryHelper build() {
            return new TransactionRetryHelper(this);
        }

        public TransactionalGraph getGraph() {
            return graph;
        }

        public TransactionWork getWork() {
            return work;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy