| package com.dy.pmsOther.dyFm; | 
|   | 
| import java.util.Calendar; | 
|   | 
| public class FileNameIdUtil { | 
|   | 
|     private static int add = 0 ; | 
|     private static int chengShu = 1000 ; | 
|     private static int maxAdd = 999 ; | 
|     private static long last = 0 ; | 
|      | 
|     //后缀 | 
|     //在分布式系统中,例如多个业务中间件dataMw,多个系统都会向数据库中插入数据,用的都是此ID生成器, | 
|     //此ID生成器在各个子系统中难免为同一类数据生成相同的ID,造成数据库插入因主键相同而报错, | 
|     //所以设计此后缀,每个子系统后缀不同 | 
|     private static String suffix = "0" ; | 
|      | 
|     static { | 
|         last = current() ; | 
|     } | 
|      | 
|     /** | 
|      * 为自实现程序提供的ID生成器 | 
|      * 15长度ID,年度取两位,如果是17位长度ID,年度取四位,那17位数字超出了javascript的表数范围  | 
|      */ | 
|     public  String generate(){ | 
|         return doGenerate() ;     | 
|     } | 
|      | 
|     /** | 
|      * 设置后缀,不同子系统设置不同的后缀 | 
|      * @param suffix | 
|      */ | 
|     public static void setSuffix(String suffix)throws Exception{ | 
|         if(suffix == null || suffix.trim().equals("")){ | 
|             throw new Exception("后缀不能为空") ; | 
|         } | 
|         FileNameIdUtil.suffix = suffix.trim() ; | 
|     } | 
|      | 
|     /** | 
|      * 执行生成 | 
|      * @return | 
|      */ | 
|     private synchronized String doGenerate(){ | 
|         Long id = null ; | 
|         long now = current() ; | 
|         if(now != last){ | 
|             //上次生成ID 与本次生成ID 不在同一秒内 | 
|             last = now ; | 
|             add = 0 ; | 
|             id = last * chengShu + add ++; | 
|         }else{ | 
|             //上次生成ID 与本次生成ID 在同一秒内 | 
|             if(add == maxAdd){ | 
|                 //附加量已经用尽 | 
|                 waitNextSecond(last) ;//等到下一秒 | 
|                 id = last * chengShu + add ++ ;//返回上一秒生成的ID | 
|                 add = 0 ;//附加量归零,为下一秒准备 | 
|             }else{ | 
|                 //附加量未用尽 | 
|                 id = last * chengShu + add ++ ; | 
|             } | 
|         } | 
|         return id + suffix ;     | 
|     } | 
|     /** | 
|      * 等待下一秒到来 | 
|      * @param last | 
|      */ | 
|     private void waitNextSecond(Long last){ | 
|         try { | 
|             Thread.sleep(10); | 
|         } catch (InterruptedException e) { | 
|         }finally{ | 
|             long now = current() ; | 
|             if(now == last){ | 
|                 waitNextSecond(last) ; | 
|             } | 
|         } | 
|     } | 
|   | 
|     | 
|      | 
|     /** | 
|      * 格式为 150516010203 | 
|      * @return | 
|      */ | 
|     private static long current(){ | 
|         Calendar cal = Calendar.getInstance(); | 
|         long d = (cal.get(Calendar.YEAR) % 100) * 10000000000L  | 
|         + (cal.get(Calendar.MONTH) + 1) * 100000000L  | 
|         + cal.get(Calendar.DAY_OF_MONTH) * 1000000L  | 
|         + cal.get(Calendar.HOUR_OF_DAY) * 10000L  | 
|         + cal.get(Calendar.MINUTE) * 100L  | 
|         + cal.get(Calendar.SECOND)  ; | 
|          | 
|         return d ; | 
|     } | 
|   | 
|     public static void main(String args[]){ | 
|         FileNameIdUtil o = new FileNameIdUtil() ; | 
|         int total = 800 ; | 
|         long start = System.currentTimeMillis() ; | 
|         for(int i = 0 ; i < total ; i++){ | 
|             System.out.println((String)(o.generate())) ; | 
|         } | 
|         long end = System.currentTimeMillis() ; | 
|         System.out.println("产生" + total + "ID用时" + (end - start) + "毫秒"); | 
|     } | 
|      | 
| } |