閱讀331 返回首頁    go 阿裏雲 go 技術社區[雲棲]


自己寫的在服務器上一段重試次數的程序,對於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);
	}
}





最後更新:2017-04-03 08:26:17

  上一篇:go C# 係統應用之ListView控件 (三).添加ContextMenuStrip右鍵菜單打開刪除文件
  下一篇:go ODPS產品商業化模式介紹及價格總覽