java
에 에 대해 unique order number
을 생성해야합니다. time of creation of order
은 충분히 고유 한 값이라고 생각했습니다. Two orders cannot be created at the same second
. 다른 사람이 주문 번호를 사용하지 못하도록 일부 생성 시간 값을 추측하여 작성 시간 문자열의 hash
부분을 추가하여 최종 주문 번호 문자열로 만들었습니다. 창조의 시간을 기준으로 주문 번호를 생성 고객의 주문에 대해 고유하면서도 주문 된 주문 번호를 만들려면
이 방법에 어떤 보이지 않는 함정이 있습니까
은? 시스템에서 생성 된 주문에 대해 약간의 정렬 순서를 제공하기위한되었다 .. 코드는public static String createOrderNumber(Date orderDate) throws NoSuchAlgorithmException {
DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
String datestring = df.format(orderDate).toString();
System.out.println("datestring="+datestring);
System.out.println("datestring size="+datestring.length());
String hash = makeHashString(datestring);//creates SHA1 hash of 16 digits
System.out.println("hash="+hash);
System.out.println("hash size="+hash.length());
int datestringlen = datestring.length();
String ordernum = datestring+hash.substring(datestringlen,datestringlen+5);
System.out.println("ordernum size="+ordernum.length());
return ordernum;
}
private static String makeHashString(String plain) throws NoSuchAlgorithmException {
final int MD_PASSWORD_LENGTH = 16;
final String HASH_ALGORITHM = "SHA1";
String hash = null;
try {
MessageDigest md = MessageDigest.getInstance(HASH_ALGORITHM);
md.update(plain.getBytes());
BigInteger hashint = new BigInteger(1, md.digest());
hash = hashint.toString(MD_PASSWORD_LENGTH);
} catch (NoSuchAlgorithmException nsae) {
throw(nsae);
}
return hash;
}
여기 주어진다
샘플 출력은
datestring=20110924103251
datestring size=14
hash=a9bcd51fc69d9225c5d96061d9c8628137df14e0
hash size=40
ordernum size=19
ordernum=2011092410325125c5d
* "두 번째 주문은 동일한 초에 만들 수 없습니다."* 서버가 초당 20 주문을 처리하는 경우 가능합니다. –