自己写的在服务器上一段重试次数的程序,对于TAIR操作需要加乐观锁版本号防止集群上数据安全

封装类

package aa;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class TairMethodWrapper {

	private static Map<String, TairRetryEntry> superdealCache = new ConcurrentHashMap();

	public static interface SuperdealTairMethodWrapperInvoke {
		void invoke();
	}

	class TairRetryEntry {
		long writeTime;
		int count;
	}

	public void invoke(SuperdealTairMethodWrapperInvoke superdealTairMethodWrapperInvoke,
			String type, Long repeatInterval, Integer repeatCount) {
		// TODO Auto-generated method stub
		if(!isRetry(type, repeatInterval, repeatCount)) {
			isRetry(type, repeatInterval, repeatCount);
		}
	}
	

	protected boolean isRetry(String type, Long repeatInterval,
			Integer repeatCount) {
		TairRetryEntry entry = superdealCache.get(type);
		if (entry == null) {
			entry = new TairRetryEntry();
			entry.count = 0;
			entry.writeTime = System.currentTimeMillis();
			superdealCache.put(type, entry);
		} else if ((System.currentTimeMillis() - entry.writeTime) >= repeatInterval) {
			entry.count = 0;
			entry.writeTime = System.currentTimeMillis();
		}
		
		entry.count++;
		if (entry.count >= repeatCount) {
			return true;
		}
		//do our method here  if success,return true
		
		
		return false;
	}

}


调用方法

import aa.TairMethodWrapper;
import aa.TairMethodWrapper.SuperdealTairMethodWrapperInvoke;










public class TestRetry1 {

	public static void main(String[] args) {
		retry1();
	}
	
	private static void retry1() {
		 new TairMethodWrapper().invoke(new SuperdealTairMethodWrapperInvoke(){

			@Override
			public void invoke() {
				// TODO Auto-generated method stub
				
			}}, "featuredeal", Long.parseLong("150"), 5);
	}
}





你可能感兴趣的:(自己写的在服务器上一段重试次数的程序,对于TAIR操作需要加乐观锁版本号防止集群上数据安全)