String identifier = UUID.randomUUID().toString();
String czset = semname + ":owner";
String ctr = semname + ":counter";
long now = System.currentTimeMillis();
Transaction trans = conn.multi();
trans.zremrangeByScore(
semname.getBytes(),
"-inf".getBytes(),
String.valueOf(now - timeout).getBytes());
ZParams params = new ZParams();
params.weights(1, 0);
trans.zinterstore(czset, params, czset, semname);
trans.incr(ctr);
List<Object> results = trans.exec();
int counter = ((Long)results.get(results.size() - 1)).intValue();
trans = conn.multi();
trans.zadd(semname, now, identifier);
trans.zadd(czset, counter, identifier);
trans.zrank(czset, identifier);
results = trans.exec();
int result = ((Long)results.get(results.size() - 1)).intValue();
if (result < limit){
return identifier;
}
trans = conn.multi();
trans.zrem(semname, identifier);
trans.zrem(czset, identifier);
trans.exec();
return null;
}