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

来源:互联网 发布:linux c mysql编程 编辑:程序博客网 时间:2024/06/07 23:10

封装类

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 stubif(!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 truereturn 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(){@Overridepublic void invoke() {// TODO Auto-generated method stub}}, "featuredeal", Long.parseLong("150"), 5);}}





0 0
原创粉丝点击