自己写的在服务器上一段重试次数的程序,对于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);
	}
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值