57 lines
1.5 KiB
Plaintext
Executable File
57 lines
1.5 KiB
Plaintext
Executable File
package com.fp.common.fin;
|
|
|
|
import java.text.DecimalFormat;
|
|
import java.text.SimpleDateFormat;
|
|
|
|
/**
|
|
* Clase que se encarga de generar un numero de mensaje para procesar transacciones financieras.
|
|
* @author Jorge Vaca
|
|
* @version 2.1
|
|
*/
|
|
public class MessageGenerator {
|
|
/*Almacena una instancia de MessageGenerator.*/
|
|
public static MessageGenerator cache;
|
|
|
|
/*Monitor para sincronizar la obtencion del numero de mensaje.*/
|
|
private boolean monitor=false;
|
|
|
|
/*Formato para obtener la fecha del servidor de aplicaciones.*/
|
|
private SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMMdd-HH:mm:ss.SSSSSS");
|
|
|
|
/**
|
|
* Entrega una instancia de MessageGenerator.
|
|
* @return MessageGenerator
|
|
* @throws Exception
|
|
*/
|
|
public static MessageGenerator getInstance() throws Exception {
|
|
synchronized (MessageGenerator.class) {
|
|
if(cache == null){
|
|
cache = new MessageGenerator();
|
|
}
|
|
}
|
|
return cache;
|
|
}
|
|
|
|
/**
|
|
* Entrega un numero de mensaje dado un prefijo.
|
|
* @param pPrefix Prefijo del numero de mensaje.
|
|
* @return String
|
|
* @throws Exception
|
|
*/
|
|
public synchronized String generateId(String pPrefix) throws Exception{
|
|
while(monitor){
|
|
wait();
|
|
}
|
|
try{
|
|
monitor=true;
|
|
Double d = Math.floor((Math.random() * 100000));
|
|
DecimalFormat df = new DecimalFormat("00000");
|
|
return pPrefix+"-"+sdf.format(System.currentTimeMillis()) +"-"+ df.format(d);
|
|
}finally{
|
|
monitor=false;
|
|
notifyAll();
|
|
}
|
|
}
|
|
|
|
}
|