递归算法最后返回值问题


public String set(byte[] val) throws Exception{
    Cluster cluster=getRandomMaster(writableCluster);
    SSDB ssdb=cluster.getMaster();
    System.out.println("set cluster.id:"+cluster.getId());
    String key=StringUtils.leftAppendZero(cluster.getId(),4)+ MD5Util.getMD5String(val);
    System.out.println("set key01:"+key);
    if(ssdb.isOpen()){
         ssdb.set(key, val);
    }else{
         set(val);//如果前面取出的不可用,则再回调重新选一个可写服务器
    }
    System.out.println("set key02:"+key);
    return key;
}

输出结果:

set cluster.id:3
set key01:0003637c62818f5f42f73fdd4c214eaafad6
set cluster.id:4
set key01:0004637c62818f5f42f73fdd4c214eaafad6
set cluster.id:2
set key01:0002637c62818f5f42f73fdd4c214eaafad6
set key02:0002637c62818f5f42f73fdd4c214eaafad6
set key02:0004637c62818f5f42f73fdd4c214eaafad6
set key02:0003637c62818f5f42f73fdd4c214eaafad6
最终返回:0003637c62818f5f42f73fdd4c214eaafad6


public String set(byte[] val) throws Exception{
    Cluster cluster=getRandomMaster(writableCluster);
    SSDB ssdb=cluster.getMaster();
    System.out.println("set cluster.id:"+cluster.getId());
    String key=StringUtils.leftAppendZero(cluster.getId(),4)+ MD5Util.getMD5String(val);
    System.out.println("set key01:"+key);
    if(ssdb.isOpen()){
         ssdb.set(key, val);
    }else{
        key = set(val);//如果前面取出的不可用,则再回调重新选一个可写服务器
    }
    System.out.println("set key02:"+key);
    return key;
}

输出结果:
set cluster.id:3
set key01:0003637c62818f5f42f73fdd4c214eaafad6
set cluster.id:4
set key01:0004637c62818f5f42f73fdd4c214eaafad6
set cluster.id:2
set key01:0002637c62818f5f42f73fdd4c214eaafad6
set key02:0002637c62818f5f42f73fdd4c214eaafad6
set key02:0002637c62818f5f42f73fdd4c214eaafad6
set key02:0002637c62818f5f42f73fdd4c214eaafad6

最终返回:0002637c62818f5f42f73fdd4c214eaafad6

 

你可能感兴趣的:(java)