Package io.fathom.cloud.compute.scheduler

Source Code of io.fathom.cloud.compute.scheduler.OptimisticRetry

package io.fathom.cloud.compute.scheduler;

import io.fathom.cloud.CloudException;

import java.util.concurrent.Callable;

import javax.inject.Inject;
import javax.inject.Singleton;
import javax.persistence.OptimisticLockException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.persist.UnitOfWork;

@Singleton
@Deprecated
public class OptimisticRetry {
    private static final Logger log = LoggerFactory.getLogger(OptimisticRetry.class);

    @Inject
    UnitOfWork unitOfWork;

    public <T> T run(Callable<T> runnable) throws CloudException {
        int attempt = 0;

        while (true) {
            attempt++;

            unitOfWork.begin();
            try {
                T t = runnable.call();
                return t;
            } catch (OptimisticLockException e) {
                log.warn("Retrying update after OptimisticLockException", e);
                continue;
            } catch (Exception e) {
                throw new CloudException("Error performing update", e);
            } finally {
                unitOfWork.end();
            }
        }
    }
}
TOP

Related Classes of io.fathom.cloud.compute.scheduler.OptimisticRetry

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.